de.caff.generics

## Class CountingTreeSet<T>

- java.lang.Object
- - de.caff.generics.AbstractBasicCountingSet<T>
  - - de.caff.generics.CountingTreeSet<T>

- All Implemented Interfaces:
    CountingSet<T>, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>, java.util.SortedSet<T>

      public class CountingTreeSet<T>
      extends AbstractBasicCountingSet<T>
      implements java.util.SortedSet<T>

  A sorted set which also counts how often any element was added.

  Because it would be breaking the set semantics removal is not allowed. All removal methods but `AbstractBasicCountingSet.clear()` therefore will throw an `UnsupportedOperationException`.

  The methods which return subsets will return standard sets, not members of this class.

  Author:
    Rammi

- - ### Field Summary

    - ### Fields inherited from class de.caff.generics.AbstractBasicCountingSet

      `creator, map`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CountingTreeSet()` Deprecated. use `create()` instead because it handles comparability correctly
    `CountingTreeSet(java.util.Comparator<? super T> comparator)` Constructor.
    `CountingTreeSet(java.util.Comparator<? super T> comparator, java.util.function.Supplier<Counter> creator)` Constructor.
    `CountingTreeSet(java.util.function.Supplier<Counter> creator)` Deprecated. use `create(Supplier)` instead because it handles comparability correctly

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.util.Comparator<? super T>` | `comparator()` Returns the comparator associated with this sorted set, or `null` if it uses its elements' natural ordering.
    `<TT extends java.lang.Comparable<TT>> CountingTreeSet<TT>` | `create()` Create a new simple counting tree set.
    `<TT extends java.lang.Comparable<T>> CountingTreeSet<TT>` | `create(java.util.function.Supplier<Counter> creator)` Create a new non-threadsafe counting tree set.
    `<TT extends java.lang.Comparable<T>> CountingTreeSet<TT>` | `createThreadSafe(java.util.function.Supplier<Counter> creator)` Create a new non-threadsafe counting tree set.
    `T` | `first()` Returns the first (lowest) element currently in this sorted set.
    `java.util.SortedSet<T>` | `headSet(T toElement)` Returns a view of the portion of this sorted set whose elements are strictly less than `toElement`.
    `T` | `last()` Returns the last (highest) element currently in this sorted set.
    `java.util.SortedSet<T>` | `subSet(T fromElement, T toElement)` Returns a view of the portion of this sorted set whose elements range from `fromElement`, inclusive, to `toElement`, exclusive.
    `java.util.SortedSet<T>` | `tailSet(T fromElement)` Returns a view of the portion of this sorted set whose elements are greater than or equal to `fromElement`.

    - ### Methods inherited from class de.caff.generics.AbstractBasicCountingSet

      `add, addAll, clear, contains, containsAll, getCount, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.SortedSet

      `spliterator`

    - ### Methods inherited from interface java.util.Set

      `add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray`

    - ### Methods inherited from interface java.util.Collection

      `parallelStream, removeIf, stream`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach`

