Class ImmutableSet<E>

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

public final class ImmutableSet<E> extends AbstractBaseCollection<E> implements ImmutableCollection<E>
A hash-based implementation of the Set and ImmutableCollection interfaces.

After creation, instances of this class do not permit adding nor removing of elements. Any mutator method will throw an UnsupportedOperationException when called. This is true for mutator methods present in this class and for mutator methods present in the returned iterators.

Since:
2
  • Method Details

    • copyOf

      public static <E> ImmutableSet<E> copyOf(E[] array)
      Returns a new ImmutableSet containing all of the distinct elements in the specified array.

      Two non-null elements e1 and e2 are distinct if the expression e1.equals(e2) evaluates to false.

      This method returns a copy in the sense that modifications in the specified array are not propagated to the returned set.

      Type Parameters:
      E - type of the elements in this set
      Parameters:
      array - an array for which an immutable set copy will be created
      Returns:
      a new ImmutableSet containing all of the distinct elements in the specified array
      Throws:
      NullPointerException - if the array is null or any element in the array is null
    • copyOf

      public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements)
      Returns the specified iterable if it is an ImmutableSet; returns a new ImmutableSet containing all of the distinct elements returned by the specified iterable's iterator.

      Two non-null elements e1 and e2 are distinct if the expression e1.equals(e2) evaluates to false.

      Type Parameters:
      E - type of the elements in this set
      Parameters:
      elements - an Iterable for which an immutable set copy will be created
      Returns:
      the specified iterable if it is an ImmutableSet or a new ImmutableSet containing all of the elements returned by the specified iterable's iterator
      Throws:
      NullPointerException - if the iterable is null or if any element returned by the iterable's iterator is null
    • copyOf

      public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> iterator)
      Returns a new ImmutableSet instance containing all of the distinct elements returned by the specified iterator.

      Two non-null elements e1 and e2 are distinct if the expression e1.equals(e2) evaluates to false.

      Type Parameters:
      E - type of the elements in this set
      Parameters:
      iterator - an Iterator for which an immutable set copy will be created
      Returns:
      a new ImmutableSet instance containing all of the distinct elements produced by the specified iterator
      Throws:
      NullPointerException - if the iterator is null or any element produced by the iterator is null
    • of

      public static <E> ImmutableSet<E> of()
      Returns the empty ImmutableSet.
      Type Parameters:
      E - type of the elements in this set
      Returns:
      the empty ImmutableSet
    • of

      public static <E> ImmutableSet<E> of(E element)
      Returns a new ImmutableSet containing one element.
      Type Parameters:
      E - type of the elements in this set
      Parameters:
      element - the single element
      Returns:
      a new ImmutableSet containing one element
      Throws:
      NullPointerException - if the specified element is null
    • of

      public static <E> ImmutableSet<E> of(E first, E... more)
      Returns a new ImmutableSet containing all of the distinct specified elements.

      Two non-null elements e1 and e2 are distinct if the expression e1.equals(e2) evaluates to false.

      Type Parameters:
      E - type of the elements in this set
      Parameters:
      first - the first element
      more - the additional elements
      Returns:
      a new ImmutableSet containing all of the distinct specified elements
      Throws:
      NullPointerException - if any of the specified elements is null
    • add

      public final boolean add(E e)
      This operation is not supported.

      This method performs no operation other than throw an UnsupportedOperationException.

      Specified by:
      add in interface BaseCollection<E>
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface ImmutableCollection<E>
      Specified by:
      add in interface Set<E>
      Parameters:
      e - ignored (the operation is not supported)
      Returns:
      this method does not return as it always throw an exception
      Throws:
      UnsupportedOperationException - always
    • addAll

      public final boolean addAll(Collection<? extends E> c)
      This operation is not supported.

      This method performs no operation other than throw an UnsupportedOperationException.

      Specified by:
      addAll in interface BaseCollection<E>
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface ImmutableCollection<E>
      Specified by:
      addAll in interface Set<E>
      Parameters:
      c - ignored (the operation is not supported)
      Returns:
      this method does not return as it always throw an exception
      Throws:
      UnsupportedOperationException - always
    • clear

      public final void clear()
      This operation is not supported.

      This method performs no operation other than throw an UnsupportedOperationException.

      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface ImmutableCollection<E>
      Specified by:
      clear in interface Set<E>
      Throws:
      UnsupportedOperationException - always
    • 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()