anthill.storage.impl
Class MemoryStorage

java.lang.Object
  |
  +--anthill.storage.impl.MemoryStorage
All Implemented Interfaces:
Clearable, GenericStorage, Storage

public class MemoryStorage
extends java.lang.Object
implements GenericStorage

The MemoryStorage is a simple implementation of the generic storage which keeps resources in memory.

Implementation note: currently, no check on the actual memory occupation of an object is performed; this implementation use the size() field of the object to obtain the length of the object. Future implementations for real deployment will include this additional check.

Version:
$Revision$
Author:
Alberto Montresor

Fields inherited from interface anthill.storage.Storage
DOC_STORAGE, PHR_STORAGE, URL_STORAGE
 
Constructor Summary
MemoryStorage(Factory factory, int capacity)
          Constructor for a memory capacity object
 
Method Summary
 void addResource(java.lang.Object key, Resource resource)
          Adds a resource to the storage and associates it to the specified key.
 void clear()
          Restores the object to its initial state.
 boolean containsResource(java.lang.Object key)
          Returns true if the resource associated to key is contained in the storage, false otherwise.
 long getCapacity()
          Returns the initial capacity of this generic storage.
 long getRemainingCapacity()
          Returns the remaining capacity of this generic storage.
 Resource getResource(java.lang.Object key)
          Returns the resource identified by the given key.
 java.util.Iterator iterator()
          Returns the complete set of resources stored in this storage object using an iterator.
 void removeResource(java.lang.Object key)
          Removes a resource from the storage.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MemoryStorage

public MemoryStorage(Factory factory,
                     int capacity)
Constructor for a memory capacity object
Parameters:
factory - factory object used to instantiate objects and obtaining configuration values
capacity - initial capacity
Method Detail

clear

public void clear()
Description copied from interface: Clearable
Restores the object to its initial state.
Specified by:
clear in interface Clearable

containsResource

public boolean containsResource(java.lang.Object key)
Returns true if the resource associated to key is contained in the storage, false otherwise.
Specified by:
containsResource in interface GenericStorage
Parameters:
key - the key to be searched
Returns:
true if the resource associated to key is contained in the storage

getResource

public Resource getResource(java.lang.Object key)
Returns the resource identified by the given key. If the key is not associated with any resource, a null value is returned.
Specified by:
getResource in interface GenericStorage
Following copied from interface: anthill.storage.GenericStorage
Parameters:
key - whose associated resource to retreive.
Returns:
resource with which the specified key is associated.

iterator

public java.util.Iterator iterator()
Returns the complete set of resources stored in this storage object using an iterator.
Specified by:
iterator in interface GenericStorage
Following copied from interface: anthill.storage.GenericStorage
Returns:
iterator representing this storage object, allowing to inspect its content.

addResource

public void addResource(java.lang.Object key,
                        Resource resource)
                 throws CapacityException
Adds a resource to the storage and associates it to the specified key. If the remaining capacity of the storage is not sufficient to store the resource, a CapacityException is thrown containing information about the remaining capacity. Method size() may be invoked on the resource to determine the amount of space required by the resource. In order to be stored on persistent storage, resource must implement the Serializable interface.
Specified by:
addResource in interface GenericStorage
Parameters:
key - the key identifying the resource
resource - the resource to be added

removeResource

public void removeResource(java.lang.Object key)
Removes a resource from the storage.
Specified by:
removeResource in interface GenericStorage
Parameters:
key - the identifier of the resource to be removed

getCapacity

public long getCapacity()
Returns the initial capacity of this generic storage.
Specified by:
getCapacity in interface GenericStorage

getRemainingCapacity

public long getRemainingCapacity()
Returns the remaining capacity of this generic storage.
Specified by:
getRemainingCapacity in interface GenericStorage