de.enough.polish.content.storage
Class StorageIndex

java.lang.Object
  extended by de.enough.polish.content.storage.StorageIndex
All Implemented Interfaces:
Comparator
Direct Known Subclasses:
RmsStorageIndex

public class StorageIndex
extends Object
implements Comparator

Serves as an index for stored contents

Author:
Andre Schmidt

Field Summary
protected  ArrayList index
          the index list
protected  long maxCacheSize
          the maximum cache size
 
Constructor Summary
StorageIndex(long maxCacheSize)
          Creates a new StorageIndex instance
 
Method Summary
 void addReference(StorageReference reference)
          Adds a reference to the index and stores the index
 void applyOrder()
          Applies the clean order to this index by the use of the internal index array and the compareTo() method
 int compare(Object first, Object second)
          Compares two objects.
 long getAvailableCacheSize()
          Returns the maximum possible number of bytes that can be stored.
 long getCacheSize()
          Returns the current cache size
 int getDisposableIndex()
          Returns the index of the first disposable content.
 StorageReference getReference(ContentDescriptor descriptor)
          Returns the reference for the specified descriptor if it is present in the index
 StorageReference getReference(int i)
          Returns the reference at position i
 boolean isCleanNeeded()
          Returns true, if the current cache size is greater than the threshold, otherwise false
 boolean isCleanNeeded(int extraBytes)
          Returns true, if the current cache size is not enough to store an additional extraBytes bytes, otherwise false
 boolean isDisposableTo(StorageReference reference, StorageReference master)
          Returns true, if the reference is disposable to the specified master, otherwise false
 boolean isPrepared()
          Returns true, if the index is prepared, otherwise false
protected  ArrayList load()
          Loads the ArrayList representing the index.
 void prepare()
          Prepares this index by calling load and adding the loaded references to the index
 void removeReference(StorageReference reference)
          Removes a reference from the index and stores the index
 void shutdown()
          Called when a content storage is shutdown.
 int size()
          Returns the number of the references in this index
protected  void store(ArrayList index)
          Stores the ArrayList representing the index.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

index

protected ArrayList index
the index list


maxCacheSize

protected final long maxCacheSize
the maximum cache size

Constructor Detail

StorageIndex

public StorageIndex(long maxCacheSize)
Creates a new StorageIndex instance

Parameters:
maxCacheSize - the maximum cache size for this instances
Method Detail

prepare

public void prepare()
Prepares this index by calling load and adding the loaded references to the index


isPrepared

public boolean isPrepared()
Returns true, if the index is prepared, otherwise false

Returns:
true, if the index is prepared, otherwise false

addReference

public void addReference(StorageReference reference)
Adds a reference to the index and stores the index

Parameters:
reference - the reference

removeReference

public void removeReference(StorageReference reference)
Removes a reference from the index and stores the index

Parameters:
reference - the reference

getDisposableIndex

public int getDisposableIndex()
Returns the index of the first disposable content.

Returns:
the index of the first disposable content.

getReference

public StorageReference getReference(int i)
Returns the reference at position i

Parameters:
i - the position
Returns:
the reference at the specified position

size

public int size()
Returns the number of the references in this index

Returns:
the number of the references in this index

getCacheSize

public long getCacheSize()
Returns the current cache size

Returns:
the current cache size

getAvailableCacheSize

public long getAvailableCacheSize()
Returns the maximum possible number of bytes that can be stored. This takes into consideration both user-specified limitations (e.g. maximum cache size) and platform-specific limitations (e.g. storage space).

Returns:
the maximum possible number of bytes that can be stored in the cache.

load

protected ArrayList load()
Loads the ArrayList representing the index. It is encouraged to overwrite this method for persistence of the index.

Returns:
the ArrayList representing the index

store

protected void store(ArrayList index)
Stores the ArrayList representing the index. It is encouraged to overwrite this method for persistence of the index.

Parameters:
index - the ArrayList representing the index

getReference

public StorageReference getReference(ContentDescriptor descriptor)
Returns the reference for the specified descriptor if it is present in the index

Parameters:
descriptor - the content descriptor
Returns:
the found reference or null

isCleanNeeded

public boolean isCleanNeeded()
Returns true, if the current cache size is greater than the threshold, otherwise false

Returns:
true, if the current cache size is greater than the threshold, otherwise false

isCleanNeeded

public boolean isCleanNeeded(int extraBytes)
Returns true, if the current cache size is not enough to store an additional extraBytes bytes, otherwise false

Parameters:
extraBytes - the number of extra bytes to store
Returns:
true, if the current cache size is greater than the threshold, otherwise false

compare

public int compare(Object first,
                   Object second)
Description copied from interface: Comparator
Compares two objects. In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive. The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.) The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0. Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z. It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."

Specified by:
compare in interface Comparator
Parameters:
first - the first object
second - the object that is compared with o1
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

isDisposableTo

public boolean isDisposableTo(StorageReference reference,
                              StorageReference master)
Returns true, if the reference is disposable to the specified master, otherwise false

Parameters:
reference - the reference
master - the master
Returns:
true, if the reference is disposable to the specified master, otherwise false

applyOrder

public void applyOrder()
Applies the clean order to this index by the use of the internal index array and the compareTo() method


shutdown

public void shutdown()
Called when a content storage is shutdown. Overwrite this to implements shutdown behaviour.


toString

public String toString()
Overrides:
toString in class Object