Class ClassIndexedSet<T>

  • Type Parameters:
    T - the type of object stored by this class
    All Implemented Interfaces:
    Iterable<T>, Collection<T>, Set<T>
    Direct Known Subclasses:
    CriteriaSet

    public class ClassIndexedSet<T>
    extends AbstractSet<T>
    implements Set<T>
    Set implementation which provides indexed access to set members via their class, and which allows only one instance of a given class to be present in the set. Null members are not allowed.
    • Field Detail

      • set

        private final HashSet<T> set
        Storage for set members.
      • index

        private final HashMap<Class<? extends T>,​T> index
        Storage for index of class -> member.
    • Constructor Detail

      • ClassIndexedSet

        public ClassIndexedSet()
        Constructor.
    • Method Detail

      • add

        public boolean add​(@Nonnull
                           T o,
                           boolean replace)
        Add member to set, optionally replacing any existing instance of the same class.
        Parameters:
        o - the object to add
        replace - flag indicating whether to replace an existing class type
        Returns:
        true if object was added
      • contains

        public boolean contains​(@Nullable
                                Class<? extends T> clazz)
        Check whether set contains an instance of the specified class.
        Parameters:
        clazz - the class to check
        Returns:
        true if set contains an instance of the specified class, false otherwise
      • get

        @Nullable
        public <X extends T> X get​(@Nullable
                                   Class<X> clazz)
        Get the set element specified by the class parameter.
        Type Parameters:
        X - generic parameter which eliminates need for casting by the caller
        Parameters:
        clazz - the class to whose instance is to be retrieved
        Returns:
        the element whose class is of the type specified
      • getIndexClass

        @Nonnull
        protected <X extends TClass<X> getIndexClass​(@Nonnull
                                                       X o)
        Get the index class of the specified object. Subclasses may override to use a class index other than the main runtime class of the object.
        Type Parameters:
        X - generic parameter which eliminates need for casting by the caller
        Parameters:
        o - the object whose class index to determine
        Returns:
        the class index value associated with the object instance
      • removeFromIndex

        private void removeFromIndex​(T o)
        Remove the specified object from the index.
        Parameters:
        o - the object to remove