Class SetView<E>

java.lang.Object
one.microstream.collections.SetView<E>
All Implemented Interfaces:
Iterable<E>, CapacityCarrying, ExtendedCollection<E>, Sized, XGettingCollection<E>, XGettingSet<E>, XIterable<E>, XJoinable<E>, Copyable

public final class SetView<E>
extends Object
implements XGettingSet<E>
Wrapper class that reduces the services provided by any wrapped XGettingSet to only those of XGettingSet, effectively making the wrapped XGettingSet instance immutable (or read-only) if used through an instance of this class.

All methods declared in XGettingSet are transparently passed to the wrapped set.
All modifying methods declared in Collection and Set (all variations of add~(), remove~() and retain~() as well as set() and clear()) immediately throw an UnsupportedOperationException when called.

This concept can be very useful if a class wants to provide public read access to an internal set without either the danger of the set being modified from the outside or the need to copy the whole set on every access.

This is one of many useful concepts that are missing in the JDK Collections Framework and thus so far lead to either inefficient or unneccessary verbose program code (or both).

  • Constructor Details

  • Method Details

    • get

      public E get()
      Description copied from interface: XGettingCollection
      Gets one element from the collection. If the collection is not ordered XGettingSequence, then it is undefined which element is returned. If the collection is ordered, the element at index 0 is returned.
      Specified by:
      get in interface XGettingCollection<E>
      Returns:
      the first / any element.
    • equality

      public Equalator<? super E> equality()
      Specified by:
      equality in interface XGettingCollection<E>
    • hasVolatileElements

      public boolean hasVolatileElements()
      Description copied from interface: ExtendedCollection
      Tells if this collection contains volatile elements.
      An element is volatile, if it can become no longer reachable by the collection without being removed from the collection. Examples are WeakReference of SoftReference or implementations of collection entries that remove the element contained in an entry by some means outside the collection.
      Note that WeakReference instances that are added to a a simple (non-volatile) implementation of a collection do not make the collection volatile, as the elements themselves (the reference instances) are still strongly referenced.
      Specified by:
      hasVolatileElements in interface ExtendedCollection<E>
      Specified by:
      hasVolatileElements in interface XGettingCollection<E>
      Returns:
      true if the collection contains volatile elements.
    • containsSearched

      public boolean containsSearched​(Predicate<? super E> predicate)
      Specified by:
      containsSearched in interface XGettingCollection<E>
    • applies

      public boolean applies​(Predicate<? super E> predicate)
      Description copied from interface: XGettingCollection
      Tests each element of the collection on the given predicate.
      Specified by:
      applies in interface XGettingCollection<E>
      Parameters:
      predicate - that's tested on each element.
      Returns:
      If all elements test successfully, true is returned. Otherwise (if at least one test has failed), false is returned.
    • nullAllowed

      public boolean nullAllowed()
      Description copied from interface: ExtendedCollection
      Defines if null-elements are allowed inside the collection or not.
      Specified by:
      nullAllowed in interface ExtendedCollection<E>
      Returns:
      true if null is allowed inside the collection; false if not
    • nullContained

      public boolean nullContained()
      Specified by:
      nullContained in interface XGettingCollection<E>
    • containsAll

      public boolean containsAll​(XGettingCollection<? extends E> elements)
      Specified by:
      containsAll in interface XGettingCollection<E>
      Parameters:
      elements - to be searched in the collection.
      Returns:
      Whether this collection contains all given elements as specified by the Equalator.
    • contains

      public boolean contains​(E element)
      Description copied from interface: XGettingCollection
      Checks if the given element is contained in the collection.
      In contrast to the XGettingCollection.containsId(Object) method, this method uses the internal Equalator defined by the collection itself.
      Specified by:
      contains in interface XGettingCollection<E>
      Parameters:
      element - to be searched in the collection
      Returns:
      Whether this collection contains the given element as specified by the Equalator.
    • containsId

      public boolean containsId​(E element)
      Description copied from interface: XGettingCollection
      Special version of contains() that guarantees to use identity comparison (" == ") when searching for the given element regardless of the collection's internal logic.
      This method has the same behavior as XGettingCollection.containsSearched(Predicate) with a Predicate implementation that checks for object identity. The only difference is a performance and usability advantage
      Specified by:
      containsId in interface XGettingCollection<E>
      Parameters:
      element - the element to be searched in the collection by identity.
      Returns:
      whether this collection contains exactly the given element.
    • copy

      public SetView<E> copy()
      Description copied from interface: XGettingCollection
      Creates a true copy of this collection which references the same elements as this collection does at the time the method is called. The elements themselves are NOT copied (no deep copying).
      The type of the returned set is the same as of this list if possible.
      Specified by:
      copy in interface Copyable
      Specified by:
      copy in interface XGettingCollection<E>
      Specified by:
      copy in interface XGettingSet<E>
      Returns:
      a copy of this list
    • filterTo

      public <C extends Consumer<? super E>> C filterTo​(C target, Predicate<? super E> predicate)
      Description copied from interface: XGettingCollection
      Calls Consumer.accept(Object) on the target Consumer for all the elements of this collection which test true on the given predicate.

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,3);
      BulkList<Integer> filteredCollection = collection1.filterTo(BulkList.New(), e-> e % 2 == 0);

      Results in filteredCollection containing 2.

      Specified by:
      filterTo in interface XGettingCollection<E>
      Parameters:
      target - on which the Consumer.accept(Object) is called for elements that test true.
      predicate - on which to test all elements.
      Returns:
      Given target
    • copyTo

      public <C extends Consumer<? super E>> C copyTo​(C target)
      Description copied from interface: XGettingCollection
      Calls Consumer.accept(Object) on the target Consumer for all the elements of this collection.

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,3);
      BulkList<Integer> copiedCollection = collection1.copyTo(BulkList.New());

      Results in copiedCollection containing 1, 2 and 3.

      Specified by:
      copyTo in interface XGettingCollection<E>
      Parameters:
      target - on which the Consumer.accept(Object) is called for all elements of this collection.
      Returns:
      Given target
    • count

      public long count​(E element)
      Description copied from interface: XGettingCollection
      Count how many times this element matches another element in the collection using the Equalator.
      Specified by:
      count in interface XGettingCollection<E>
      Parameters:
      element - to count
      Returns:
      Amount of matches
    • countBy

      public long countBy​(Predicate<? super E> predicate)
      Description copied from interface: XGettingCollection
      Count how many matches are found using the given predicate on each element of the collection.
      Specified by:
      countBy in interface XGettingCollection<E>
      Parameters:
      predicate - defines which elements are counted and which are not
      Returns:
      Amount of matches
    • distinct

      public <C extends Consumer<? super E>> C distinct​(C target, Equalator<? super E> equalator)
      Description copied from interface: XGettingCollection
      Calls Consumer.accept(Object) on the target Consumer for all the unique/distinct elements of this collection. This means the elements are not equal to each other.
      Uniqueness is defined by the given Equalator.

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,2,3);
      BulkList<Integer> distinctCollection = collection1.distinct(BulkList.New(), Equalator.identity());

      Results in distinctCollection containing 1, 2 and 3.

      Specified by:
      distinct in interface XGettingCollection<E>
      Parameters:
      target - on which the Consumer.accept(Object) is called for every distinct element of this collection.
      equalator - defines what distinct means (which elements are equal to one another)
      Returns:
      Given target
    • distinct

      public <C extends Consumer<? super E>> C distinct​(C target)
      Description copied from interface: XGettingCollection
      Calls Consumer.accept(Object) on the target Consumer for all the unique/distinct elements of this collection. This means the elements are not equal to each other.
      Uniqueness is defined by the collections internal Equalator.

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,2,3);
      BulkList<Integer> distinctCollection = collection1.distinct(BulkList.New());

      Results in distinctCollection containing 1, 2 and 3.

      Specified by:
      distinct in interface XGettingCollection<E>
      Parameters:
      target - on which the Consumer.accept(Object) is called for every distinct element of this collection.
      Returns:
      Given target
    • equals

      public boolean equals​(XGettingCollection<? extends E> samples, Equalator<? super E> equalator)
      Specified by:
      equals in interface XGettingCollection<E>
      Parameters:
      samples - is the collection which is checked for equality
      equalator - is used to check the equality of the collections
      Returns:
      true if the passed collection is of the same type as this collection and this.equalsContent(list, equalator) yields true
    • equalsContent

      public boolean equalsContent​(XGettingCollection<? extends E> samples, Equalator<? super E> equalator)
      Description copied from interface: XGettingCollection
      Returns true if all elements of this list and the passed list are sequentially equal as defined by the passed equalator.

      Note that for colletion types that don't have a defined order of elements, this method is hardly usable (as is XGettingCollection.equals(Object) for them as defined in Collection). The core problem of comparing collections that have no defined order is that they aren't really reliably comparable to any other collection.

      Specified by:
      equalsContent in interface XGettingCollection<E>
      Parameters:
      samples - is the collection which is checked for equality
      equalator - the equalator to use to determine the equality of each element
      Returns:
      true if this list is equal to the passed list, false otherwise
    • except

      public <C extends Consumer<? super E>> C except​(XGettingCollection<? extends E> other, Equalator<? super E> equalator, C target)
      Description copied from interface: XGettingCollection
      Calls Consumer.accept(Object) on the target Consumer for each element of this collection that is not contained in the other collection (through the given equalator).

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,3);
      BulkList<Integer> collection2 = BulkList.New(2,3,4);
      BulkList<Integer> exceptCollection = collection1.except(collection2, Equalator.identity(), BulkList.New());

      Results in exceptCollection containing 1.

      Specified by:
      except in interface XGettingCollection<E>
      Type Parameters:
      C - type of the target
      Parameters:
      other - collection whose elements are excluded from the target.
      equalator - which is used for the equal-tests.
      target - on which the Consumer.accept(Object) is called for elements not contained in the other collection.
      Returns:
      Given target
    • iterate

      public final <P extends Consumer<? super E>> P iterate​(P procedure)
      Description copied from interface: XIterable
      Executes the given procedure for each element of the XIterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, procedures are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the procedure are relayed to the caller.
      Should be identical to Iterable.forEach(Consumer).
      Specified by:
      iterate in interface XGettingSet<E>
      Specified by:
      iterate in interface XIterable<E>
      Type Parameters:
      P - type of procedure
      Parameters:
      procedure - The procedure to be performed for each element
      Returns:
      Given procedure
    • join

      public final <A> A join​(BiConsumer<? super E,​? super A> joiner, A aggregate)
      Description copied from interface: XJoinable
      Iterates over all elements of the collections and calls the joiner with each element and the aggregate.
      Specified by:
      join in interface XGettingCollection<E>
      Specified by:
      join in interface XJoinable<E>
      Type Parameters:
      A - type of aggregate
      Parameters:
      joiner - is the actual function to do the joining
      aggregate - where to join into
    • intersect

      public <C extends Consumer<? super E>> C intersect​(XGettingCollection<? extends E> other, Equalator<? super E> equalator, C target)
      Description copied from interface: XGettingCollection
      Tests equality between each element of the two lists and calls Consumer.accept(Object) on the target Consumer for the equal elements.
      Therefore it effectively creates a mathematical intersection between the two collections.

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,3);
      BulkList<Integer> collection2 = BulkList.New(2,3,4);
      BulkList<Integer> intersection = collection1.intersect(collection2, Equalator.identity(), BulkList.New());

      Results in intersection containing 2 and 3.

      Specified by:
      intersect in interface XGettingCollection<E>
      Parameters:
      other - collection to intersect with.
      equalator - which is used for the equal-tests.
      target - on which the Consumer.accept(Object) is called for equal elements.
      Returns:
      Given target
    • max

      public E max​(Comparator<? super E> comparator)
      Specified by:
      max in interface XGettingCollection<E>
    • min

      public E min​(Comparator<? super E> comparator)
      Specified by:
      min in interface XGettingCollection<E>
    • seek

      public E seek​(E sample)
      Description copied from interface: XGettingCollection
      Returns the first contained element matching the passed sample as defined by the collection's equality logic or null, if no fitting element is contained. (For collections using referential equality, this method is basically just a variation of XGettingCollection.contains(Object) with a different return type. For collections with data-dependant equality, the returned element might be the same as the passed one or a data-wise equal one, depending on the content of the collection)
      Specified by:
      seek in interface XGettingCollection<E>
      Parameters:
      sample - to seek in the collection
      Returns:
      the first contained element matching the passed sample
    • search

      public E search​(Predicate<? super E> predicate)
      Description copied from interface: XGettingCollection
      Returns the first contained element matching the passed predicate.
      Specified by:
      search in interface XGettingCollection<E>
      Parameters:
      predicate - defines which element is searched
      Returns:
      Matching element
    • toArray

      public E[] toArray​(Class<E> type)
      Description copied from interface: XGettingCollection
      Returns a typed array containing all of the elements in this collection.

      The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

      This method acts as bridge between MicroStream-based collections and Java-native-based APIs.

      Specified by:
      toArray in interface XGettingCollection<E>
      Returns:
      a typed array containing all of the elements in this collection
    • union

      public <C extends Consumer<? super E>> C union​(XGettingCollection<? extends E> other, Equalator<? super E> equalator, C target)
      Description copied from interface: XGettingCollection
      Calls Consumer.accept(Object) on the target Consumer for all the elements of this collection. And calls it for all elements of the other collection, that are not already in this collection (defined by the given Equalator)
      Therefore it effectively creates a mathematical union between the two collections.

      Since all MicroStream Collections implement the Consumer interface, new collections can be used as target.

      Example:
      BulkList<Integer> collection1 = BulkList.New(1,2,3);
      BulkList<Integer> collection2 = BulkList.New(2,3,4);
      BulkList<Integer> union = collection1.union(collection2, Equalator.identity(), BulkList.New());

      Results in union containing 1, 2, 3 and 4.

      Specified by:
      union in interface XGettingCollection<E>
      Parameters:
      other - collection to build a union with.
      equalator - which is used for the equal-tests.
      target - on which the Consumer.accept(Object) is called for all unified elements.
      Returns:
      Given target
    • hashCode

      @Deprecated public int hashCode()
      Deprecated.
      Specified by:
      hashCode in interface XGettingCollection<E>
      Overrides:
      hashCode in class Object
    • equals

      @Deprecated public boolean equals​(Object o)
      Deprecated.
      Description copied from interface: XGettingCollection
      Performs an equality comparison according to the specification in Collection.

      Note that it is this interface's author opinion that the whole concept of equals() in standard Java, especially in the collection implementations, is flawed.
      The reason is because all different kinds of comparison types that actually depend on the situation have to be mixed up in a harcoded fashion in one method, from identity comparison over data indentity comparison to content comparison.
      In order to get the right behavior in every situation, one has to distinct between different types of equality

      This means several things:
      1.) You can't just say for example an ArrayList is the "same" as a LinkedList just because they contain the same content.
      There are different implementations for a good reason, so you have to distinct them when comparing. There are simple code examples which create massive misbehavior that will catastrophically ruin the runtime behavior of a programm due to this error in Java / JDK / Sun / whatever.
      2.) You can't always determine equality of two collections by determining equality of each element as Collection defines it.

      As a conclusion: don't use this method!
      Be clear what type of comparison you really need, then use one of the following methods and proper comparators:
      XGettingCollection.equals(XGettingCollection, Equalator)
      XGettingCollection.equalsContent(XGettingCollection, Equalator)

      Specified by:
      equals in interface XGettingCollection<E>
      Overrides:
      equals in class Object
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Sized
    • size

      public long size()
      Specified by:
      size in interface Sized
      Specified by:
      size in interface XGettingCollection<E>
    • maximumCapacity

      public long maximumCapacity()
      Description copied from interface: CapacityCarrying
      Returns the maximum amount of elements this carrier instance can contain.
      The actual value may be depend on the configuration of the concrete instance or may depend only on the implementation of the carrier (meaning it is constant for all instances of the implementation, e.g. Integer.MAX_VALUE)
      Specified by:
      maximumCapacity in interface CapacityCarrying
      Returns:
      the maximum amount of elements this carrier instance can contain.
    • isFull

      public boolean isFull()
      Description copied from interface: CapacityCarrying
      Returns true if the current capacity cannot be increased any more.
      Specified by:
      isFull in interface CapacityCarrying
    • remainingCapacity

      public long remainingCapacity()
      Description copied from interface: CapacityCarrying
      Returns the amount of elements this carrier instance can collect before reaching its maximimum capacity.
      Specified by:
      remainingCapacity in interface CapacityCarrying
    • toArray

      public Object[] toArray()
      Description copied from interface: XGettingCollection
      Returns an array containing all of the elements in this collection.

      The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

      This method acts as bridge between MicroStream-based collections and Java-native-based APIs.

      Specified by:
      toArray in interface XGettingCollection<E>
      Returns:
      an array containing all of the elements in this collection
    • immure

      public XImmutableSet<E> immure()
      Description copied from interface: XGettingCollection
      Provides an instance of an immutable collection type with equal behavior and data as this instance.

      If this instance already is of an immutable collection type, it returns itself.

      Specified by:
      immure in interface XGettingCollection<E>
      Specified by:
      immure in interface XGettingSet<E>
      Returns:
      an immutable copy of this collection instance.
    • old

      public SetView.OldSetView<E> old()
      Specified by:
      old in interface XGettingCollection<E>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface XGettingCollection<E>
    • view

      public SetView<E> view()
      Description copied from interface: XGettingCollection
      Creates a view of this collection and returns it. It is a read-only collection, which wraps around this collection and only allows read methods.

      A view is different from immutable collection (XGettingCollection.immure()) in the way, that changes in this collection are still affecting the view. The immutable collection on the other hand has no reference to this collection and changes therefore do not affect the immutable collection.

      Specified by:
      view in interface XGettingCollection<E>
      Returns:
      new read-only collection to view this collection