edu.rice.cs.plt.collect
Class AbstractRelation<T1,T2>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<T>
          extended by edu.rice.cs.plt.collect.AbstractPredicateSet<Pair<T1,T2>>
              extended by edu.rice.cs.plt.collect.AbstractRelation<T1,T2>
All Implemented Interfaces:
PredicateSet<Pair<T1,T2>>, Relation<T1,T2>, SizedIterable<Pair<T1,T2>>, Predicate<java.lang.Object>, Predicate2<T1,T2>, java.lang.Iterable<Pair<T1,T2>>, java.util.Collection<Pair<T1,T2>>, java.util.Set<Pair<T1,T2>>
Direct Known Subclasses:
AbstractFunctionalRelation, AbstractInjectiveRelation, CartesianRelation, ComposedRelation, FilteredRelation, IndexedRelation, UnindexedRelation, UnionRelation

public abstract class AbstractRelation<T1,T2>
extends AbstractPredicateSet<Pair<T1,T2>>
implements Relation<T1,T2>

An abstract parent class for implementations of Relation. Subclasses must provide the size methods isInfinite(), hasFixedSize(), and isStatic(), and the query methods contains(Object, Object), contains(Object), iterator(), firstSet(), matchFirst(T1), secondSet(), and matchSecond(T2). To support mutation, they must also override add(Object, Object) and remove(Object, Object). For best performance, they may also override AbstractPredicateSet.isEmpty(), AbstractPredicateSet.size(int) and AbstractCollection.clear().


Nested Class Summary
protected  class AbstractRelation.InverseRelation
          An inverse of the enclosing relation, defined in terms of Pair.inverse().
 
Constructor Summary
AbstractRelation()
           
 
Method Summary
 boolean add(Pair<T1,T2> p)
          Invokes add(Object, Object).
 boolean add(T1 first, T2 second)
          Add Pair.make(first, second) to the set.
abstract  boolean contains(java.lang.Object obj)
          Test whether the set contains an object.
abstract  boolean contains(T1 first, T2 second)
          Tests whether the given objects appear as a pair in this relation.
 boolean containsFirst(T1 first)
          Returns firstSet().contains(first).
 boolean containsSecond(T2 second)
          Returns secondSet().contains(second).
 PredicateSet<T2> excludeFirsts()
          Returns secondSet().
 PredicateSet<T1> excludeSeconds()
          Returns firstSet().
abstract  PredicateSet<T1> firstSet()
          The set of firsts.
abstract  boolean hasFixedSize()
          true if this iterable is known to have a fixed size.
 Relation<T2,T1> inverse()
          Returns an AbstractRelation.InverseRelation.
abstract  boolean isInfinite()
          true if the iterable is known to have infinite size.
abstract  boolean isStatic()
          true if this iterable is unchanging.
abstract  java.util.Iterator<Pair<T1,T2>> iterator()
           
abstract  PredicateSet<T2> matchFirst(T1 first)
          The set of seconds corresponding to a specific first.
abstract  PredicateSet<T1> matchSecond(T2 second)
          The set of firsts corresponding to a specific second.
 boolean remove(java.lang.Object o)
          Invokes remove(Object, Object) if contains(o) is true.
 boolean remove(T1 first, T2 second)
          Remove Pair.make(first, second) from the set.
abstract  PredicateSet<T2> secondSet()
          The set of seconds.
 
Methods inherited from class edu.rice.cs.plt.collect.AbstractPredicateSet
isEmpty, size, size
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, containsAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, clear, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, size, toArray, toArray
 
Methods inherited from interface edu.rice.cs.plt.iter.SizedIterable
isEmpty, size, size
 

Constructor Detail

AbstractRelation

public AbstractRelation()
Method Detail

isInfinite

public abstract boolean isInfinite()
Description copied from interface: SizedIterable
true if the iterable is known to have infinite size. If true, an iterator over the iterable in its current state will never return false from hasNext().

Specified by:
isInfinite in interface SizedIterable<Pair<T1,T2>>

hasFixedSize

public abstract boolean hasFixedSize()
Description copied from interface: SizedIterable
true if this iterable is known to have a fixed size. This is the case if the iterable is immutable, or if changes can only replace values, not remove or add them. An infinite iterable may be fixed if it is guaranteed to never become finite.

Specified by:
hasFixedSize in interface SizedIterable<Pair<T1,T2>>

isStatic

public abstract boolean isStatic()
Description copied from interface: SizedIterable
true if this iterable is unchanging. This implies that hasFixedSize() is true, and that iterator() will always return the same (either == or equal() and immutable) elements in the same order. ("Immutable" here means that equals() invocations are consistent over time -- if two objects are equal, they will never become inequal, and vice versa.)

