messif.buckets.index.impl
Class AbstractArrayIndex<K,T>

java.lang.Object
  extended by messif.utility.SortedArrayData<K,T>
      extended by messif.buckets.index.impl.AbstractArrayIndex<K,T>
Type Parameters:
K - the type of keys this index is ordered by
T - the type of objects stored in this index
All Implemented Interfaces:
Addible<T>, Index<T>, ModifiableIndex<T>, ModifiableOrderedIndex<K,T>, OrderedIndex<K,T>
Direct Known Subclasses:
AddressStorageIndex, IntStorageIndex, LongStorageIndex

public abstract class AbstractArrayIndex<K,T>
extends SortedArrayData<K,T>
implements ModifiableOrderedIndex<K,T>

Implementation of index that stores the indexed data in a sorted array. Access to the actual array is abstract through the add, remove and get methods.

All search methods are correctly implemented using binary search on the array whenever possible.


Constructor Summary
AbstractArrayIndex()
           
 
Method Summary
protected abstract  Lock acquireSearchLock()
          Locks this index for searching and returns a lock object if it is supported.
 void finalize()
          Finalize this index.
protected abstract  boolean remove(int index)
          Removes the element at the specified position in this collection.
 ModifiableSearch<T> search()
          Returns a search for all objects in this index.
 ModifiableSearch<T> search(java.util.Collection<? extends K> keys)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
<C> ModifiableSearch<T>
search(IndexComparator<? super C,? super T> comparator, C key)
          Returns a search for objects in this index that have any of the specified keys.
<C> ModifiableSearch<T>
search(IndexComparator<? super C,? super T> comparator, C from, C to)
          Returns a search for objects in this index that are within the specified key-range.
<C> ModifiableSearch<T>
search(IndexComparator<? super C,? super T> comparator, java.util.Collection<? extends C> keys)
          Returns a search for objects in this index that have any of the specified keys.
 ModifiableSearch<T> search(K key, boolean restrictEqual)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
 ModifiableSearch<T> search(K from, K to)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
 ModifiableSearch<T> search(K startKey, K from, K to)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
 
Methods inherited from class messif.utility.SortedArrayData
binarySearch, compare, first, fullSearch, get, indexOf, last, mergeSort, size
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface messif.buckets.index.OrderedIndex
comparator
 
Methods inherited from interface messif.buckets.index.ModifiableIndex
destroy
 
Methods inherited from interface messif.buckets.index.Index
size
 
Methods inherited from interface messif.buckets.Addible
add
 

Constructor Detail

AbstractArrayIndex

public AbstractArrayIndex()
Method Detail

remove

protected abstract boolean remove(int index)
Removes the element at the specified position in this collection.

Parameters:
index - index of the element to remove
Returns:
false if the object was not removed (e.g. because there is no object with this index)

acquireSearchLock

protected abstract Lock acquireSearchLock()
Locks this index for searching and returns a lock object if it is supported. A null is returned otherwise. The called must call the Lock.unlock() method if this method has returned non-null.

Returns:
a lock on this index or null

finalize

public void finalize()
              throws java.lang.Throwable
Description copied from interface: ModifiableIndex
Finalize this index. All transient resources associated with this index are released. After this method is called, the store and retrieval methods' behavior is unpredictable.

Specified by:
finalize in interface ModifiableIndex<T>
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - if there was an error while cleaning

search

public final ModifiableSearch<T> search()
                                 throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for all objects in this index.

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Returns:
a search for all objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final ModifiableSearch<T> search(K key,
                                        boolean restrictEqual)
                                 throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. If the restrictEqual is true, the search returns only objects that are comparator-equal to key.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
key - the key to search for
restrictEqual - if true, the search is restricted only to objects that are comparator-equal to key
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final ModifiableSearch<T> search(K from,
                                        K to)
                                 throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. All objects from the interval [from, to] are returned. If a null value is specified as a boundary, that bound is not restricted. That means that search(x, null) will return all objects from this index that are bigger than or equal to x.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
from - the lower bound on the searched objects, i.e. objects greater or equal are returned
to - the upper bound on the searched objects, i.e. objects smaller or equal are returned
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final ModifiableSearch<T> search(K startKey,
                                        K from,
                                        K to)
                                 throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. All objects from the interval [from, to] are returned. Search starts at the object nearest to the given startKey. If a null value is specified as a boundary, that bound is not restricted. That means that search(start, x, null) will return all objects from this index that are bigger than or equal to x starting at object with key start.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
startKey - the key from which to start the search
from - the lower bound on the searched objects, i.e. objects greater or equal are returned
to - the upper bound on the searched objects, i.e. objects smaller or equal are returned
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final ModifiableSearch<T> search(java.util.Collection<? extends K> keys)
                                 throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. All objects that are comparator-equal to any of the given keys are returned.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
keys - the keys to search for
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final <C> ModifiableSearch<T> search(IndexComparator<? super C,? super T> comparator,
                                            C key)
                                 throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for objects in this index that have any of the specified keys. The equality is checked exclusively by using the specified comparator, thus key need not necessarily be of the same class as the objects stored in this index and also consistency with equals is not required.

Note that objects are not necessarily returned in the order defined by the comparator

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Type Parameters:
C - the type of the key used by the search
Parameters:
comparator - compares the key with the stored objects
key - the key to search for
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final <C> ModifiableSearch<T> search(IndexComparator<? super C,? super T> comparator,
                                            C from,
                                            C to)
                                 throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for objects in this index that are within the specified key-range. The key boundaries [from, to] need not necessarily be of the same class as the objects stored in this index, however, the comparator must be able to compare the boundaries and the internal objects.

Note that objects are not returned in the order defined by the comparator

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Type Parameters:
C - the type the boundaries used by the search
Parameters:
comparator - compares the boundaries [from, to] with the stored objects
from - the lower bound on the searched objects, i.e. objects greater or equal are returned
to - the upper bound on the searched objects, i.e. objects smaller or equal are returned
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public final <C> ModifiableSearch<T> search(IndexComparator<? super C,? super T> comparator,
                                            java.util.Collection<? extends C> keys)
                                 throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for objects in this index that have any of the specified keys. The equality is checked exclusively by using the specified comparator, thus key need not necessarily be of the same class as the objects stored in this index and also consistency with equals is not required.

Note that objects are not necessarily returned in the order defined by the comparator

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Type Parameters:
C - the type of the keys used by the search
Parameters:
comparator - compares the keys with the stored objects
keys - the keys to search for (at least one key must be given)
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index