Class ClassToInstanceMultiMap<B>

  • Type Parameters:
    B - a bound for the types of values in the map
    Direct Known Subclasses:
    LockableClassToInstanceMultiMap

    @NotThreadSafe
    public class ClassToInstanceMultiMap<B>
    extends Object
    An append-only multimap where each entry associates a raw type (i.e. class) to instances of that class. In addition the map may also associate any supertype (i.e. all superclasses and interfaces implemented by the class) with the class. Null values are not supported.
    • Field Detail

      • indexSupertypes

        private final boolean indexSupertypes
        Whether supertypes should also be indexed.
      • backingMap

        @Nonnull
        private final HashMap<Class<?>,​List<B>> backingMap
        Map which backs this map.
    • Constructor Detail

      • ClassToInstanceMultiMap

        public ClassToInstanceMultiMap()
        Constructor. Does not index supertypes.
      • ClassToInstanceMultiMap

        public ClassToInstanceMultiMap​(boolean isIndexingSupertypes)
        Constructor.
        Parameters:
        isIndexingSupertypes - indicates whether supertypes of a value should be indexed
    • Method Detail

      • clear

        public void clear()
        Removes all mappings from this map.
      • containsKey

        public boolean containsKey​(@Nullable
                                   Class<?> key)
        Returns true if the map contains a mapping for the given key.
        Parameters:
        key - key to check for in the map
        Returns:
        true if the map contains a mapping for the specified key
      • containsValue

        public boolean containsValue​(@Nullable
                                     B value)
        Returns true iff the map contains a mapping to the given value, false if value is null.
        Parameters:
        value - value to check for in this map
        Returns:
        true iff the map contains a mapping to the specified value
      • get

        @Nonnull
        @NonnullElements
        @Unmodifiable
        @Live
        public <T> List<T> get​(@Nullable
                               Class<T> type)
        Gets the instances mapped to the given type or an empty list.
        Type Parameters:
        T - type identifier
        Parameters:
        type - map key
        Returns:
        instances mapped to the given type or an empty list
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no entries, false otherwise.
        Returns:
        true if this map contains no entries, false otherwise
      • keys

        @Nonnull
        @NonnullElements
        @Unmodifiable
        @Live
        public Set<Class<?>> keys()
        Gets the set of keys contained in this map. The set is backed by the map so changes made to the map are reflected in the set. However the set does not allow direct modification, any changes need to be done through this map.
        Returns:
        set of keys contained in this map
      • put

        public void put​(@Nonnull
                        B value)
        Adds a value to this map. If indexSupertypes is false only the values class type is used as a key to the value. If indexSupertypes is true, then the class types, all its supertypes, and all implemented interfaces are used as keys to the value. Duplicate values, as determined by the values Object.hashCode() and Object.equals(Object) methods, are not stored. Only one instance of the value is ever stored in the map.
        Parameters:
        value - value to be stored in the map
      • putAll

        public void putAll​(@Nullable @NonnullElements
                           Iterable<? extends B> newValues)
        Adds all the values to the map.
        Parameters:
        newValues - values to be added
        See Also:
        put(Object)
      • putAll

        public void putAll​(@Nullable @NonnullElements
                           ClassToInstanceMultiMap<? extends B> map)
        Adds all the values to the map. This operations operates only on the given map's value collection. Therefore, regardless of the given map's policy on indexing by value supertypes, this map will index values based on its policy.
        Parameters:
        map - map containing values to be added
        See Also:
        put(Object)
      • remove

        public void remove​(@Nullable
                           B value)
        Remove the specified value from the map and from the value list of all indexes.

        If the value list for a type index becomes empty due to the value removal, the entire type index will be removed and containsKey(Class) for that type will then return false.

        Parameters:
        value - the value to remove
      • removeAll

        public void removeAll​(@Nullable @NonnullElements
                              Iterable<? extends B> removeValues)
        Remove the specified values from the map and from the value list of all indexes.

        If the value list for a type index becomes empty due to a value removal, the entire type index will be removed and containsKey(Class) for that type will then return false.

        Parameters:
        removeValues - the values to remove
      • removeAll

        public void removeAll​(@Nullable @NonnullElements
                              ClassToInstanceMultiMap<? extends B> map)
        Remove the values contained in the specified map from this map and from the value list of all indexes.

        If the value list for a type index becomes empty due to a value removal, the entire type index will be removed and containsKey(Class) for that type will then return false.

        Parameters:
        map - the map containing the values to remove
      • remove

        public void remove​(@Nullable
                           Class<?> type)
        Remove from the map all values which have the specified type.

        Note that when a value was indexed by multiple superclass and/or interface types, it will be removed from all those type indexes, not just the specified one.

        If the value list for a type index becomes empty due to a value removal, the entire type index will be removed and containsKey(Class) for that type will then return false.

        Parameters:
        type - the type of values to remove
      • values

        @Nonnull
        @NonnullElements
        @Unmodifiable
        @Live
        public Collection<? extends B> values()
        The collection of values currently present in the map. This collection is backed by the map so changes to the map will be reflected in the collection. However the collection does not allow direct modification so any changes must be done through this map.
        Returns:
        collection of values currently present in the map
      • getIndexTypes

        @Nonnull
        @NonnullElements
        private Set<Class<?>> getIndexTypes​(@Nonnull
                                            B value)
        Get the effective set of all class types via which the specified value should be indexed.
        Parameters:
        value - the value to index
        Returns:
        the set of classes by which to index the value
      • getSuperTypes

        private void getSuperTypes​(@Nonnull
                                   Class<?> clazz,
                                   @Nonnull @NonnullElements
                                   Set<Class<?>> accumulator)
        Gets all of the superclasses and interfaces implemented by the given class.
        Parameters:
        clazz - class for which supertypes will be determined
        accumulator - collection to which supertypes are added as they are determined
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object