Specified by:
isStatic in interface SizedIterable<Pair<T1,T2>>

contains

public abstract boolean contains(T1 first,
                                 T2 second)
Tests whether the given objects appear as a pair in this relation. Not guaranteed to have types T1 and T2 because the Collection.contains(java.lang.Object) method allows arbitrary objects.

Specified by:
contains in interface Relation<T1,T2>
Specified by:
contains in interface Predicate2<T1,T2>

contains

public abstract boolean contains(java.lang.Object obj)
Description copied from class: AbstractPredicateSet
Test whether the set contains an object. Overridden here to force subclasses to provide an implementation. The default implementation (AbstractCollection.contains(java.lang.Object)) is a linear search, which is almost always unreasonable for a set.

Specified by:
contains in interface Relation<T1,T2>
Specified by:
contains in interface Predicate<java.lang.Object>
Specified by:
contains in interface java.util.Collection<Pair<T1,T2>>
Specified by:
contains in interface java.util.Set<Pair<T1,T2>>
Specified by:
contains in class AbstractPredicateSet<Pair<T1,T2>>

iterator

public abstract java.util.Iterator<Pair<T1,T2>> iterator()
Specified by:
iterator in interface java.lang.Iterable<Pair<T1,T2>>
Specified by:
iterator in interface java.util.Collection<Pair<T1,T2>>
Specified by:
iterator in interface java.util.Set<Pair<T1,T2>>
Specified by:
iterator in class java.util.AbstractCollection<Pair<T1,T2>>

firstSet

public abstract PredicateSet<T1> firstSet()
Description copied from interface: Relation
The set of firsts. Need not allow mutation, but must reflect subsequent changes.

Specified by:
firstSet in interface Relation<T1,T2>

matchFirst

public abstract PredicateSet<T2> matchFirst(T1 first)
Description copied from interface: Relation
The set of seconds corresponding to a specific first. Need not allow mutation, but must reflect subsequent changes.

Specified by:
matchFirst in interface Relation<T1,T2>

secondSet

public abstract PredicateSet<T2> secondSet()
Description copied from interface: Relation
The set of seconds. Need not allow mutation, but must reflect subsequent changes.

Specified by:
secondSet in interface Relation<T1,T2>

matchSecond

public abstract PredicateSet<T1> matchSecond(T2 second)
Description copied from interface: Relation
The set of firsts corresponding to a specific second. Need not allow mutation, but must reflect subsequent changes.

Specified by:
matchSecond in interface Relation<T1,T2>

add

public boolean add(T1 first,
                   T2 second)
Description copied from interface: Relation
Add Pair.make(first, second) to the set.

Specified by:
add in interface Relation<T1,T2>

remove

public boolean remove(T1 first,
                      T2 second)
Description copied from interface: Relation
Remove Pair.make(first, second) from the set.

Specified by:
remove in interface Relation<T1,T2>

add

public boolean add(Pair<T1,T2> p)
Invokes add(Object, Object).

Specified by:
add in interface Relation<T1,T2>
Specified by:
add in interface java.util.Collection<Pair<T1,T2>>
Specified by:
add in interface java.util.Set<Pair<T1,T2>>
Overrides:
add in class java.util.AbstractCollection<Pair<T1,T2>>

remove

public boolean remove(java.lang.Object o)
Invokes remove(Object, Object) if contains(o) is true.

Specified by:
remove in interface Relation<T1,T2>
Specified by:
remove in interface java.util.Collection<Pair<T1,T2>>
Specified by:
remove in interface java.util.Set<Pair<T1,T2>>
Overrides:
remove in class java.util.AbstractCollection<Pair<T1,T2>>

inverse

public Relation<T2,T1> inverse()
Returns an AbstractRelation.InverseRelation.

Specified by:
inverse in interface Relation<T1,T2>

containsFirst

public boolean containsFirst(T1 first)
Returns firstSet().contains(first).

Specified by:
containsFirst in interface Relation<T1,T2>

excludeFirsts

public PredicateSet<T2> excludeFirsts()
Returns secondSet().

Specified by:
excludeFirsts in interface Relation<T1,T2>

containsSecond

public boolean containsSecond(T2 second)
Returns secondSet().contains(second).

Specified by:
containsSecond in interface Relation<T1,T2>

excludeSeconds

public PredicateSet<T1> excludeSeconds()
Returns firstSet().

Specified by:
excludeSeconds in interface Relation<T1,T2>