anthill.nestor
Class LocalStorage

java.lang.Object
  |
  +--anthill.nestor.LocalStorage
All Implemented Interfaces:
Clearable, Storage

public class LocalStorage
extends java.lang.Object
implements Storage

LocalStorage is a simple implementation of the Storage interface, aimed at serving as resource repository for nests during the off-line simulation. Each LocalStorage is characterized by its capacity, i.e. by the number of documents that can be simultaneously maintained in it. Due to the fact that the capacity is limited, resources are stored following a last-recently-used (LRU) policy.

This data structure is designed for single-threaded off-line simulation, i.e. its methods are not synchronized.


Field Summary
protected  int capacity
          Current capacity (i.e., number of resources) of this storage
protected  java.util.LinkedList list
          LRU queue
protected  java.util.HashMap map
          Hash map for obtaining resources
protected  int size
          Current size (i.e., number of resources currently stored in this storage)
 
Constructor Summary
LocalStorage(int capacity)
          Builds a new empty storage, with the given capacity.
 
Method Summary
 void addAntResource(Resource resource)
          Add a resource to the storage.
 void addClientResource(Resource resource)
          Add a resource to the storage.
 void clear()
          Restores the object to its initial state.
 Resource[] getResources(java.lang.Object request)
          Returns the Resource objects which satisfy the specified request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

size

protected int size
Current size (i.e., number of resources currently stored in this storage)

capacity

protected int capacity
Current capacity (i.e., number of resources) of this storage

list

protected java.util.LinkedList list
LRU queue

map

protected java.util.HashMap map
Hash map for obtaining resources
Constructor Detail

LocalStorage

public LocalStorage(int capacity)
Builds a new empty storage, with the given capacity.
Parameters:
capacity - the number of resources which can be stored in this object
Method Detail

getResources

public Resource[] getResources(java.lang.Object request)
Returns the Resource objects which satisfy the specified request. This implementations assumes that a request for a documents is constituted by a string containing the object itself. If the resource is present, it is moved to the first position of the LRU queue.
Specified by:
getResources in interface Storage
Parameters:
request - the request to be satisfied
Returns:
the found resources, or null if no resource may be found

addAntResource

public void addAntResource(Resource resource)
Add a resource to the storage. This method is invoked from a nest on behalf of an ant. If the resource is already present in the storage, it is moved to the first position of the LRU queue. If the resource is not present, it is stored in the storage and it is placed in the first position of the LRU queue. In both cases, the storage verifies that the number of resources in the system does not exceed its current capacity; if so, resources in the last position of the LRU queue are removed from the storage.
Specified by:
addAntResource in interface Storage
Parameters:
resource - the resource to be added

addClientResource

public void addClientResource(Resource resource)
Add a resource to the storage. This method is invoked from a nest on behalf of an user. In this simple implementation, the semantics of an insertion requested by an user is equal to the semantics of an insertion requested by an ant.
Specified by:
addClientResource in interface Storage
Parameters:
resource - the resource to be added

clear

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