Class FlyweightSet<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

    public class FlyweightSet<E>
    extends java.util.AbstractSet<E>
    A thread-safe Set that manages flyweights: sharable objects that are usually immutable. Call the get method for supplying the FlyweightSet with candidate flyweight instances.

    Objects that do not customize the hashCode and equals methods don't make sense to use as flyweights because each instance will be considered unique. The object returned from the get method will always be the same as the one passed in.

    • Constructor Summary

      Constructors 
      Constructor Description
      FlyweightSet()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(java.lang.Object obj)  
      protected boolean equals​(E a, E b)  
      protected int hashCode​(E obj)  
      java.util.Iterator iterator()  
      E put​(E obj)
      Pass in a candidate flyweight object and get a unique instance from this set.
      int size()  
      java.lang.String toString()  
      • Methods inherited from class java.util.AbstractSet

        equals, hashCode, removeAll
      • Methods inherited from class java.util.AbstractCollection

        add, addAll, clear, containsAll, isEmpty, remove, retainAll, toArray, toArray
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        add, addAll, clear, containsAll, isEmpty, remove, retainAll, spliterator, toArray, toArray
    • Constructor Detail

      • FlyweightSet

        public FlyweightSet()
    • Method Detail

      • put

        public E put​(E obj)
        Pass in a candidate flyweight object and get a unique instance from this set. The returned object will always be of the same type as that passed in. If the object passed in does not equal any object currently in the set, it will be added to the set, becoming a flyweight.
        Parameters:
        obj - candidate flyweight; null is also accepted
      • iterator

        public java.util.Iterator iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.Set<E>
        Specified by:
        iterator in class java.util.AbstractCollection<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.Set<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
      • contains

        public boolean contains​(java.lang.Object obj)
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface java.util.Set<E>
        Overrides:
        contains in class java.util.AbstractCollection<E>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractCollection<E>
      • hashCode

        protected int hashCode​(E obj)
      • equals

        protected boolean equals​(E a,
                                 E b)