java.lang.Object
br.com.objectos.collections.base.AbstractBaseCollection<E>
br.com.objectos.collections.set.MutableSet<E>
Type Parameters:
E - type of the elements in this set
All Implemented Interfaces:
BaseCollection<E>, Joinable, MutableCollection<E>, br.com.objectos.core.object.ToStringObject, Iterable<E>, Collection<E>, Set<E>

public final class MutableSet<E> extends AbstractBaseCollection<E> implements MutableCollection<E>
A hash-based implementation of the Set and MutableCollection interfaces.
Since:
2
  • Constructor Details

    • MutableSet

      public MutableSet()
      Creates a new MutableSet instance.
  • Method Details

    • create

      public static <E> MutableSet<E> create()
      Creates and returns a new MutableSet instance.

      This method is mainly provided as a convenience for Java Multi-Release codebases. In particular codebases that must support versions prior to Java 7 and, therefore, cannot use the diamond operator.

      Type Parameters:
      E - type of the elements in this set
      Returns:
      a new MutableSet instance
    • add

      public final boolean add(E e)
      Adds the specified element to this set if it is not already present.
      Specified by:
      add in interface BaseCollection<E>
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Parameters:
      e - an element to be added to this set
      Returns:
      true if this set was modified as a result of this operation. false if the element was already present in this set
      Throws:
      NullPointerException - if the specified element is null
    • addAll

      public final boolean addAll(Collection<? extends E> c)
      Adds all of the elements in the specified collection to this set if they are not already present.

      The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this set, and this set is nonempty.)

      Specified by:
      addAll in interface BaseCollection<E>
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
      Parameters:
      c - a collection containing the elements to be added to this set
      Returns:
      true if this set was modified as a result of this operation. false if all of the elements were already present in this set
      Throws:
      NullPointerException - if the specified collection is null or if it contains any element that is null
    • addAllIterable

      public final boolean addAllIterable(Iterable<? extends E> iterable)
      Adds all of the elements returned by the specified iterable's iterator to this set if they are not already present.

      The behavior of this operation is undefined if the specified iterable is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified iterable is this list, and this list is nonempty.)

      Specified by:
      addAllIterable in interface MutableCollection<E>
      Parameters:
      iterable - Iterable containing elements to be added to this set
      Returns:
      true if this set was modified as a result of this operation. false if all of the elements were already present in this set
      Throws:
      NullPointerException - if the specified iterable is null or if it provides any element that is null
    • addWithNullMessage

      public final boolean addWithNullMessage(E e, Object nullMessage)
      Adds the specified element e to this set if it is not already present.

      If a NullPointerException is to be thrown, the nullMessage value is used as the exception's message.

      Typical usage:

       set.addWithNullMessage(value, "value == null");
      Specified by:
      addWithNullMessage in interface MutableCollection<E>
      Parameters:
      e - an element to be added to this set (if it not null)
      nullMessage - the NullPointerException message
      Returns:
      true if this set was modified as a result of this operation. false if the element was already present in this set
      Throws:
      NullPointerException - if the specified element is null
      See Also:
      • Checks.checkNotNull(Object, Object)
    • addWithNullMessage

      public final boolean addWithNullMessage(E e, Object nullMessageStart, int index, Object nullMessageEnd)
      Adds the specified element e to this set if it is not already present.

      If a NullPointerException is to be thrown, the concatenation of nullMessageStart, index and nullMessageEnd is used as the exception's message.

      Typical usage:

       set.addWithNullMessage(element, "elements[", index, "] == null");
      Specified by:
      addWithNullMessage in interface MutableCollection<E>
      Parameters:
      e - an element to be added to this set (if it not null)
      nullMessageStart - the first part of the NullPointerException message
      index - the second part of the NullPointerException message
      nullMessageEnd - the third part of the NullPointerException message
      Returns:
      true if this set was modified as a result of this operation. false if the element was already present in this set
      See Also:
      • Checks.checkNotNull(Object, Object, int, Object)
    • clear

      public final void clear()
      Removes all of the elements from this set.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
    • toImmutableSet

      public final ImmutableSet<E> toImmutableSet()
      Returns an ImmutableSet copy of this set.

      The returned ImmutableSet will contain all of the elements from this set.

      The returned set will be a copy in the sense that, after this method returns, modifying this set will have no effect on the returned (copied) one.

      Note, however, that the behaviour of this method is undefined if this set is modified while the copy is being made.

      Returns:
      an ImmutableSet copy of this set
    • contains

      public final boolean contains(Object o)
      Returns true if this set contains the specified element. More formally, returns true if and only if this set contains at least one element e such that e.equals(o).
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Parameters:
      o - an element to check for presence in this set
      Returns:
      true if this set contains the specified value
    • equals

      public final boolean equals(Object obj)

      Compares the specified object with this set for equality. Returns true if and only if

      • the specified object is also a Set;
      • both sets have same size; and
      • each element in this set is also present in the specified set.
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface Set<E>
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to be compared for equality with this set
      Returns:
      true if the specified object is equal to this set
    • iterator

      public final UnmodifiableIterator<E> iterator()
      Returns an iterator over the elements in this set. The elements are returned in no particular order.
      Specified by:
      iterator in interface BaseCollection<E>
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Returns:
      an iterator over the elements in this set
    • size

      public final int size()
      Returns the size of this set. The size of a set is equal to the number of elements it contains.
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Returns:
      the size of this set
    • getOnlyImpl

      protected final E getOnlyImpl()
      Returns the only element in this set.
      Returns:
      the only element in this set
    • removeIf

      public final boolean removeIf(Predicate<? super E> arg0)
    • contains

      public final boolean contains(Object arg0, Object... arg1)
      Specified by:
      contains in interface BaseCollection<E extends Object>
    • containsAll

      public final boolean containsAll(Collection<?> arg0)
      Specified by:
      containsAll in interface Collection<E extends Object>
    • formatToString

      public void formatToString(StringBuilder arg0, int arg1)
      Specified by:
      formatToString in interface br.com.objectos.core.object.ToStringObject
    • getOnly

      public final E getOnly()
      Specified by:
      getOnly in interface BaseCollection<E extends Object>
    • isEmpty

      public final boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E extends Object>
    • join

      public String join()
      Specified by:
      join in interface Joinable
    • join

      public String join(String arg0)
      Specified by:
      join in interface Joinable
    • join

      public String join(String arg0, String arg1, String arg2)
      Specified by:
      join in interface Joinable
    • remove

      public final boolean remove(Object arg0)
      Specified by:
      remove in interface BaseCollection<E extends Object>
      Specified by:
      remove in interface Collection<E extends Object>
    • removeAll

      public final boolean removeAll(Collection<?> arg0)
      Specified by:
      removeAll in interface BaseCollection<E extends Object>
      Specified by:
      removeAll in interface Collection<E extends Object>
    • retainAll

      public final boolean retainAll(Collection<?> arg0)
      Specified by:
      retainAll in interface BaseCollection<E extends Object>
      Specified by:
      retainAll in interface Collection<E extends Object>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E extends Object>
    • toArray

      public <T extends Object> T[] toArray(T[] arg0)
      Specified by:
      toArray in interface Collection<E extends Object>
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • indexName

      protected final String indexName(int arg0, int arg1)
    • sizeDigits

      protected final int sizeDigits()