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

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

public class IntStorageIndex<K,T>
extends AbstractArrayIndex<K,T>
implements java.io.Serializable

Implementation of a single index over a storage with integer addresses. The addresses provided by the storage are kept in internal sorted array that allows fast access to data in the storage. Objects are indexed according to the given IndexComparator.

See Also:
Serialized Form

Constructor Summary
IntStorageIndex(IntStorage<T> storage, IndexComparator<K,T> comparator)
          Creates a new instance of IntStorageIndex for the specified storage.
 
Method Summary
protected  Lock acquireSearchLock()
          Locks this index for searching and returns a lock object if it is supported.
 boolean add(T object)
          Adds the specified object to this instance.
 IndexComparator<K,T> comparator()
          Returns the comparator that defines order of this index.
protected  int compare(K key, T object)
          Compares its two arguments for order.
 void destroy()
          Destroy this index.
 void finalize()
          Finalize this index.
protected  T get(int i)
          Returns the element at the specified position in this collection.
protected  int insertionPoint(T object)
          Searches for the point where to insert the object object.
protected  boolean remove(int i)
          Removes the element at the specified position in this collection.
 int size()
          Returns the number of elements in this collection.
 
Methods inherited from class messif.buckets.index.impl.AbstractArrayIndex
search, search, search, search, search, search, search, search
 
Methods inherited from class messif.utility.SortedArrayData
binarySearch, first, fullSearch, indexOf, last, mergeSort
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IntStorageIndex

public IntStorageIndex(IntStorage<T> storage,
                       IndexComparator<K,T> comparator)
Creates a new instance of IntStorageIndex for the specified storage.

Parameters:
storage - the storage to associate with this index
comparator - the comparator imposing natural order of this index
Method Detail

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 AbstractArrayIndex<K,T>
Throws:
java.lang.Throwable - if there was an error while cleaning

destroy

public void destroy()
             throws java.lang.Throwable
Description copied from interface: ModifiableIndex
Destroy this index. This method releases all resources (transient and persistent) associated with this index. After this method is called, the store and retrieval methods' behavior is unpredictable.

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

comparator

public IndexComparator<K,T> comparator()
Description copied from interface: OrderedIndex
Returns the comparator that defines order of this index.

Specified by:
comparator in interface OrderedIndex<K,T>
Returns:
the comparator that defines order of this index

compare

protected int compare(K key,
                      T object)
               throws java.lang.ClassCastException
Description copied from class: SortedArrayData
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Specified by:
compare in class SortedArrayData<K,T>
Parameters:
key - the key to indexCompare
object - the object to be compared
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
java.lang.ClassCastException - if the arguments' types prevent them from being compared by this comparator.

size

public int size()
Description copied from class: SortedArrayData
Returns the number of elements in this collection.

Specified by:
size in interface Index<T>
Specified by:
size in class SortedArrayData<K,T>
Returns:
the number of elements in this collection

insertionPoint

protected int insertionPoint(T object)
                      throws BucketStorageException
Searches for the point where to insert the object object.

Parameters:
object - the object to insert
Returns:
the point in the array where to put the object
Throws:
BucketStorageException - if there was a problem determining the point

add

public boolean add(T object)
            throws BucketStorageException
Description copied from interface: Addible
Adds the specified object to this instance.

Specified by:
add in interface Addible<T>
Parameters:
object - the object to be added
Returns:
true if the addition was successful
Throws:
BucketStorageException - if there was an error adding the object

remove

protected boolean remove(int i)
Description copied from class: AbstractArrayIndex
Removes the element at the specified position in this collection.

Specified by:
remove in class AbstractArrayIndex<K,T>
Parameters:
i - index of the element to remove
Returns:
false if the object was not removed (e.g. because there is no object with this index)

get

protected T get(int i)
         throws java.lang.IndexOutOfBoundsException,
                java.lang.IllegalStateException
Description copied from class: SortedArrayData
Returns the element at the specified position in this collection.

Specified by:
get in class SortedArrayData<K,T>
Parameters:
i - index of the element to return
Returns:
the element at the specified position in this collection
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
java.lang.IllegalStateException - if the object at position index cannot be accessed

acquireSearchLock

protected Lock acquireSearchLock()
Description copied from class: AbstractArrayIndex
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.

Specified by:
acquireSearchLock in class AbstractArrayIndex<K,T>
Returns:
a lock on this index or null