lights
Class Tuple

java.lang.Object
  |
  +--lights.Tuple
All Implemented Interfaces:
ITuple, java.io.Serializable

public class Tuple
extends java.lang.Object
implements ITuple, java.io.Serializable

The definition of a tuple, i.e., an ordered sequence of typed fields. The type of a field can be any class implementing the Serializable interface. A field can either contain a value, or be characterized just by its type. In Linda jargon, the former is called an actual field, while the latter is called a formal field. Tuples with formals are typically used as a template to query the tuple space by pattern matching. Tuple fields are accessed by their index, with the first element conventionally set to index 0.

The interface, defined by ITuple provides methods to manipulate the fields of a tuple in various ways. All methods return the tuple resulting from the manipulation, so that cascaded operations are possible, e.g.,

System.out.println(t.add(obj1).add(obj2).add(obj3).toString());
Matching is determined by the matches method.

this.matches(t) returns true if:

  1. this and t have the same number of fields;
  2. all the fields of this match the corresponding field of t.
or
  1. this has no fields.
This latter rule expresses the convention adopted in this package to consider tuples with no fields as templates that match any tuple, independently from the type and number of fields. This is particularly useful when using group operations.

Notably, the matching rules above do not depend on the type of the tuple. Thus, for instance, if this is of class Tuple, and t is of type MyTuple, extending Tuple, as long as they comply with the two rules above they still match, even though their types are different.
Different matching rules, e.g., taking into account subtyping, or allowing matching between tuples with different arity, can be provided by overriding the method matches.

Version:
1.0
Author:
Gian Pietro Picco
See Also:
Serialized Form

Constructor Summary
Tuple()
          Creates an uninitialized tuple.
 
Method Summary
 ITuple add(IField field)
          Adds a field at the end of the tuple.
 ITuple addActual(java.io.Serializable obj)
          Adds an actual field, whose value is the given object, at the end of the tuple.
 ITuple addFormal(java.lang.Class classObj)
          Adds a formal field, whose type is the given class, at the end of the tuple.
 IField get(int index)
          Returns the field at position index.
 IField[] getFields()
          Returns all the fields in this tuple.
 ITuple insertAt(IField field, int index)
          Inserts the given field at position index.
 int length()
          Returns the number of fields in the tuple.
 boolean matches(ITuple tuple)
          Determines the rule used for pattern matching between tuples.
 ITuple removeAt(int index)
          Removes the field at position index.
 ITuple set(IField field, int index)
          Replaces the field at position index with the given one.
 java.lang.String toString()
          Returns a string representation of the field.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Tuple

public Tuple()
Creates an uninitialized tuple.
Method Detail

add

public ITuple add(IField field)
Adds a field at the end of the tuple.
Specified by:
add in interface ITuple
Returns:
the resulting tuple.

addActual

public ITuple addActual(java.io.Serializable obj)
Adds an actual field, whose value is the given object, at the end of the tuple. The type of the field is automatically set to the object's class.
Specified by:
addActual in interface ITuple
Returns:
the resulting tuple.

addFormal

public ITuple addFormal(java.lang.Class classObj)
Adds a formal field, whose type is the given class, at the end of the tuple.
Specified by:
addFormal in interface ITuple
Returns:
the resulting tuple.

set

public ITuple set(IField field,
                  int index)
Replaces the field at position index with the given one.
Specified by:
set in interface ITuple
Returns:
the resulting tuple.

get

public IField get(int index)
Returns the field at position index.
Specified by:
get in interface ITuple

insertAt

public ITuple insertAt(IField field,
                       int index)
Inserts the given field at position index. All the fields whose position is greater than index are shifted downwards, i.e., their index is increased by one.
Specified by:
insertAt in interface ITuple
Returns:
the resulting tuple.

removeAt

public ITuple removeAt(int index)
Removes the field at position index. The fields whose position is greater than index are shifted upwards, i.e., their index is decreased by one.
Specified by:
removeAt in interface ITuple
Returns:
the resulting tuple.

getFields

public IField[] getFields()
Returns all the fields in this tuple.
Specified by:
getFields in interface ITuple
Returns:
an array containing the fields of this tuple.

length

public int length()
Returns the number of fields in the tuple.
Specified by:
length in interface ITuple

matches

public boolean matches(ITuple tuple)
Determines the rule used for pattern matching between tuples. this.matches(t) returns true if:
  1. this and t have the same number of fields;
  2. all the fields of this match the corresponding field of t.
or this has no fields.
Specified by:
matches in interface ITuple
Returns:
true if the tuple passed as a parameter matches this tuple, false otherwise.

toString

public java.lang.String toString()
Returns a string representation of the field.
Overrides:
toString in class java.lang.Object