messif.buckets.storage.impl
Class MemoryStorage<T>

java.lang.Object
  extended by messif.buckets.storage.impl.MemoryStorage<T>
Type Parameters:
T - the class of objects stored in this storage
All Implemented Interfaces:
java.io.Serializable, Addible<T>, Index<T>, ModifiableIndex<T>, IntStorage<T>, IntStorageIndexed<T>, Storage<T>, StorageIndexed<T>

public class MemoryStorage<T>
extends java.lang.Object
implements IntStorageIndexed<T>, java.io.Serializable

Memory based storage. The objects in this storage are stored in an internal array in the order of insertion. The address is the position within the internal array.

See Also:
Serialized Form

Constructor Summary
MemoryStorage(java.lang.Class<? extends T> storedObjectsClass)
          Constructs an empty memory storage.
MemoryStorage(java.lang.Class<? extends T> storedObjectsClass, int initialCapacity)
          Constructs an empty memory storage with the specified initial capacity.
 
Method Summary
 boolean add(T object)
          Adds the specified object to this instance.
static
<E> MemoryStorage<E>
castToMemoryStorage(java.lang.Class<E> storageObjectsClass, java.lang.Object object)
          Cast the provided object to MemoryStorage with generics typing.
static
<T> MemoryStorage<T>
create(java.lang.Class<T> storedObjectsClass, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Creates a new memory storage.
 void destroy()
          Destroy this index.
 void finalize()
          Finalize this index.
 java.lang.Class<? extends T> getStoredObjectsClass()
          Returns the class of objects that the this storage works with.
 boolean isEmpty()
          Returns true if this storage contains no elements.
 T read(int address)
          Reads the object stored at the specified address in this storage.
 void remove(int address)
          Removes the object stored at the specified address in this storage.
 IntStorageSearch<T> search()
          Returns a search for all objects in this index.
<C> IntStorageSearch<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> IntStorageSearch<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> IntStorageSearch<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.
 int size()
          Returns the number of elements in this storage.
 IntAddress<T> store(T object)
          Stores an object in this storage.
 java.lang.String toString()
          Returns a string representation of this storage.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MemoryStorage

public MemoryStorage(java.lang.Class<? extends T> storedObjectsClass,
                     int initialCapacity)
              throws java.lang.IllegalArgumentException
Constructs an empty memory storage with the specified initial capacity.

Parameters:
storedObjectsClass - the class of objects that the storage will work with
initialCapacity - the initial capacity of the storage
Throws:
java.lang.IllegalArgumentException - if the specified initial capacity is invalid

MemoryStorage

public MemoryStorage(java.lang.Class<? extends T> storedObjectsClass)
Constructs an empty memory storage. The initial capacity of the internal storage is set to 16 objects.

Parameters:
storedObjectsClass - the class of objects that the storage will work with
Throws:
java.lang.IllegalArgumentException - if the specified initial capacity is invalid
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>
Specified by:
finalize in interface Storage<T>
Overrides:
finalize in class java.lang.Object
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>
Specified by:
destroy in interface Storage<T>
Throws:
java.lang.Throwable - if there was an error while cleaning

create

public static <T> MemoryStorage<T> create(java.lang.Class<T> storedObjectsClass,
                                          java.util.Map<java.lang.String,java.lang.Object> parameters)
                               throws java.lang.IllegalArgumentException
Creates a new memory storage. The additional parameters are specified in the parameters map with the following recognized key names:

Type Parameters:
T - the class of objects that the new storage will work with
Parameters:
storedObjectsClass - the class of objects that the new storage will work with
parameters - list of named parameters (see above)
Returns:
a new memory storage instance
Throws:
java.lang.IllegalArgumentException - if the parameters specified are invalid (null values, etc.)

castToMemoryStorage

public static <E> MemoryStorage<E> castToMemoryStorage(java.lang.Class<E> storageObjectsClass,
                                                       java.lang.Object object)
                                            throws java.lang.ClassCastException
Cast the provided object to MemoryStorage with generics typing. The objects stored in the storage must be of the same type as the storageObjectsClass.

Type Parameters:
E - the class of objects stored in the storage
Parameters:
storageObjectsClass - the class of objects stored in the storage
object - the storage instance
Returns:
the generics-typed MemoryStorage object
Throws:
java.lang.ClassCastException - if passed object is not a MemoryStorage or the storage objects are incompatible

getStoredObjectsClass

public java.lang.Class<? extends T> getStoredObjectsClass()
Returns the class of objects that the this storage works with.

Returns:
the class of objects that the this storage works with

size

public int size()
Returns the number of elements in this storage.

Specified by:
size in interface Index<T>
Returns:
the number of elements in this storage

isEmpty

public boolean isEmpty()
Returns true if this storage contains no elements.

Returns:
true if this storage contains no elements

store

public IntAddress<T> store(T object)
Description copied from interface: Storage
Stores an object in this storage. The address returned by this call can be used to retrieve or remove the object.

Specified by:
store in interface IntStorage<T>
Specified by:
store in interface Storage<T>
Parameters:
object - the object to store
Returns:
the address where the object has been stored

read

public T read(int address)
       throws BucketStorageException
Description copied from interface: IntStorage
Reads the object stored at the specified address in this storage.

Specified by:
read in interface IntStorage<T>
Parameters:
address - the address of the object to read
Returns:
the object retrieved
Throws:
BucketStorageException - if there was an error reading the data

remove

public void remove(int address)
            throws BucketStorageException,
                   java.lang.UnsupportedOperationException
Description copied from interface: IntStorage
Removes the object stored at the specified address in this storage. This operation is optional and need not be implemented.

Specified by:
remove in interface IntStorage<T>
Parameters:
address - the address of the object to remove
Throws:
BucketStorageException - if there was an error deleting an object
java.lang.UnsupportedOperationException - if this storage does not support removal of objects

toString

public java.lang.String toString()
Returns a string representation of this storage. The string representation consists of a list of the stored objects in the order of insertion, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this storage

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

search

public IntStorageSearch<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>
Specified by:
search in interface IntStorageIndexed<T>
Specified by:
search in interface StorageIndexed<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 <C> IntStorageSearch<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>
Specified by:
search in interface IntStorageIndexed<T>
Specified by:
search in interface StorageIndexed<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 <C> IntStorageSearch<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>
Specified by:
search in interface IntStorageIndexed<T>
Specified by:
search in interface StorageIndexed<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

search

public <C> IntStorageSearch<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>
Specified by:
search in interface IntStorageIndexed<T>
Specified by:
search in interface StorageIndexed<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