Class Accumulator<T>

java.lang.Object
com.amalgamasimulation.utils.container.Accumulator<T>
Type Parameters:
T - a type whose instances represent different categories

public class Accumulator<T> extends Object
Accumulator represents a mapping from a set of categories to a set of numeric values.

Author:
Andrey Malykhanov, Kliment Zakharov
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Accumulator<T> otherAccumulator)
    Adds the specified accumulator to the current accumulator.
    double
    add(T element, double value)
    Increments the amount associated with element category stored in this Accumulator by the amount value.
    void
    Removes all elements from the accumulator.
    boolean
    contains(T element)
    Checks if this accumulator contains the specified element.
    boolean
     
    double
    get(T element)
    Returns value of the specified element or 0 if the accumulator does not contain the specified element.
    Creates a copy of the current accumulator.
    getDifference(Accumulator<T> otherAccumulator)
    Returns a new Accumulator containing the difference between the current Accumulator and the specified accumulator.
    Returns a list of elements contained in the current accumulator sorted in the order of adding elements to the accumulator.
    double
    getFraction(T element)
    Returns the proportion of value of the specified Accumulator element to the sum of values in the Accumulator.
    getMultiplied(double factor)
    Returns a new instance of accumulator that is equal to the current accumulator with all values multiplied by the specified factor.
    Returns a new Accumulator that contains the ratio of each value to the total sum of values in this Accumulator.
    Returns a list of element-value pairs contained in the current accumulator sorted by sequence of adding elements to the accumulator.
    double
    getSubSum(List<T> elements)
    Returns the sum of the values of the specified elements or 0 if the accumulator does not contain any of the specified elements.
    double
    Returns the sum of all values contained in this accumulator.
    getSum(Accumulator<T> otherAccumulator)
    Returns a new instance of accumulator that is sum of the current accumulator and the specified accumulator.
    int
     
    void
    multiply(double factor)
    Multiplies all values of the current accumulator by the specified factor.
    static <T> Accumulator<T>
    of(Object... pairs)
    Creates a new Accumulator instance and initializes its contents by values passed as pairs parameter.
    static <T> Accumulator<T>
    of(T cat1, double v1)
    Returns a new Accumulator containing a single mapping.
    static <T> Accumulator<T>
    of(T cat1, double v1, T cat2, double v2)
    Returns a new Accumulator containing two mappings.
    static <T> Accumulator<T>
    of(T cat1, double v1, T cat2, double v2, T cat3, double v3)
    Returns a new Accumulator containing three mappings.
    void
    remove(Accumulator<T> otherAccumulator)
    Decrements the values stored in this Accumulator by the respective amounts from the otherAccumulator.
    void
    set(T element, double value)
    Sets the specified value to the specified accumulator element.
    Returns a detailed string representation of the current accumulator and its contents.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Accumulator

      public Accumulator()
  • Method Details

    • of

      public static <T> Accumulator<T> of(Object... pairs)
      Creates a new Accumulator instance and initializes its contents by values passed as pairs parameter.
      Type Parameters:
      T - a type whose instances represent different categories
      Parameters:
      pairs - an array of the following format: category_1, amount_1, category_2, amount_2, ...
      Returns:
      a new Accumulator instance
    • of

      public static <T> Accumulator<T> of(T cat1, double v1)
      Returns a new Accumulator containing a single mapping.
      Type Parameters:
      T - the Accumulator's category type
      Parameters:
      cat1 - category
      v1 - value
      Returns:
      an Accumulator containing the specified mapping
    • of

      public static <T> Accumulator<T> of(T cat1, double v1, T cat2, double v2)
      Returns a new Accumulator containing two mappings.
      Type Parameters:
      T - the Accumulator's category type
      Parameters:
      cat1 - the first mapping's category
      v1 - the first mapping's value
      cat2 - the second mapping's category
      v2 - the second mapping's value
      Returns:
      an Accumulator containing the specified mappings
    • of

      public static <T> Accumulator<T> of(T cat1, double v1, T cat2, double v2, T cat3, double v3)
      Returns a new Accumulator containing three mappings.
      Type Parameters:
      T - the Accumulator's category type
      Parameters:
      cat1 - the first mapping's category
      v1 - the first mapping's value
      cat2 - the second mapping's category
      v2 - the second mapping's value
      cat3 - the third mapping's category
      v3 - the third mapping's value
      Returns:
      an Accumulator containing the specified mappings
    • add

      public double add(T element, double value)
      Increments the amount associated with element category stored in this Accumulator by the amount value. If this Accumulator has no value associated with the element, a new item (element, value) is added to the Accumulator.

      This is a mutator method; the current Accumulator instance is modified during the method call.

      Parameters:
      element - category
      value - amount increment
      Returns:
      the value that is the result of accumulation of existing value and the specified value for the specified element
    • add

      public void add(Accumulator<T> otherAccumulator)
      Adds the specified accumulator to the current accumulator.

      This is a mutator method; the current Accumulator instance is modified during the method call.

      Parameters:
      otherAccumulator - - Specified accumulator.
    • getSum

      public Accumulator<T> getSum(Accumulator<T> otherAccumulator)
      Returns a new instance of accumulator that is sum of the current accumulator and the specified accumulator.

      This Accumulator instance is not modified.

      Parameters:
      otherAccumulator - - Specified accumulator.
      Returns:
      Accumulator that is sum of the current accumulator and the specified accumulator.
    • getNormalized

      public Accumulator<T> getNormalized()
      Returns a new Accumulator that contains the ratio of each value to the total sum of values in this Accumulator.

      This Accumulator instance is not modified.

      If this Accumulator has no values or the sum of its values is equal to 0, then an empty Accumulator is returned.

      Returns:
      normalized version of this Accumulator
    • remove

      public void remove(Accumulator<T> otherAccumulator)
      Decrements the values stored in this Accumulator by the respective amounts from the otherAccumulator. Items with zero and negative values are retained in the current Accumulator.

      This is a mutator method; the current Accumulator instance is modified during the method call.

      Parameters:
      otherAccumulator - - a subtracted Accumulator.
    • getDifference

      public Accumulator<T> getDifference(Accumulator<T> otherAccumulator)
      Returns a new Accumulator containing the difference between the current Accumulator and the specified accumulator.

      This Accumulator instance is not modified.

      Parameters:
      otherAccumulator - - Specified accumulator.
      Returns:
      Accumulator that is difference of the current accumulator and the specified accumulator.
    • multiply

      public void multiply(double factor)
      Multiplies all values of the current accumulator by the specified factor.

      This is a mutator method; the current Accumulator instance is modified during the method call.

      Parameters:
      factor - - Specified factor.
    • getMultiplied

      public Accumulator<T> getMultiplied(double factor)
      Returns a new instance of accumulator that is equal to the current accumulator with all values multiplied by the specified factor.

      This Accumulator instance is not modified.

      Parameters:
      factor - - Specified factor.
      Returns:
      Accumulator that is equal to the current accumulator with all values multiplied by the specified factor.
    • set

      public void set(T element, double value)
      Sets the specified value to the specified accumulator element. If the specified element already exists in the accumulator, its value is overwritten, otherwise the specified element is added to the accumulator with the specified value.

      This is a mutator method; the current Accumulator instance is modified during the method call.

      Parameters:
      element - - Specified element.
      value - - Specified value.
    • get

      public double get(T element)
      Returns value of the specified element or 0 if the accumulator does not contain the specified element.
      Parameters:
      element - - Specified element.
      Returns:
      Value of the specified element, or 0 if the accumulator does not contain the specified element.
    • getSubSum

      public double getSubSum(List<T> elements)
      Returns the sum of the values of the specified elements or 0 if the accumulator does not contain any of the specified elements.
      Parameters:
      elements - - Specified elements.
      Returns:
      Sum of the values of the specified elements
    • getFraction

      public double getFraction(T element)
      Returns the proportion of value of the specified Accumulator element to the sum of values in the Accumulator.
      Parameters:
      element - - Specified element.
      Returns:
    • contains

      public boolean contains(T element)
      Checks if this accumulator contains the specified element.
      Parameters:
      element - - Specified element.
      Returns:
      true if the accumulator contains the specified element, false otherwise.
    • getElements

      public List<T> getElements()
      Returns a list of elements contained in the current accumulator sorted in the order of adding elements to the accumulator.
      Returns:
      List of elements contained in the current accumulator.
    • getPairs

      public List<Pair<T,Double>> getPairs()
      Returns a list of element-value pairs contained in the current accumulator sorted by sequence of adding elements to the accumulator.
      Returns:
      List of element-value pairs contained in the current accumulator.
    • clear

      public void clear()
      Removes all elements from the accumulator.
    • getCopy

      public Accumulator<T> getCopy()
      Creates a copy of the current accumulator.

      This Accumulator instance is not modified.

      Returns:
      Copy of the current accumulator.
    • getSum

      public double getSum()
      Returns the sum of all values contained in this accumulator.

      This Accumulator instance is not modified.

      Returns:
      sum of all values contained in this accumulator.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

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

      public String toDetailedString()
      Returns a detailed string representation of the current accumulator and its contents.
      Returns:
      Detailed string representation of the current accumulator and its contents.