Class ObjectIndex<E>

  • All Implemented Interfaces:
    Iterable<E>, Collection<E>
    Direct Known Subclasses:
    NamedObjectIndex, 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 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()).

      • 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>
      • 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.