edu.rice.cs.plt.collect
Class ConcreteRelationIndex<K,V>

java.lang.Object
  extended by edu.rice.cs.plt.collect.ConcreteRelationIndex<K,V>
All Implemented Interfaces:
RelationIndex<K,V>, SizedIterable<Pair<K,V>>, java.io.Serializable, java.lang.Iterable<Pair<K,V>>

public class ConcreteRelationIndex<K,V>
extends java.lang.Object
implements RelationIndex<K,V>, java.io.Serializable

A RelationIndex implementation that maintains concrete data structures to index the contents of the relation. To support mutation in other indices to reflect changes made directly to sets and iterators produced by this index, clients should override the addToRelation(K, V), removeFromRelation(K, V), and clearRelation() methods.

Keys must have a valid hashCode() implementation.

See Also:
Serialized Form

Constructor Summary
ConcreteRelationIndex(Thunk<? extends java.util.Map<K,PredicateSet<V>>> mapFactory, Thunk<? extends java.util.Set<V>> setFactory)
          Create an empty ConcreteRelationIndex.
 
Method Summary
 void added(K key, V value)
          Requests that the index be updated to reflect the addition of the given key/value pair.
protected  void addToRelation(K key, V value)
          Add the given entry to the relation.
 void cleared()
          Requests that the index be cleared to reflect the current state of the relation.
protected  void clearRelation()
          Clear the relation.
 boolean contains(java.lang.Object key, java.lang.Object value)
          Whether the given key-value mapping occurs.
 boolean hasFixedSize()
          true if this iterable is known to have a fixed size.
 boolean isEmpty()
          Whether the iterable does not contain any elements.
 boolean isInfinite()
          true if the iterable is known to have infinite size.
 boolean isStatic()
          true if this iterable is unchanging.
 java.util.Iterator<Pair<K,V>> iterator()
          Iterates through all key-value pairs in the index.
 PredicateSet<K> keys()
          A dynamically-updating view of all keys mapping to at least one value.
 PredicateSet<V> match(K key)
          A dynamically-updating view of all values matching key.
 void removed(K key, V value)
          Requests that the index be updated to reflect the removal of the given key/value pair.
protected  void removeFromRelation(K key, V value)
          Remove the given entry from the relation.
 int size()
          Compute the number of elements in the iterable.
 int size(int bound)
          Compute the number of elements in the iterable, up to the given bound.
protected  void validateAdd(K key, V value)
          Checks that the given pair, which does not appear in this index, can be added to the relation.
protected  void validateClear()
          Checks that the relation can be cleared.
protected  void validateRemove(K key, V value)
          Checks that the given pair, which appears in this index, can be removed from the relation.
protected  void validateRemoveKey(K key, PredicateSet<V> vals)
          Checks that the given key, which appears with at least one value in this index, can be removed with all associated values from the relation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConcreteRelationIndex

public ConcreteRelationIndex(Thunk<? extends java.util.Map<K,PredicateSet<V>>> mapFactory,
                             Thunk<? extends java.util.Set<V>> setFactory)
Create an empty ConcreteRelationIndex.

Parameters:
mapFactory - Produces an empty map for mapping keys to sets of values. The mutability of keys() corresponds to this map's keySet()'s mutability.
setFactory - Produces an initial set to hold values associated with a key.
Method Detail

validateAdd

protected void validateAdd(K key,
                           V value)
Checks that the given pair, which does not appear in this index, can be added to the relation. Called when a client attempts to directly mutate a match set, but before any mutation has occurred. If the add is not permitted, subclasses may throw an exception. By default, does nothing.


addToRelation

protected void addToRelation(K key,
                             V value)
Add the given entry to the relation. Called when a client directly mutates a match set, and after this index has been mutated. By default, does nothing.


validateRemove

protected void validateRemove(K key,
                              V value)
Checks that the given pair, which appears in this index, can be removed from the relation. Called when a client attempts to directly mutate a match set, but before any mutation has occurred. If the removal is not permitted, subclasses may throw an exception. By default, does nothing.


validateRemoveKey

protected void validateRemoveKey(K key,
                                 PredicateSet<V> vals)
Checks that the given key, which appears with at least one value in this index, can be removed with all associated values from the relation. Called when a client attempts to directly mutate the key set or a match set, but before any mutation has occurred. If the removal is not permitted, subclasses may throw an exception. By default, does nothing.


removeFromRelation

protected void removeFromRelation(K key,
                                  V value)
Remove the given entry from the relation. Called when a client directly mutates the key set or a match set, and after this index has been mutated. By default, does nothing.


validateClear

protected void validateClear()
Checks that the relation can be cleared. Called when a client attempts to directly clear the key set, but before any mutation has occurred. If clearing is not permitted, subclasses may throw an exception. By default, does nothing.


clearRelation

protected void clearRelation()
Clear the relation. Called when a client directly clears the key set, and after this index has been mutated. By default, does nothing.


contains

public boolean contains(java.lang.Object key,
                        java.lang.Object value)
Description copied from interface: RelationIndex
Whether the given key-value mapping occurs.

Specified by:
contains in interface RelationIndex<K,V>

keys

public PredicateSet<K> keys()
Description copied from interface: RelationIndex
A dynamically-updating view of all keys mapping to at least one value. Mutation (removal), if supported, will be automatically reflected in the relation being indexed.

Specified by:
keys in interface RelationIndex<K,V>

match

public PredicateSet<V> match(K key)
Description copied from interface: RelationIndex
A dynamically-updating view of all values matching key. May be empty. Mutation, if supported, will be automatically reflected in the relation being indexed.

Specified by:
match in interface RelationIndex<K,V>

iterator

public java.util.Iterator<Pair<K,V>> iterator()
Description copied from interface: RelationIndex
Iterates through all key-value pairs in the index.

Specified by:
iterator in interface RelationIndex<K,V>
Specified by:
iterator in interface java.lang.Iterable<Pair<K,V>>

isEmpty

public boolean isEmpty()
Description copied from interface: SizedIterable
Whether the iterable does not contain any elements.

Specified by:
isEmpty in interface SizedIterable<Pair<K,V>>

size

public int size()
Description copied from interface: SizedIterable
Compute the number of elements in the iterable. If the size is infinite or too large to be represented as an int, Integer.MAX_VALUE should be returned. Otherwise, next() may be safely invoked on the iterator exactly this number of times.

Specified by:
size in interface SizedIterable<Pair<K,V>>

size

public int size(int bound)
Description copied from interface: SizedIterable
Compute the number of elements in the iterable, up to the given bound. If the size is infinite or greater than bound, bound is returned.

Specified by:
size in interface SizedIterable<Pair<K,V>>
Parameters:
bound - Maximum result. Assumed to be nonnegative.

isInfinite

public 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<K,V>>

hasFixedSize

public 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<K,V>>

isStatic

public 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<K,V>>

added

public void added(K key,
                  V value)
Description copied from interface: RelationIndex
Requests that the index be updated to reflect the addition of the given key/value pair. Assumes the pair is not already present.

Specified by:
added in interface RelationIndex<K,V>

removed

public void removed(K key,
                    V value)
Description copied from interface: RelationIndex
Requests that the index be updated to reflect the removal of the given key/value pair. Assumes the pair is present.

Specified by:
removed in interface RelationIndex<K,V>

cleared

public void cleared()
Description copied from interface: RelationIndex
Requests that the index be cleared to reflect the current state of the relation. Assumes the index is non-empty.

Specified by:
cleared in interface RelationIndex<K,V>