Class UnifiedSet<T>

    • Field Detail

      • NULL_KEY

        protected static final java.lang.Object NULL_KEY
      • table

        protected transient java.lang.Object[] table
      • occupied

        protected transient int occupied
    • Constructor Detail

      • UnifiedSet

        public UnifiedSet()
      • UnifiedSet

        public UnifiedSet​(int initialCapacity)
      • UnifiedSet

        public UnifiedSet​(int initialCapacity,
                          float loadFactor)
      • UnifiedSet

        public UnifiedSet​(java.util.Collection<? extends T> collection)
    • Method Detail

      • newSet

        public static <K> UnifiedSet<K> newSet()
      • newSet

        public static <K> UnifiedSet<K> newSet​(int size)
      • newSet

        public static <K> UnifiedSet<K> newSet​(java.lang.Iterable<? extends K> source)
      • newSet

        public static <K> UnifiedSet<K> newSet​(int size,
                                               float loadFactor)
      • newSetWith

        public static <K> UnifiedSet<K> newSetWith​(K... elements)
      • fastCeil

        private int fastCeil​(float v)
      • index

        protected int index​(java.lang.Object key)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface Pool<T>
        Specified by:
        clear in interface java.util.Set<T>
      • add

        public boolean add​(T key)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.Set<T>
        Overrides:
        add in class AbstractMutableCollection<T>
      • chainedAdd

        private boolean chainedAdd​(T key,
                                   int index)
      • contains

        public boolean contains​(java.lang.Object key)
        Description copied from interface: RichIterable
        Returns true if the iterable has an element which responds true to element.equals(object).
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface RichIterable<T>
        Specified by:
        contains in interface java.util.Set<T>
        Overrides:
        contains in class AbstractRichIterable<T>
      • each

        public void each​(Procedure<? super T> procedure)
        Description copied from interface: RichIterable
        The procedure is executed for each element in the iterable.

        Example using a Java 8 lambda expression:

         people.each(person -> LOGGER.info(person.getName()));
         

        Example using an anonymous inner class:

         people.each(new Procedure<Person>()
         {
             public void value(Person person)
             {
                 LOGGER.info(person.getName());
             }
         });
         
        This method is a variant of InternalIterable.forEach(Procedure) that has a signature conflict with Iterable.forEach(java.util.function.Consumer).
        Specified by:
        each in interface RichIterable<T>
        See Also:
        InternalIterable.forEach(Procedure), Iterable.forEach(java.util.function.Consumer)
      • each

        protected void each​(Procedure<? super T> procedure,
                            int start,
                            int end)
      • forEachWith

        public <P> void forEachWith​(Procedure2<? super T,​? super P> procedure,
                                    P parameter)
        Description copied from interface: InternalIterable
        The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.

        Example using a Java 8 lambda:

         people.forEachWith((Person person, Person other) ->
             {
                 if (person.isRelatedTo(other))
                 {
                      LOGGER.info(person.getName());
                 }
             }, fred);
         

        Example using an anonymous inner class:

         people.forEachWith(new Procedure2<Person, Person>()
         {
             public void value(Person person, Person other)
             {
                 if (person.isRelatedTo(other))
                 {
                      LOGGER.info(person.getName());
                 }
             }
         }, fred);
         
        Specified by:
        forEachWith in interface InternalIterable<T>
        Overrides:
        forEachWith in class AbstractRichIterable<T>
      • forEachWithIndex

        public void forEachWithIndex​(ObjectIntProcedure<? super T> objectIntProcedure)
        Description copied from interface: InternalIterable
        Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.

        Example using a Java 8 lambda:

         people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
         

        Example using an anonymous inner class:

         people.forEachWithIndex(new ObjectIntProcedure<Person>()
         {
             public void value(Person person, int index)
             {
                 LOGGER.info("Index: " + index + " person: " + person.getName());
             }
         });
         
        Specified by:
        forEachWithIndex in interface InternalIterable<T>
        Overrides:
        forEachWithIndex in class AbstractRichIterable<T>
      • newEmpty

        public UnifiedSet<T> newEmpty()
        Description copied from interface: MutableCollection
        Creates a new empty mutable version of the same collection type. For example, if this instance is a FastList, this method will return a new empty FastList. If the class of this instance is immutable or fixed size (i.e. SingletonList) then a mutable alternative to the class will be provided.
        Specified by:
        newEmpty in interface MutableCollection<T>
        Specified by:
        newEmpty in interface MutableSet<T>
      • getFirst

        public T getFirst()
        Description copied from interface: RichIterable
        Returns the first element of an iterable. In the case of a List it is the element at the first index. In the case of any other Collection, it is the first element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

        The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the first element could be any element from the Set.

        Specified by:
        getFirst in interface RichIterable<T>
      • getLast

        public T getLast()
        Description copied from interface: RichIterable
        Returns the last element of an iterable. In the case of a List it is the element at the last index. In the case of any other Collection, it is the last element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

        The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the last element could be any element from the Set.

        Specified by:
        getLast in interface RichIterable<T>
      • select

        public UnifiedSet<T> select​(Predicate<? super T> predicate)
        Description copied from interface: RichIterable
        Returns all elements of the source collection that return true when evaluating the predicate. This method is also commonly called filter.

        Example using a Java 8 lambda expression:

         RichIterable<Person> selected =
             people.select(person -> person.getAddress().getCity().equals("London"));
         

        Example using an anonymous inner class:

         RichIterable<Person> selected =
             people.select(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.getAddress().getCity().equals("London");
                 }
             });
         
        Specified by:
        select in interface MutableCollection<T>
        Specified by:
        select in interface MutableSet<T>
        Specified by:
        select in interface MutableSetIterable<T>
        Specified by:
        select in interface RichIterable<T>
        Specified by:
        select in interface SetIterable<T>
        Specified by:
        select in interface UnsortedSetIterable<T>
      • selectWith

        public <P> UnifiedSet<T> selectWith​(Predicate2<? super T,​? super P> predicate,
                                            P parameter)
        Description copied from interface: RichIterable
        Similar to RichIterable.select(Predicate), except with an evaluation parameter for the second generic argument in Predicate2.

        E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

        Example using a Java 8 lambda expression:

         RichIterable<Person> selected =
             people.selectWith((Person person, Integer age) -> person.getAge()>= age, Integer.valueOf(18));
         

        Example using an anonymous inner class:

         RichIterable<Person> selected =
             people.selectWith(new Predicate2<Person, Integer>()
             {
                 public boolean accept(Person person, Integer age)
                 {
                     return person.getAge()>= age;
                 }
             }, Integer.valueOf(18));
         
        Specified by:
        selectWith in interface MutableCollection<T>
        Specified by:
        selectWith in interface MutableSet<T>
        Specified by:
        selectWith in interface MutableSetIterable<T>
        Specified by:
        selectWith in interface RichIterable<T>
        Specified by:
        selectWith in interface SetIterable<T>
        Specified by:
        selectWith in interface UnsortedSetIterable<T>
        Parameters:
        predicate - a Predicate2 to use as the select criteria
        parameter - a parameter to pass in for evaluation of the second argument P in predicate
        See Also:
        RichIterable.select(Predicate)
      • reject

        public UnifiedSet<T> reject​(Predicate<? super T> predicate)
        Description copied from interface: RichIterable
        Returns all elements of the source collection that return false when evaluating of the predicate. This method is also sometimes called filterNot and is the equivalent of calling iterable.select(Predicates.not(predicate)).

        Example using a Java 8 lambda expression:

         RichIterable<Person> rejected =
             people.reject(person -> person.person.getLastName().equals("Smith"));
         

        Example using an anonymous inner class:

         RichIterable<Person> rejected =
             people.reject(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.person.getLastName().equals("Smith");
                 }
             });
         
        Specified by:
        reject in interface MutableCollection<T>
        Specified by:
        reject in interface MutableSet<T>
        Specified by:
        reject in interface MutableSetIterable<T>
        Specified by:
        reject in interface RichIterable<T>
        Specified by:
        reject in interface SetIterable<T>
        Specified by:
        reject in interface UnsortedSetIterable<T>
        Parameters:
        predicate - a Predicate to use as the reject criteria
        Returns:
        a RichIterable that contains elements that cause Predicate.accept(Object) method to evaluate to false
      • rejectWith

        public <P> UnifiedSet<T> rejectWith​(Predicate2<? super T,​? super P> predicate,
                                            P parameter)
        Description copied from interface: RichIterable
        Similar to RichIterable.reject(Predicate), except with an evaluation parameter for the second generic argument in Predicate2.

        E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

        Example using a Java 8 lambda expression:

         RichIterable<Person> rejected =
             people.rejectWith((Person person, Integer age) -> person.getAge() < age, Integer.valueOf(18));
         

        Example using an anonymous inner class:

         MutableList<Person> rejected =
             people.rejectWith(new Predicate2<Person, Integer>()
             {
                 public boolean accept(Person person, Integer age)
                 {
                     return person.getAge() < age;
                 }
             }, Integer.valueOf(18));
         
        Specified by:
        rejectWith in interface MutableCollection<T>
        Specified by:
        rejectWith in interface MutableSet<T>
        Specified by:
        rejectWith in interface MutableSetIterable<T>
        Specified by:
        rejectWith in interface RichIterable<T>
        Specified by:
        rejectWith in interface SetIterable<T>
        Specified by:
        rejectWith in interface UnsortedSetIterable<T>
        Parameters:
        predicate - a Predicate2 to use as the select criteria
        parameter - a parameter to pass in for evaluation of the second argument P in predicate
        See Also:
        RichIterable.select(Predicate)
      • partition

        public PartitionMutableSet<T> partition​(Predicate<? super T> predicate)
        Description copied from interface: RichIterable
        Filters a collection into a PartitionedIterable based on the evaluation of the predicate.

        Example using a Java 8 lambda expression:

         PartitionIterable<Person> newYorkersAndNonNewYorkers =
             people.partition(person -> person.getAddress().getState().getName().equals("New York"));
         

        Example using an anonymous inner class:

         PartitionIterable<Person> newYorkersAndNonNewYorkers =
             people.partition(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.getAddress().getState().getName().equals("New York");
                 }
             });
         
        Specified by:
        partition in interface MutableCollection<T>
        Specified by:
        partition in interface MutableSet<T>
        Specified by:
        partition in interface MutableSetIterable<T>
        Specified by:
        partition in interface RichIterable<T>
        Specified by:
        partition in interface SetIterable<T>
      • partitionWith

        public <P> PartitionMutableSet<T> partitionWith​(Predicate2<? super T,​? super P> predicate,
                                                        P parameter)
        Description copied from interface: RichIterable
        Filters a collection into a PartitionIterable based on the evaluation of the predicate.

        Example using a Java 8 lambda expression:

         PartitionIterable<Person> newYorkersAndNonNewYorkers =
             people.partitionWith((Person person, String state) -> person.getAddress().getState().getName().equals(state), "New York");
         

        Example using an anonymous inner class:

         PartitionIterable<Person> newYorkersAndNonNewYorkers =
             people.partitionWith(new Predicate2<Person, String>()
             {
                 public boolean accept(Person person, String state)
                 {
                     return person.getAddress().getState().getName().equals(state);
                 }
             }, "New York");
         
        Specified by:
        partitionWith in interface MutableCollection<T>
        Specified by:
        partitionWith in interface MutableSet<T>
        Specified by:
        partitionWith in interface MutableSetIterable<T>
        Specified by:
        partitionWith in interface RichIterable<T>
        Specified by:
        partitionWith in interface SetIterable<T>
      • shortCircuit

        protected boolean shortCircuit​(Predicate<? super T> predicate,
                                       boolean expected,
                                       boolean onShortCircuit,
                                       boolean atEnd,
                                       int start,
                                       int end)
        Specified by:
        shortCircuit in class AbstractUnifiedSet<T>
      • shortCircuitWith

        protected <P> boolean shortCircuitWith​(Predicate2<? super T,​? super P> predicate2,
                                               P parameter,
                                               boolean expected,
                                               boolean onShortCircuit,
                                               boolean atEnd)
        Specified by:
        shortCircuitWith in class AbstractUnifiedSet<T>
      • chainedShortCircuitWith

        private <P> boolean chainedShortCircuitWith​(UnifiedSet.ChainedBucket bucket,
                                                    Predicate2<? super T,​? super P> predicate,
                                                    P parameter,
                                                    boolean expected)
      • with

        public UnifiedSet<T> with​(T element)
        Description copied from interface: MutableCollection
        This method allows mutable and fixed size collections the ability to add elements to their existing elements. In order to support fixed size a new instance of a collection would have to be returned taking the elements of the original collection and appending the new element to form the new collection. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
         MutableCollection<String> list = list.with("1");
         list = list.with("2");
         return list;
         
        In the case of FixedSizeCollection a new instance of MutableCollection will be returned by with, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling add on itself.
        Specified by:
        with in interface MutableCollection<T>
        Specified by:
        with in interface MutableSet<T>
        Specified by:
        with in interface MutableSetIterable<T>
        See Also:
        Collection.add(Object)
      • with

        public UnifiedSet<T> with​(T element1,
                                  T element2,
                                  T element3)
      • withAll

        public UnifiedSet<T> withAll​(java.lang.Iterable<? extends T> iterable)
        Description copied from interface: MutableCollection
        This method allows mutable and fixed size collections the ability to add multiple elements to their existing elements. In order to support fixed size a new instance of a collection would have to be returned taking the elements of the original collection and appending the new elements to form the new collection. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
         MutableCollection<String> list = list.withAll(FastList.newListWith("1", "2"));
         
        In the case of FixedSizeCollection a new instance of MutableCollection will be returned by withAll, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling addAll on itself.
        Specified by:
        withAll in interface MutableCollection<T>
        Specified by:
        withAll in interface MutableSet<T>
        Specified by:
        withAll in interface MutableSetIterable<T>
        See Also:
        Collection.addAll(Collection)
      • without

        public UnifiedSet<T> without​(T element)
        Description copied from interface: MutableCollection
        This method allows mutable and fixed size collections the ability to remove elements from their existing elements. In order to support fixed size a new instance of a collection would have to be returned containing the elements that would be left from the original collection after calling remove. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
         MutableCollection<String> list = list.without("1");
         list = list.without("2");
         return list;
         
        In the case of FixedSizeCollection a new instance of MutableCollection will be returned by without, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling remove on itself.
        Specified by:
        without in interface MutableCollection<T>
        Specified by:
        without in interface MutableSet<T>
        Specified by:
        without in interface MutableSetIterable<T>
        See Also:
        Collection.remove(Object)
      • withoutAll

        public UnifiedSet<T> withoutAll​(java.lang.Iterable<? extends T> elements)
        Description copied from interface: MutableCollection
        This method allows mutable and fixed size collections the ability to remove multiple elements from their existing elements. In order to support fixed size a new instance of a collection would have to be returned containing the elements that would be left from the original collection after calling removeAll. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
         MutableCollection<String> list = list.withoutAll(FastList.newListWith("1", "2"));
         
        In the case of FixedSizeCollection a new instance of MutableCollection will be returned by withoutAll, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling removeAll on itself.
        Specified by:
        withoutAll in interface MutableCollection<T>
        Specified by:
        withoutAll in interface MutableSet<T>
        Specified by:
        withoutAll in interface MutableSetIterable<T>
        See Also:
        Collection.removeAll(Collection)
      • ensureCapacity

        private void ensureCapacity​(int size)
      • copySet

        protected boolean copySet​(UnifiedSet<?> unifiedset)
      • remove

        public boolean remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.Set<T>
        Overrides:
        remove in class AbstractMutableCollection<T>
      • size

        public int size()
        Description copied from interface: RichIterable
        Returns the number of items in this iterable.
        Specified by:
        size in interface BatchIterable<T>
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface Pool<T>
        Specified by:
        size in interface RichIterable<T>
        Specified by:
        size in interface java.util.Set<T>
      • equals

        public boolean equals​(java.lang.Object object)
        Description copied from interface: SetIterable
        Follows the same general contract as Set.equals(Object).
        Specified by:
        equals in interface java.util.Collection<T>
        Specified by:
        equals in interface java.util.Set<T>
        Specified by:
        equals in interface SetIterable<T>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Description copied from interface: SetIterable
        Follows the same general contract as Set.hashCode().
        Specified by:
        hashCode in interface java.util.Collection<T>
        Specified by:
        hashCode in interface java.util.Set<T>
        Specified by:
        hashCode in interface SetIterable<T>
        Overrides:
        hashCode in class java.lang.Object
      • trimToSize

        public boolean trimToSize()
      • addForTrim

        private void addForTrim​(java.lang.Object key,
                                int oldIndex,
                                int mask)
      • chainedAddForTrim

        private void chainedAddForTrim​(java.lang.Object key,
                                       int index)
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
      • writeExternalChain

        private void writeExternalChain​(java.io.ObjectOutput out,
                                        UnifiedSet.ChainedBucket bucket)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • addIfFound

        private void addIfFound​(T key,
                                UnifiedSet<T> other)
      • retainAllFromNonSet

        private boolean retainAllFromNonSet​(java.lang.Iterable<?> iterable)
      • retainAllFromSet

        private boolean retainAllFromSet​(java.util.Set<?> collection)
      • toArray

        public java.lang.Object[] toArray()
        Description copied from interface: RichIterable
        Converts this iterable to an array.
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface RichIterable<T>
        Specified by:
        toArray in interface java.util.Set<T>
        Overrides:
        toArray in class AbstractRichIterable<T>
        See Also:
        Collection.toArray()
      • copyToArray

        private void copyToArray​(java.lang.Object[] result)
      • copyBucketToArray

        private int copyBucketToArray​(java.lang.Object[] result,
                                      UnifiedSet.ChainedBucket bucket,
                                      int count)
      • toArray

        public <T> T[] toArray​(T[] array)
        Description copied from interface: RichIterable
        Converts this iterable to an array using the specified target array, assuming the target array is as long or longer than the iterable.
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface RichIterable<T>
        Specified by:
        toArray in interface java.util.Set<T>
        Overrides:
        toArray in class AbstractRichIterable<T>
        See Also:
        Collection.toArray(Object[])
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.Set<T>
      • groupBy

        public <V> UnifiedSetMultimap<V,​T> groupBy​(Function<? super T,​? extends V> function)
        Description copied from interface: RichIterable
        For each element of the iterable, the function is evaluated and the results of these evaluations are collected into a new multimap, where the transformed value is the key and the original values are added to the same (or similar) species of collection as the source iterable.

        Example using a Java 8 method reference:

         Multimap<String, Person> peopleByLastName =
             people.groupBy(Person::getLastName);
         

        Example using an anonymous inner class:

         Multimap<String, Person> peopleByLastName =
             people.groupBy(new Function<Person, String>()
             {
                 public String valueOf(Person person)
                 {
                     return person.getLastName();
                 }
             });
         
        Specified by:
        groupBy in interface MutableCollection<T>
        Specified by:
        groupBy in interface MutableSet<T>
        Specified by:
        groupBy in interface MutableSetIterable<T>
        Specified by:
        groupBy in interface RichIterable<T>
        Specified by:
        groupBy in interface UnsortedSetIterable<T>
      • get

        public T get​(T key)
        Description copied from interface: Pool
        Locates an object in the pool which is equal to key.
        Specified by:
        get in interface Pool<T>
        Parameters:
        key - the value to look for
        Returns:
        The object reference in the pool equal to key or null.
      • put

        public T put​(T key)
        Description copied from interface: Pool
        Puts key into the pool. If there is no existing object that is equal to key, key will be added to the pool and the return value will be the same instance. If there is an existing object in the pool that is equal to key, the pool will remain unchanged and the pooled instance will be is returned.
        Specified by:
        put in interface Pool<T>
        Parameters:
        key - the value to add if not in the pool
        Returns:
        the object reference in the pool equal to key (either key itself or the existing reference)
      • chainedPut

        private T chainedPut​(T key,
                             int index)
      • removeFromPool

        public T removeFromPool​(T key)
        Description copied from interface: Pool
        Locates an object in the pool which is equal to key and removes it.
        Specified by:
        removeFromPool in interface Pool<T>
        Parameters:
        key - object to remove
        Returns:
        The object reference in the pool equal to key or null.
      • nonSentinel

        private T nonSentinel​(java.lang.Object key)
      • toSentinelIfNull

        private static java.lang.Object toSentinelIfNull​(java.lang.Object key)
      • nonNullTableObjectEquals

        private boolean nonNullTableObjectEquals​(java.lang.Object cur,
                                                 T key)