- - ### Constructor Detail

    - #### CountingTreeSet

          @Deprecated
          public CountingTreeSet(@NotNull
                                             java.util.function.Supplier<Counter> creator)

      Deprecated. use `create(Supplier)` instead because it handles comparability correctly
      Constructor.

      This constructor silently assumes that T is Comparable!

      Parameters:
        `creator` - counter creator used for counter creation, see `Counter.SIMPLE` or `Counter.THREAD_SAFE` for standard ones

    - #### CountingTreeSet

          public CountingTreeSet(@NotNull
                                 java.util.Comparator<? super T> comparator,
                                 @NotNull
                                 java.util.function.Supplier<Counter> creator)

      Constructor.
      Parameters:
        `comparator` - comparator for element comparision
        `creator` - counter creator used for counter creation, see `Counter.SIMPLE` or `Counter.THREAD_SAFE` for standard ones

    - #### CountingTreeSet

          @Deprecated
          public CountingTreeSet()

      Deprecated. use `create()` instead because it handles comparability correctly
      Constructor.

      This will use `simple counters`.

      This constructor silently assumes that T is Comparable!

    - #### CountingTreeSet

          public CountingTreeSet(@NotNull
                                 java.util.Comparator<? super T> comparator)

      Constructor.

      This will use `simple counters`.

      Parameters:
        `comparator` - comparator for element comparison

  - ### Method Detail

    - #### create

          @NotNull
          public <TT extends java.lang.Comparable<T>> CountingTreeSet<TT> create(@NotNull
                                                                                          java.util.function.Supplier<Counter> creator)

      Create a new non-threadsafe counting tree set.
      Type Parameters:
        `TT` - type of set keys
      Parameters:
        `creator` - creator for new elements
      Returns:
        counting tree set

    - #### createThreadSafe

          @NotNull
          public <TT extends java.lang.Comparable<T>> CountingTreeSet<TT> createThreadSafe(@NotNull
                                                                                                    java.util.function.Supplier<Counter> creator)

      Create a new non-threadsafe counting tree set.
      Type Parameters:
        `TT` - type of set keys
      Parameters:
        `creator` - creator for new elements
      Returns:
        counting tree set

    - #### create

          @NotNull
          public <TT extends java.lang.Comparable<TT>> CountingTreeSet<TT> create()

      Create a new simple counting tree set.
      Type Parameters:
        `TT` - type of tree set
      Returns:
        counting tree set

    - #### comparator

          public java.util.Comparator<? super T> comparator()

      Returns the comparator associated with this sorted set, or `null` if it uses its elements' natural ordering.
      Specified by:
        `comparator` in interface `java.util.SortedSet<T>`
      Returns:
        the comparator associated with this sorted set, or `null` if it uses its elements' natural ordering.

    - #### subSet

          @NotNull
          public java.util.SortedSet<T> subSet(T fromElement,
                                                        T toElement)

      Returns a view of the portion of this sorted set whose elements range from `fromElement`, inclusive, to `toElement`, exclusive. (If `fromElement` and `toElement` are equal, the returned sorted set is empty.) The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations that this sorted set supports.

      The sorted set returned by this method will throw an `IllegalArgumentException` if the user attempts to insert a element outside the specified range.

      Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the element type allows for calculation of the successor a given value, merely request the subrange from `lowEndpoint` to `successor(highEndpoint)`. For example, suppose that `s` is a sorted set of strings. The following idiom obtains a view containing all of the strings in `s` from `low` to `high`, inclusive:

           SortedSet sub = s.subSet(low, high+"\0");

      A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the Strings in `s` from `low` to `high`, exclusive:

           SortedSet sub = s.subSet(low+"\0", high);

      Specified by:
        `subSet` in interface `java.util.SortedSet<T>`
      Parameters:
        `fromElement` - low endpoint (inclusive) of the subSet.
        `toElement` - high endpoint (exclusive) of the subSet.
      Returns:
        a view of the specified range within this sorted set.
      Throws:
        `java.lang.ClassCastException` - if `fromElement` and `toElement` cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if `fromElement` or `toElement` cannot be compared to elements currently in the set.
        `java.lang.IllegalArgumentException` - if `fromElement` is greater than `toElement`; or if this set is itself a subSet, headSet, or tailSet, and `fromElement` or `toElement` are not within the specified range of the subSet, headSet, or tailSet.
        `java.lang.NullPointerException` - if `fromElement` or `toElement` is `null` and this sorted set does not tolerate `null` elements.

    - #### headSet

          @NotNull
          public java.util.SortedSet<T> headSet(T toElement)

      Returns a view of the portion of this sorted set whose elements are strictly less than `toElement`. The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations.

      The sorted set returned by this method will throw an `IllegalArgumentException` if the user attempts to insert a element outside the specified range.

      Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a headSet bounded by `successor(highEndpoint)`. For example, suppose that `s` is a sorted set of strings. The following idiom obtains a view containing all of the strings in `s` that are less than or equal to `high`:

              SortedSet head = s.headSet(high+"\0");

      Specified by:
        `headSet` in interface `java.util.SortedSet<T>`
      Parameters:
        `toElement` - high endpoint (exclusive) of the headSet.
      Returns:
        a view of the specified initial range of this sorted set.
      Throws:
        `java.lang.ClassCastException` - if `toElement` is not compatible with this set's comparator (or, if the set has no comparator, if `toElement` does not implement `Comparable`). Implementations may, but are not required to, throw this exception if `toElement` cannot be compared to elements currently in the set.
        `java.lang.NullPointerException` - if `toElement` is `null` and this sorted set does not tolerate `null` elements.
        `java.lang.IllegalArgumentException` - if this set is itself a subSet, headSet, or tailSet, and `toElement` is not within the specified range of the subSet, headSet, or tailSet.

    - #### tailSet

          @NotNull
          public java.util.SortedSet<T> tailSet(T fromElement)

      Returns a view of the portion of this sorted set whose elements are greater than or equal to `fromElement`. The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations.

      The sorted set returned by this method will throw an `IllegalArgumentException` if the user attempts to insert a element outside the specified range.

      Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailSet bounded by `successor(lowEndpoint)`. For example, suppose that `s` is a sorted set of strings. The following idiom obtains a view containing all of the strings in `s` that are strictly greater than `low`:

              SortedSet tail = s.tailSet(low+"\0");

      Specified by:
        `tailSet` in interface `java.util.SortedSet<T>`
      Parameters:
        `fromElement` - low endpoint (inclusive) of the tailSet.
      Returns:
        a view of the specified final range of this sorted set.
      Throws:
        `java.lang.ClassCastException` - if `fromElement` is not compatible with this set's comparator (or, if the set has no comparator, if `fromElement` does not implement `Comparable`). Implementations may, but are not required to, throw this exception if `fromElement` cannot be compared to elements currently in the set.
        `java.lang.NullPointerException` - if `fromElement` is `null` and this sorted set does not tolerate `null` elements.
        `java.lang.IllegalArgumentException` - if this set is itself a subSet, headSet, or tailSet, and `fromElement` is not within the specified range of the subSet, headSet, or tailSet.

    - #### first

          public T first()

      Returns the first (lowest) element currently in this sorted set.
      Specified by:
        `first` in interface `java.util.SortedSet<T>`
      Returns:
        the first (lowest) element currently in this sorted set.
      Throws:
        `java.util.NoSuchElementException` - sorted set is empty.

    - #### last

          public T last()

      Returns the last (highest) element currently in this sorted set.
      Specified by:
        `last` in interface `java.util.SortedSet<T>`
      Returns:
        the last (highest) element currently in this sorted set.
      Throws:
        `java.util.NoSuchElementException` - sorted set is empty.