org.entityfs.util
Class AggregatingEntityRepository

java.lang.Object
  extended by org.entityfs.util.AggregatingEntityRepository
All Implemented Interfaces:
Iterable<EntityView>, EntityHolder, EntityListable, EntityRepository, Lockable, ReadLockable

public class AggregatingEntityRepository
extends Object
implements EntityRepository

This is an EntityRepository that aggregates the contents of several entity repositories. It can be used to make several directories look as one.

This object keeps an internal list of all repositories supplied to the constructor. When the client requests an entity, all repositories in the list are searched until the entity is found.

The read lock of this object is a CompoundEntityLock over all aggregated repositories. This object does not have any write lock since it is read only.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
AggregatingEntityRepository(Collection<? extends EntityRepository> c)
          Create a new object.
 
Method Summary
 boolean containsEntity(EntityView ev)
          Does this entity holder contain the supplied entity? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains the entity.
 boolean containsEntity(String name)
          Can an entity with the given name be retrieved from this object? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains an entity with the given name.
 EntityView getEntityOrNull(String name)
          Get the named entity.
 EntityLock getReadLock()
          Get the read lock without locking it.
 boolean isEmpty()
          Is the listable empty? If a directory view is empty, the viewed directory may still contain entities that are hidden by the view's filter.
 boolean isReadLockedByCurrentThread()
          Does the calling thread hold a read lock for this object? A write lock also qualifies as a read lock.
 Iterator<EntityView> iterator()
           
 Set<EntityView> listEntities()
          Return all entities that are visible in this directory.
 EntityLock lockForReading()
          Lock object for reading and return the lock.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AggregatingEntityRepository

public AggregatingEntityRepository(Collection<? extends EntityRepository> c)
Create a new object.

Parameters:
c - A collection of repositories. The repositories are added to the internal list of repositories in the order that they are returned when iterating over the collection.
Method Detail

getReadLock

public EntityLock getReadLock()
Description copied from interface: ReadLockable
Get the read lock without locking it.

This method always returns a lock, even if the object's file system is not locking or if the object is read only.

Specified by:
getReadLock in interface ReadLockable
Returns:
The lockable's read lock. This might be unlocked or locked by someone else.

lockForReading

public EntityLock lockForReading()
Description copied from interface: ReadLockable
Lock object for reading and return the lock. How the lock is acquired is determined by the EntityLockAdapterFactory and LockAcquiringStrategy used. If this is called on an object that somehow is connected to an entity object, that entity's file system's entity lock adapter factory and lock acquiring strategy is used.

A read lock cannot be upgraded to a write lock (just like how Java's ReadWriteLock works).

For more discussions on locks and locking, see Entity and FileSystem.

This method always returns a lock, even if the object's file system is not locking or if the object is read only.

Specified by:
lockForReading in interface ReadLockable
Returns:
A locked read lock.

isReadLockedByCurrentThread

public boolean isReadLockedByCurrentThread()
Description copied from interface: ReadLockable
Does the calling thread hold a read lock for this object? A write lock also qualifies as a read lock.

Specified by:
isReadLockedByCurrentThread in interface ReadLockable
Returns:
true if the lockable object is read or write locked by the current thread.

isEmpty

public boolean isEmpty()
Description copied from interface: EntityListable
Is the listable empty? If a directory view is empty, the viewed directory may still contain entities that are hidden by the view's filter.

Specified by:
isEmpty in interface EntityListable
Returns:
true if the directory view is empty.

containsEntity

public boolean containsEntity(String name)
Description copied from interface: EntityHolder
Can an entity with the given name be retrieved from this object? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains an entity with the given name.

Specified by:
containsEntity in interface EntityHolder
Parameters:
name - The name of the entity.
Returns:
true if there is an entity with the given name referenced by this holder.

containsEntity

public boolean containsEntity(EntityView ev)
Description copied from interface: EntityHolder
Does this entity holder contain the supplied entity? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains the entity.

Specified by:
containsEntity in interface EntityHolder
Parameters:
ev - The entity (or a view of it).
Returns:
true If the entity holder contains the supplied entity.

listEntities

public Set<EntityView> listEntities()
Description copied from interface: EntityListable
Return all entities that are visible in this directory. The result is not sorted in any way.

If this is called on a directory view, all returned ViewCapable entities inherit the view's filters.

Specified by:
listEntities in interface EntityListable
Returns:
A Set of entity views. If this is called on a directory entity object, a set of entity objects (not views) are returned. The set may be empty, but never null.

getEntityOrNull

public EntityView getEntityOrNull(String name)
Description copied from interface: EntityHolder
Get the named entity. For directories, this is an entity in the directory. For a symbolic link pointing to a directory, this is an entity in the target directory.

If this is called on a directory view, and the returned entity is ViewCapable, the returned entity view inherits the same view settings as the directory view (same filter instances).

If this is called on an EntityHolder that contains several entities with the same name, the implementation may choose how to deal with that. This can never happen for directories since entity names in a directory are required to be unique.

Specified by:
getEntityOrNull in interface EntityHolder
Parameters:
name - The name of the entity.
Returns:
An EntityView or null. If this method is called on a DirectoryView and the returned entity is ViewCapable , the returned object inherits the view settings of the parent. If the returned entity is a file, the returned object can be cast to an EFile, if it is a directory, it can be cast to a DirectoryView (or a Directory if this method is called on a Directory rather than a DirectoryView). The type of the returned object can be determined by calling its EntityView.getType() method or by using instanceof.

iterator

public Iterator<EntityView> iterator()
Specified by:
iterator in interface Iterable<EntityView>