Class Accumulator<T>
- Type Parameters:
T- a type whose instances represent different categories
- Author:
- Andrey Malykhanov, Kliment Zakharov
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(Accumulator<T> otherAccumulator) Adds the specified accumulator to the current accumulator.doubleIncrements the amount associated withelementcategory stored in this Accumulator by the amountvalue.voidclear()Removes all elements from the accumulator.booleanChecks if this accumulator contains the specified element.booleandoubleReturns value of the specified element or 0 if the accumulator does not contain the specified element.getCopy()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.doublegetFraction(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.getPairs()Returns a list of element-value pairs contained in the current accumulator sorted by sequence of adding elements to the accumulator.doubleReturns the sum of the values of the specified elements or 0 if the accumulator does not contain any of the specified elements.doublegetSum()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.inthashCode()voidmultiply(double factor) Multiplies all values of the current accumulator by the specified factor.static <T> Accumulator<T> Creates a newAccumulatorinstance 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.voidremove(Accumulator<T> otherAccumulator) Decrements the values stored in this Accumulator by the respective amounts from theotherAccumulator.voidSets the specified value to the specified accumulator element.Returns a detailed string representation of the current accumulator and its contents.toString()
-
Constructor Details
-
Accumulator
public Accumulator()
-
-
Method Details
-
of
Creates a newAccumulatorinstance 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
Accumulatorinstance
-
of
Returns a new Accumulator containing a single mapping.- Type Parameters:
T- theAccumulator's category type- Parameters:
cat1- categoryv1- value- Returns:
- an
Accumulatorcontaining the specified mapping
-
of
Returns a new Accumulator containing two mappings.- Type Parameters:
T- theAccumulator's category type- Parameters:
cat1- the first mapping's categoryv1- the first mapping's valuecat2- the second mapping's categoryv2- the second mapping's value- Returns:
- an
Accumulatorcontaining the specified mappings
-
of
Returns a new Accumulator containing three mappings.- Type Parameters:
T- theAccumulator's category type- Parameters:
cat1- the first mapping's categoryv1- the first mapping's valuecat2- the second mapping's categoryv2- the second mapping's valuecat3- the third mapping's categoryv3- the third mapping's value- Returns:
- an
Accumulatorcontaining the specified mappings
-
add
Increments the amount associated withelementcategory stored in this Accumulator by the amountvalue. If this Accumulator has no value associated with theelement, 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- categoryvalue- amount increment- Returns:
- the value that is the result of accumulation of existing value and the specified value for the specified element
-
add
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
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
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
Decrements the values stored in this Accumulator by the respective amounts from theotherAccumulator. 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
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
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
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
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
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
Returns the proportion of value of the specified Accumulator element to the sum of values in the Accumulator.- Parameters:
element- - Specified element.- Returns:
-
contains
Checks if this accumulator contains the specified element.- Parameters:
element- - Specified element.- Returns:
trueif the accumulator contains the specified element,falseotherwise.
-
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
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
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
-
hashCode
public int hashCode() -
toString
-
toDetailedString
Returns a detailed string representation of the current accumulator and its contents.- Returns:
- Detailed string representation of the current accumulator and its contents.
-