Interface Counter<T>

    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default Counter<T> adjustValues​(DoubleUnaryOperator function)
      Constructs a new counter made up of counts that are adjusted using the supplied function.
      Counter<T> adjustValuesSelf​(DoubleUnaryOperator function)
      Adjust the values in-place using the supplied function
      Map<T,​Double> asMap()
      Provides a map view of this counter
      default double average()
      Calculates the average of the values in the counter
      Counter<T> bottomN​(int n)
      Creates a new counter containing the N items with lowest values
      void clear()
      Clears the counter
      boolean contains​(T item)
      Determines if the item is in the counter
      default Counter<T> decrement​(T item)
      Decrements the count of the item by one.
      default Counter<T> decrement​(T item, double amount)
      Decrements the count of the item by a given amount
      default Counter<T> decrementAll​(Iterable<? extends T> iterable)
      Decrements all items in a given iterable by 1
      default Counter<T> decrementAll​(Iterable<? extends T> iterable, double amount)
      Decrements all items in a given iterable by a given amount
      default Counter<T> divideBySum()
      Divides the values in the counter by the sum and sets the sum to 1.0
      Set<Map.Entry<T,​Double>> entries()
      A set of object - double entries making up the counter
      Counter<T> filterByKey​(Predicate<? super T> predicate)
      Creates a new counter containing only those items that evaluate true for the given predicate
      Counter<T> filterByValue​(DoublePredicate doublePredicate)
      Creates a new counter containing only those items whose value evaluate true for the given predicate
      default void forEach​(BiConsumer<? super T,​? super Double> consumer)
      Convenience method for consuming item - count pairs.
      double get​(T item)
      Returns the value for the given item
      default Counter<T> increment​(T item)
      Increments the count of the item by one.
      Counter<T> increment​(T item, double amount)
      Increments the count of the item by a given amount
      default Counter<T> incrementAll​(Iterable<? extends T> iterable)
      Increments all items in a given iterable by 1
      default Counter<T> incrementAll​(Iterable<? extends T> iterable, double amount)
      Increments all items in a given iterable by a given amount
      boolean isEmpty()
      Determines if the counter is empty or not
      Set<T> items()
      The items in the counter
      default List<T> itemsByCount​(boolean ascending)
      Returns the items as a sorted list by their counts.
      default double magnitude()
      Calculates the magnitude (square root of sum of squares) of the items in the Counter.
      <R> Counter<R> mapKeys​(Function<? super T,​? extends R> function)
      Creates a new counter by mapping the items of this counter using the supplied function
      default T max()
      Determines the item with the maximum value in the counter
      default double maximumCount()
      Determines the maximum value in the counter
      Counter<T> merge​(Counter<? extends T> other)
      Merges the counts in one counter with this one.
      Counter<T> merge​(Map<? extends T,​? extends Number> other)
      Merges the counts in a map with this counter.
      default T min()
      Determines the item with the minimum value in the counter
      default double minimumCount()
      Determines the minimum value in the counter
      double remove​(T item)
      Removes an item from the counter
      Counter<T> removeAll​(Iterable<T> items)
      Removes all the given items from the counter
      default T sample()
      Sample an item based on its count.
      default T sample​(Random rnd)
      Sample an item based on its count.
      Counter<T> set​(T item, double count)
      Sets the value of an item in the counter
      int size()
      The total number of items in the counter
      default double standardDeviation()
      Calculates the standard deviation of the items in the counter
      double sum()
      The sum of values in the counter
      Counter<T> topN​(int n)
      Creates a new counter containing the N items with highest values
      Collection<Double> values()
      The values associated with the items in the counter
      default void writeCsv​(Resource output)
      Writes the counter items and values to CSV
    • Method Detail

      • adjustValues

        default Counter<T> adjustValues​(DoubleUnaryOperator function)
        Constructs a new counter made up of counts that are adjusted using the supplied function.
        Parameters:
        function - The function to use to adjust the counts
        Returns:
        The new counter with adjusted counts.
      • adjustValuesSelf

        Counter<T> adjustValuesSelf​(DoubleUnaryOperator function)
        Adjust the values in-place using the supplied function
        Parameters:
        function - The function to use to adjust the counts
        Returns:
        this counter
      • asMap

        Map<T,​Double> asMap()
        Provides a map view of this counter
        Returns:
        The counter as a Map
      • average

        default double average()
        Calculates the average of the values in the counter
        Returns:
        The average count in the counter
      • bottomN

        Counter<T> bottomN​(int n)
        Creates a new counter containing the N items with lowest values
        Parameters:
        n - the number of items desired
        Returns:
        a counter containing the N items with lowest values from this counter
      • clear

        void clear()
        Clears the counter
      • contains

        boolean contains​(T item)
        Determines if the item is in the counter
        Parameters:
        item - item to check
        Returns:
        True if item is in the counter, false otherwise
      • decrement

        default Counter<T> decrement​(T item)
        Decrements the count of the item by one.
        Parameters:
        item - The item to increment
        Returns:
        the counter
      • decrement

        default Counter<T> decrement​(T item,
                                     double amount)
        Decrements the count of the item by a given amount
        Parameters:
        item - The item to increment
        amount - The amount to decrement
        Returns:
        the counter
      • decrementAll

        default Counter<T> decrementAll​(Iterable<? extends T> iterable)
        Decrements all items in a given iterable by 1
        Parameters:
        iterable - The iterable of items to decrement
        Returns:
        the counter
      • decrementAll

        default Counter<T> decrementAll​(Iterable<? extends T> iterable,
                                        double amount)
        Decrements all items in a given iterable by a given amount
        Parameters:
        iterable - The iterable of items to decrement
        amount - The amount to decrement
        Returns:
        the counter
      • divideBySum

        default Counter<T> divideBySum()
        Divides the values in the counter by the sum and sets the sum to 1.0
        Returns:
        the counter
      • entries

        Set<Map.Entry<T,​Double>> entries()
        A set of object - double entries making up the counter
        Returns:
        the set of entries
      • filterByKey

        Counter<T> filterByKey​(Predicate<? super T> predicate)
        Creates a new counter containing only those items that evaluate true for the given predicate
        Parameters:
        predicate - the predicate to use to filter the keys
        Returns:
        A new counter containing only those items that evaluate true for the given predicate
      • filterByValue

        Counter<T> filterByValue​(DoublePredicate doublePredicate)
        Creates a new counter containing only those items whose value evaluate true for the given predicate
        Parameters:
        doublePredicate - the predicate to use to filter the values
        Returns:
        A new counter containing only those items whose value evaluate true for the given predicate
      • forEach

        default void forEach​(BiConsumer<? super T,​? super Double> consumer)
        Convenience method for consuming item - count pairs.
        Parameters:
        consumer - the consumer to use for processing the key value pairs
      • get

        double get​(T item)
        Returns the value for the given item
        Parameters:
        item - The item we want the count for
        Returns:
        The value of the item or 0 if it is not in the counter.
      • increment

        default Counter<T> increment​(T item)
        Increments the count of the item by one.
        Parameters:
        item - The item to increment
        Returns:
        the counter
      • increment

        Counter<T> increment​(T item,
                             double amount)
        Increments the count of the item by a given amount
        Parameters:
        item - The item to increment
        amount - The amount to increment
        Returns:
        the counter
      • incrementAll

        default Counter<T> incrementAll​(Iterable<? extends T> iterable)
        Increments all items in a given iterable by 1
        Parameters:
        iterable - The iterable of items to increment
        Returns:
        the counter
      • incrementAll

        default Counter<T> incrementAll​(Iterable<? extends T> iterable,
                                        double amount)
        Increments all items in a given iterable by a given amount
        Parameters:
        iterable - The iterable of items to increment
        amount - The amount to increment
        Returns:
        the counter
      • isEmpty

        boolean isEmpty()
        Determines if the counter is empty or not
        Returns:
        True if the counter is empty
      • items

        Set<T> items()
        The items in the counter
        Returns:
        The items in the counter
      • itemsByCount

        default List<T> itemsByCount​(boolean ascending)
        Returns the items as a sorted list by their counts.
        Parameters:
        ascending - True if the counts are sorted in ascending order, False if in descending order.
        Returns:
        The sorted list of items.
      • magnitude

        default double magnitude()
        Calculates the magnitude (square root of sum of squares) of the items in the Counter.
        Returns:
        the magnitude
      • mapKeys

        <R> Counter<R> mapKeys​(Function<? super T,​? extends R> function)
        Creates a new counter by mapping the items of this counter using the supplied function
        Type Parameters:
        R - The component type of the new counter
        Parameters:
        function - the function to use to transform the keys
        Returns:
        A new counter containing only those items that evaluate true for the given predicate
      • max

        default T max()
        Determines the item with the maximum value in the counter
        Returns:
        The item with max count
      • maximumCount

        default double maximumCount()
        Determines the maximum value in the counter
        Returns:
        The maximum count in the counter
      • merge

        Counter<T> merge​(Counter<? extends T> other)
        Merges the counts in one counter with this one.
        Parameters:
        other - The other counter to merge.
        Returns:
        the counter
      • merge

        Counter<T> merge​(Map<? extends T,​? extends Number> other)
        Merges the counts in a map with this counter.
        Parameters:
        other - The other counter to merge.
        Returns:
        the counter
      • min

        default T min()
        Determines the item with the minimum value in the counter
        Returns:
        The item with min count
      • minimumCount

        default double minimumCount()
        Determines the minimum value in the counter
        Returns:
        The minimum count in the counter
      • remove

        double remove​(T item)
        Removes an item from the counter
        Parameters:
        item - The item to remove
        Returns:
        the count of the removed item
      • removeAll

        Counter<T> removeAll​(Iterable<T> items)
        Removes all the given items from the counter
        Parameters:
        items - The items to remove
        Returns:
        the counter
      • sample

        default T sample​(Random rnd)
        Sample an item based on its count.
        Parameters:
        rnd - The random number generator
        Returns:
        the sampled item
      • sample

        default T sample()
        Sample an item based on its count.
        Returns:
        the sampled item
      • set

        Counter<T> set​(T item,
                       double count)
        Sets the value of an item in the counter
        Parameters:
        item - The item
        count - The count
        Returns:
        the counter
      • size

        int size()
        The total number of items in the counter
        Returns:
        The number of items in the counter
      • standardDeviation

        default double standardDeviation()
        Calculates the standard deviation of the items in the counter
        Returns:
        The standard deviation of the counts in the counter
      • sum

        double sum()
        The sum of values in the counter
        Returns:
        The sum of the counts in the counter
      • topN

        Counter<T> topN​(int n)
        Creates a new counter containing the N items with highest values
        Parameters:
        n - the number of items desired
        Returns:
        a counter containing the N items with highest values from this counter
      • values

        Collection<Double> values()
        The values associated with the items in the counter
        Returns:
        The values of the items in the counter.
      • writeCsv

        default void writeCsv​(Resource output)
                       throws IOException
        Writes the counter items and values to CSV
        Parameters:
        output - the resource to write to
        Throws:
        IOException - Something went wrong writing