Class AbstractMultiset<T>

java.lang.Object
org.javimmutable.collections.common.AbstractMultiset<T>
All Implemented Interfaces:
Serializable, Iterable<T>, ICollection<T>, IMultiset<T>, InvariantCheckable, ISet<T>, IStreamable<T>, Mapped<T,T>, SplitableIterable<T>
Direct Known Subclasses:
HashMultiset, OrderedMultiset, TreeMultiset

@Immutable public abstract class AbstractMultiset<T> extends Object implements IMultiset<T>
See Also:
  • Field Details

    • map

      protected final IMap<T,Integer> map
    • occurrences

      protected final int occurrences
  • Constructor Details

    • AbstractMultiset

      protected AbstractMultiset(IMap<T,Integer> map, int occurrences)
  • Method Details

    • insert

      @Nonnull public IMultiset<T> insert(@Nonnull T value)
      Description copied from interface: IMultiset
      Adds one occurrence of value to the multiset.
      Specified by:
      insert in interface ICollection<T>
      Specified by:
      insert in interface IMultiset<T>
      Specified by:
      insert in interface ISet<T>
      Parameters:
      value - value to add
      Returns:
      new multiset reflecting the change
    • insert

      @Nonnull public IMultiset<T> insert(@Nonnull T value, int count)
      Description copied from interface: IMultiset
      Adds count number of occurrences of value to the multiset. Count must be greater than or equal to zero.
      Specified by:
      insert in interface IMultiset<T>
      Parameters:
      value - value to add
      count - number of occurrences added
      Returns:
      new multiset reflecting the change
    • contains

      public boolean contains(@Nullable T value)
      Description copied from interface: IMultiset
      Determines if the multiset contains at least one occurrence of the specified value
      Specified by:
      contains in interface IMultiset<T>
      Specified by:
      contains in interface ISet<T>
      Parameters:
      value - value to check for
      Returns:
      true if this multiset contains the value
    • containsAtLeast

      public boolean containsAtLeast(@Nullable T value, int count)
      Description copied from interface: IMultiset
      Determines if the multiset contains at least count occurrences of value.
      Specified by:
      containsAtLeast in interface IMultiset<T>
      Parameters:
      value - value to check for
      count - number of occurrences checked for
      Returns:
      true if thsi multiset contains at least count occurrences of value
    • containsAll

      public boolean containsAll(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Determines if the multiset contains every value (but not necessarily the same number of occurrences of each value) in other. If other is empty, returns true. Synonymous to calling contains() on each element of other.
      Specified by:
      containsAll in interface IMultiset<T>
      Specified by:
      containsAll in interface ISet<T>
      Parameters:
      other - contains values to be checked for
      Returns:
      true if this multiset contains all values
    • containsAll

      public boolean containsAll(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Determines if the multiset contains every value (but not necessarily the same number of occurrences of each value) in other. If other is empty, returns true. Synonymous to calling contains() on each element of other.
      Specified by:
      containsAll in interface IMultiset<T>
      Specified by:
      containsAll in interface ISet<T>
      Parameters:
      other - contains values to be checked for
      Returns:
      true if this multiset contains all values
    • containsAllOccurrences

      public boolean containsAllOccurrences(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Determines if the multiset contains every occurrence of all the values in other. If other is empty, returns true. Slow operation.
      Specified by:
      containsAllOccurrences in interface IMultiset<T>
      Parameters:
      other - contains values to be checked for
      Returns:
      true if this multiset contains all occurrences
    • containsAllOccurrences

      public boolean containsAllOccurrences(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Determines if the multiset contains every occurrence of all the values in other. If other is empty, returns true. Slow operation.
      Specified by:
      containsAllOccurrences in interface IMultiset<T>
      Parameters:
      other - contains values to be checked for
      Returns:
      true if this multiset contains all occurrences
    • containsAllOccurrences

      public boolean containsAllOccurrences(@Nonnull IMultiset<? extends T> values)
      Description copied from interface: IMultiset
      Determines if the multiset contains every occurrence of all the values in other. If other is empty, returns true.
      Specified by:
      containsAllOccurrences in interface IMultiset<T>
      Parameters:
      values - contains values to be checked for
      Returns:
      true if this multiset contains all occurrences
    • containsAny

      public boolean containsAny(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Determines if the multiset and other have at least one value in common. Returns false if other is empty.
      Specified by:
      containsAny in interface IMultiset<T>
      Specified by:
      containsAny in interface ISet<T>
      Parameters:
      other - contains values to be checked for
      Returns:
      true if this multiset contains a value
    • containsAny

      public boolean containsAny(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Determines if the multiset and other have at least one value in common. Returns false if other is empty.
      Specified by:
      containsAny in interface IMultiset<T>
      Specified by:
      containsAny in interface ISet<T>
      Parameters:
      other - contains values to be checked for
      Returns:
      true if this multiset contains a value
    • delete

      @Nonnull public IMultiset<T> delete(@Nonnull T value)
      Description copied from interface: IMultiset
      Removes every occurrence of value from the multiset.
      Specified by:
      delete in interface IMultiset<T>
      Specified by:
      delete in interface ISet<T>
      Parameters:
      value - value to remove
      Returns:
      new multiset reflecting the change
    • deleteOccurrence

      @Nonnull public IMultiset<T> deleteOccurrence(@Nonnull T value)
      Description copied from interface: IMultiset
      Removes one occurrence of value from the multiset.
      Specified by:
      deleteOccurrence in interface IMultiset<T>
      Parameters:
      value - value to remove
      Returns:
      new multiset reflecting the change
    • deleteOccurrence

      @Nonnull public IMultiset<T> deleteOccurrence(@Nonnull T value, int subtractBy)
      Description copied from interface: IMultiset
      Removes count number of occurrences of value from the multiset. If there are fewer than count occurrences in the multimap, then all the occurrences that do exist are deleted. Count must be greater than or equal to zero.
      Specified by:
      deleteOccurrence in interface IMultiset<T>
      Parameters:
      value - value to remove
      subtractBy - number of occurrences to remove
      Returns:
      new multiset reflecting the change
    • deleteAll

      @Nonnull public IMultiset<T> deleteAll(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Removes all occurrences of each value in other from the multiset. Synonymous to calling delete() on each element of other.
      Specified by:
      deleteAll in interface IMultiset<T>
      Specified by:
      deleteAll in interface ISet<T>
      Parameters:
      other - contains values to delete
      Returns:
      new multiset reflecting the change
    • deleteAll

      @Nonnull public IMultiset<T> deleteAll(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Removes all occurrences of each value in other from the multiset. Synonymous to calling delete() on each element of other.
      Specified by:
      deleteAll in interface IMultiset<T>
      Specified by:
      deleteAll in interface ISet<T>
      Parameters:
      other - contains values to delete
      Returns:
      new multiset reflecting the change
    • deleteAllOccurrences

      @Nonnull public IMultiset<T> deleteAllOccurrences(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Removes each occurrence in other from the multiset. Synonymous to calling deleteOccurrence() on each element of other.
      Specified by:
      deleteAllOccurrences in interface IMultiset<T>
      Parameters:
      other - contains occurrences to delete
      Returns:
      new multiset reflecting the change
    • deleteAllOccurrences

      @Nonnull public IMultiset<T> deleteAllOccurrences(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Removes each occurrence in other from the multiset. Synonymous to calling deleteOccurrence() on each element of other.
      Specified by:
      deleteAllOccurrences in interface IMultiset<T>
      Parameters:
      other - contains occurrences to delete
      Returns:
      new multiset reflecting the change
    • deleteAllOccurrences

      @Nonnull public IMultiset<T> deleteAllOccurrences(@Nonnull IMultiset<? extends T> other)
      Description copied from interface: IMultiset
      Removes each occurrence in other from the multiset. Synonymous to calling deleteOccurrence() on each element of other.
      Specified by:
      deleteAllOccurrences in interface IMultiset<T>
      Parameters:
      other - contains occurrences to delete
      Returns:
      new multiset reflecting the change
    • insertAll

      @Nonnull public IMultiset<T> insertAll(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IMultiset
      Adds each occurrence in values to the multiset. Synonymous to calling insert on each element of values.
      Specified by:
      insertAll in interface ICollection<T>
      Specified by:
      insertAll in interface IMultiset<T>
      Specified by:
      insertAll in interface ISet<T>
      Parameters:
      values - contains occurrences to be added
      Returns:
      new multiset reflecting the change
    • insertAll

      @Nonnull public IMultiset<T> insertAll(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Adds each occurrence in values to the multiset. Synonymous to calling insert on each element of values.
      Specified by:
      insertAll in interface ICollection<T>
      Specified by:
      insertAll in interface IMultiset<T>
      Specified by:
      insertAll in interface ISet<T>
      Parameters:
      other - contains occurrences to be added
      Returns:
      new multiset reflecting the change
    • insertAll

      @Nonnull public IMultiset<T> insertAll(@Nonnull IMultiset<? extends T> values)
      Description copied from interface: IMultiset
      Adds each occurrence in values to the multiset. Synonymous to calling insert on each element of values.
      Specified by:
      insertAll in interface IMultiset<T>
      Parameters:
      values - contains occurrences to be added
      Returns:
      new multiset reflecting the change
    • union

      @Nonnull public IMultiset<T> union(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Combines all occurrences from other and the multiset. If only the multiset or other contains a value, that value's count is preserved in the new multiset. If both contain a value, the greater count is used. Slow operation.
      Specified by:
      union in interface IMultiset<T>
      Specified by:
      union in interface ISet<T>
      Parameters:
      other - contains values to union with this multiset
      Returns:
      new multiset reflecting the changes
    • union

      @Nonnull public IMultiset<T> union(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Combines all occurrences from other and the multiset. If only the multiset or other contains a value, that value's count is preserved in the new multiset. If both contain a value, the greater count is used. Slow operation.
      Specified by:
      union in interface IMultiset<T>
      Specified by:
      union in interface ISet<T>
      Parameters:
      other - contains values to union with this multiset
      Returns:
      new multiset reflecting the changes
    • union

      @Nonnull public IMultiset<T> union(@Nonnull IMultiset<? extends T> other)
      Description copied from interface: IMultiset
      Combines all occurrences from other and the multiset. If only the multiset or other contains a value, that value's count is preserved in the new multiset. If both contain a value, the greater count is used.
      Specified by:
      union in interface IMultiset<T>
      Parameters:
      other - contains values to union with this multiset
      Returns:
      new multiset reflecting the changes
    • intersection

      @Nonnull public IMultiset<T> intersection(@Nonnull Iterable<? extends T> other)
      Description copied from interface: IMultiset
      Creates a new multiset with the occurrences that are in both other and this. If neither the multiset nor other contains a value, it is removed. If both contain a value, the lesser count is used. Slow operation.
      Specified by:
      intersection in interface IMultiset<T>
      Specified by:
      intersection in interface ISet<T>
      Parameters:
      other - contains values to intersect with this multiset
      Returns:
      new multiset reflecting the change
    • intersection

      @Nonnull public IMultiset<T> intersection(@Nonnull Iterator<? extends T> other)
      Description copied from interface: IMultiset
      Creates a new multiset with the occurrences that are in both other and this. If neither the multiset nor other contains a value, it is removed. If both contain a value, the lesser count is used. Slow operation.
      Specified by:
      intersection in interface IMultiset<T>
      Specified by:
      intersection in interface ISet<T>
      Parameters:
      other - contains values to intersect with this multiset
      Returns:
      new multiset reflecting the change
    • intersection

      @Nonnull public IMultiset<T> intersection(@Nonnull IMultiset<? extends T> other)
      Description copied from interface: IMultiset
      Creates a new multiset with the occurrences that are in both other and this. If neither the multiset nor other contains a value, it is removed. If both contain a value, the lesser count is used.
      Specified by:
      intersection in interface IMultiset<T>
      Parameters:
      other - contains values to intersect with this multiset
      Returns:
      new multiset reflecting the change
    • intersection

      @Nonnull public IMultiset<T> intersection(@Nonnull ISet<? extends T> other)
      Description copied from interface: IMultiset
      Creates a new multiset with the occurrences that are in both other and this. If neither the multiset nor other contains a value, it is removed. If both contain a value, the lesser count is used.
      Specified by:
      intersection in interface IMultiset<T>
      Specified by:
      intersection in interface ISet<T>
      Parameters:
      other - contains values to intersect with this multiset
      Returns:
      new multiset reflecting the change
    • intersection

      @Nonnull public IMultiset<T> intersection(@Nonnull Set<? extends T> other)
      Description copied from interface: IMultiset
      Creates a new multiset with the occurrences that are in both other and this. If neither the multiset nor other contains a value, it is removed. If both contain a value, the lesser count is used.
      Specified by:
      intersection in interface IMultiset<T>
      Specified by:
      intersection in interface ISet<T>
      Parameters:
      other - contains values to intersect with this multiset
      Returns:
      new multiset reflecting the change
    • count

      public int count(@Nonnull T value)
      Description copied from interface: IMultiset
      Returns the number of occurrences associated with the specified value. If the value is not contained in the multiset, 0 is returned.
      Specified by:
      count in interface IMultiset<T>
      Parameters:
      value - value to check for
      Returns:
      number of occurrences
    • setCount

      @Nonnull public IMultiset<T> setCount(@Nonnull T value, int count)
      Description copied from interface: IMultiset
      Manually sets the number of occurrences associated with the specified value. Count must be greater than or equal to zero. If count == 0, is synonymous to delete(value). If value is not currently in the multiset, is equivalent to insert(value, count).
      Specified by:
      setCount in interface IMultiset<T>
      Parameters:
      value - value to add
      count - new number of occurrences for value
      Returns:
      new multiset reflecting the change
    • create

      protected abstract IMultiset<T> create(IMap<T,Integer> map, int occurrences)
      Implemented by derived classes to create a new instance of the appropriate class.
      Parameters:
      map - base map for new multiset
      occurrences - total occurrences in map
      Returns:
      new multiset built from map
    • emptyMutableMap

      protected abstract Map<T,Integer> emptyMutableMap()
      Implemented by derived classes to create a new empty mutable Map that operates in the same way as this multiset's underlying immutable Map.
      Returns:
      new empty Counter
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface ICollection<T>
      Returns:
      true only if collection contains no values
    • size

      public int size()
      Specified by:
      size in interface ICollection<T>
      Specified by:
      size in interface IMultiset<T>
      Returns:
      total number of unique values in the multiset. Same as the number of items in iterator() and entries().iterator().
    • occurrenceCount

      public int occurrenceCount()
      Specified by:
      occurrenceCount in interface IMultiset<T>
      Returns:
      the total number of occurrences in the multiset. Same as the number of items in occurrences.iterator().
    • getSet

      @Nonnull public Set<T> getSet()
      Specified by:
      getSet in interface ISet<T>
      Returns:
      an unmodifiable Set implementation backed by this set.
    • iterator

      @Nonnull public SplitableIterator<T> iterator()
      Description copied from interface: IStreamable
      Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator. This is necessary to allow composition of new objects from methods like keys() and values().
      Specified by:
      iterator in interface IStreamable<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface SplitableIterable<T>
    • getSpliteratorCharacteristics

      public int getSpliteratorCharacteristics()
      Specified by:
      getSpliteratorCharacteristics in interface IStreamable<T>
      Returns:
      characteristics value used when creating Spliterators
    • entries

      @Nonnull public IStreamable<IMapEntry<T,Integer>> entries()
      Description copied from interface: IMultiset
      Stream iterates through each Entry, that contains a unique value and the count of occurrences.
      Specified by:
      entries in interface IMultiset<T>
      Returns:
      IStreamable of IMap.Entries
    • occurrences

      @Nonnull public IStreamable<T> occurrences()
      Description copied from interface: IMultiset
      IStreamable that iterates through each occurrence in the multiset the correct number of times.
      Specified by:
      occurrences in interface IMultiset<T>
      Returns:
      IStreamable that behaves as if multiset was a list
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • checkInvariants

      public void checkInvariants()
      Description copied from interface: InvariantCheckable
      Checks invariants of implementing class.
      Specified by:
      checkInvariants in interface InvariantCheckable