org.scijava.object
Class ObjectIndex<E>

java.lang.Object
  extended by org.scijava.object.ObjectIndex<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>
Direct Known Subclasses:
SortedObjectIndex

public class ObjectIndex<E>
extends Object
implements Collection<E>

Data structure for managing lists of registered objects.

The object index keeps lists of objects segregated by type. The type hierarchy beneath which each object is classified can be customized through subclassing (e.g., see PluginIndex), but by default, each registered object is added to all type lists with which its class is compatible. For example, an object of type String would be added to the following type lists: String, Serializable, Comparable, CharSequence and Object. A subsequent request for all objects of type Comparable (via a call to get(Class)) would return a list that includes the object.

Note that similar to List, it is possible for the same object to be added to the index more than once, in which case it will appear on relevant type lists multiple times.

Note that similar to List, it is possible for the same object to be added to the index more than once, in which case it will appear on compatible type lists multiple times.

Author:
Curtis Rueden

Field Summary
protected  Map<Class<?>,List<E>> hoard
          "Them as counts counts moren them as dont count."
—Russell Hoban, Riddley Walker
 
Constructor Summary
ObjectIndex(Class<E> baseClass)
           
 
Method Summary
 boolean add(E o)
           
protected  boolean add(E o, boolean batch)
          Adds the object to all compatible type lists.
protected  boolean add(E o, Class<?> type, boolean batch)
          Adds an object to type lists beneath the given type hierarchy.
 boolean addAll(Collection<? extends E> c)
           
 void addLater(LazyObjects<? extends E> c)
          Registers objects which will be created lazily as needed.
protected  boolean addToList(E obj, List<E> list, boolean batch)
           
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 List<E> get(Class<?> type)
          Gets a list of registered objects compatible with the given type.
 List<E> getAll()
          Gets a list of all registered objects.
 Class<E> getBaseClass()
          Gets the base class of the items being managed.
protected  Class<?> getType(E o)
          Return the type by which to index the object.
protected static Class<?>[] getTypes(Class<?> type)
          Gets a new set containing the type and all its supertypes.
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 boolean remove(Object o)
           
protected  boolean remove(Object o, boolean batch)
          Removes the object from all compatible type lists.
protected  boolean remove(Object o, Class<?> type, boolean batch)
          Removes an object from type lists beneath the given type hierarchy.
 boolean removeAll(Collection<?> c)
           
protected  boolean removeFromList(Object obj, List<E> list, boolean batch)
           
 boolean retainAll(Collection<?> c)
           
protected  List<E> retrieveList(Class<?> type)
          Retrieves the type list for the given type, creating it if necessary.
protected  List<E>[] retrieveListsForType(Class<?> type)
           
 int size()
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

hoard

protected final Map<Class<?>,List<E>> hoard
"Them as counts counts moren them as dont count."
—Russell Hoban, Riddley Walker

Constructor Detail

ObjectIndex

public ObjectIndex(Class<E> baseClass)
Method Detail

getBaseClass

public Class<E> getBaseClass()
Gets the base class of the items being managed.


getAll

public List<E> getAll()
Gets a list of all registered objects.

Returns:
Read-only list of all registered objects, or an empty list if none (this method never returns null).

get

public List<E> get(Class<?> type)
Gets a list of registered objects compatible with the given type.

Returns:
New list of registered objects of the given type, or an empty list if no such objects exist (this method never returns null).

addLater

public void addLater(LazyObjects<? extends E> c)
Registers objects which will be created lazily as needed.

This is useful if creation of the objects is expensive for some reason. In that case, the object index can wait to actually request and register the objects until the next accessor method invocation (i.e., get(Class) or getAll()).


size

public int size()
Specified by:
size in interface Collection<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<E>

iterator

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

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<E>

add

public boolean add(E o)
Specified by:
add in interface Collection<E>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<E>

containsAll

public boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<E>

addAll

public boolean addAll(Collection<? extends E> c)
Specified by:
addAll in interface Collection<E>

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<E>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>

toString

public String toString()
Overrides:
toString in class Object

add

protected boolean add(E o,
                      boolean batch)
Adds the object to all compatible type lists.


getType

protected Class<?> getType(E o)
Return the type by which to index the object.


remove

protected boolean remove(Object o,
                         boolean batch)
Removes the object from all compatible type lists.


retrieveListsForType

protected List<E>[] retrieveListsForType(Class<?> type)

add

protected boolean add(E o,
                      Class<?> type,
                      boolean batch)
Adds an object to type lists beneath the given type hierarchy.


remove

protected boolean remove(Object o,
                         Class<?> type,
                         boolean batch)
Removes an object from type lists beneath the given type hierarchy.


addToList

protected boolean addToList(E obj,
                            List<E> list,
                            boolean batch)

removeFromList

protected boolean removeFromList(Object obj,
                                 List<E> list,
                                 boolean batch)

getTypes

protected static Class<?>[] getTypes(Class<?> type)
Gets a new set containing the type and all its supertypes.


retrieveList

protected List<E> retrieveList(Class<?> type)
Retrieves the type list for the given type, creating it if necessary.



Copyright © 2009–2015 SciJava. All rights reserved.