JIDE 3.5.15

com.jidesoft.pivot
Class DefaultSummaryCalculator

java.lang.Object
  extended by com.jidesoft.pivot.DefaultSummaryCalculator
All Implemented Interfaces:
PivotConstants, SummaryCalculator, Cloneable

public class DefaultSummaryCalculator
extends Object
implements SummaryCalculator, PivotConstants, Cloneable

Default implementation of SummaryCalculator. In this implementation, it calculates seven statistics - Sum, Min, Max, Mean, Var, StdDev, Count - for numbers (all classes extending Number and primitives) and BigDecimals; Max, Min, Count for String and only Count for all other data types.

You can always add your own summary. For example, you want to add a new summary called "Last Value". Here is the sample code.


 pivotDataModel.setSummaryCalculator(new DefaultSummaryCalculator(){
     final int SUMMARY_LAST_VALUE = PivotConstants.SUMMARY_RESERVED_MAX + 1;
     private double _lastValue;
     public void addValue(Object v) {
         super.addValue(v);
         if (object instanceof Number) {
             _lastValue = ((Number) object).doubleValue();
          }
     }
 

public void clear() { super.clear(); _lastValue = 0; }

public int getNumberOfSummaries() { return super.getNumberOfSummaries() + 1; }

public String getSummaryName(int type) { if(type == SUMMARY_LAST_VALUE) { return "Last Value"; } return super.getSummaryName(type); }

public Object getSummaryResult(int type) { if(type == SUMMARY_LAST_VALUE) { return new Double(_lastValue); } return super.getSummaryResult(type); } });

In the example, we use the DefaultSummaryCalculator because we just need to add one more summary. If you want to provide your own way to calculate the summaries, you can implement SummaryCalculator interface directly.

DefaultSummaryCalculator has built-in support for Numbers, BigDecimals, Dates/Calendars and Booleans. If you need to calculate other data types, you can create your own SummaryCalculator extending DefaultSummaryCalculator.


Field Summary
 
Fields inherited from interface com.jidesoft.pivot.SummaryCalculator
ALLOWED_SUMMARIES_ALL, ALLOWED_SUMMARIES_COUNT, ALLOWED_SUMMARIES_MAX_MIN_COUNT
 
Fields inherited from interface com.jidesoft.pivot.PivotConstants
AREA_COLUMN, AREA_DATA, AREA_FILTER, AREA_NAMES, AREA_NOT_ASSIGNED, AREA_ROW, COMPARE_TO_NEXT, COMPARE_TO_PREVIOUS, HEADER_SELECTION_DATA_TABLE_ONLY, HEADER_SELECTION_HEADER_TABLE_ONLY, RANGE_IN_TOTAL, RUNNING_SUMMARY_DIFFERENCE, RUNNING_SUMMARY_PERCENTAGE_FROM, RUNNING_SUMMARY_PERCENTAGE_OF, RUNNING_SUMMARY_PERCENTAGE_TOTAL, RUNNING_SUMMARY_TOTAL, STATISTICS_CALCULATION_DEFAULT, STATISTICS_CALCULATION_RUNNING, STATISTICS_CALCULATION_RUNNING_IN_COLUMN, STATISTICS_CALCULATION_RUNNING_LEAF, SUBTOTAL_AUTOMATIC, SUBTOTAL_CUSTOM, SUBTOTAL_NONE, SUMMARY_COUNT, SUMMARY_CUSTOM, SUMMARY_MAX, SUMMARY_MEAN, SUMMARY_MIN, SUMMARY_NAMES, SUMMARY_NONE, SUMMARY_RESERVED_MAX, SUMMARY_STDDEV, SUMMARY_SUM, SUMMARY_VAR
 
Constructor Summary
DefaultSummaryCalculator()
           
 
Method Summary
 void addValue(IPivotDataModel dataModel, PivotField field, int rowIndex, int columnIndex, Object value)
          Deprecated. replaced by addValue(PivotValueProvider, PivotField, Values, Values, Object)
 void addValue(Object object)
          Adds a value for calculation.
 void addValue(PivotValueProvider valueProvider, PivotField field, Object value)
          Deprecated. replaced by addValue(PivotValueProvider, PivotField, Values, Values, Object)
 void addValue(PivotValueProvider dataModel, PivotField field, Values rowValues, Values columnValues, Object object)
          Adds a value for calculation.
 void clear()
          Clears all previous added values.
 Object clone()
           
 int[] getAllowedSummaries(Class<?> type)
          Gets the allowed summary types for a data type.
 int[] getAllowedSummaries(Class<?> type, ConverterContext context)
          Gets the allowed summary types.
 long getCount()
          Gets the number of values that have been added so far.
 MathContext getMathContext()
          Gets the MathContext.
 int getNumberOfSummaries()
          Gets the number of summary types.
 String getSummaryName(Locale locale, int summaryType)
          Gets the summary name for the specified type.
 Object getSummaryResult(int type)
          Gets the summary result for the specified type.
 boolean isBiasCorrected()
          Checks if the biasCorrect is set.
 void setBiasCorrected(boolean biasCorrected)
          Sets the biasCorrected flag.
 void setMathContext(MathContext mathContext)
          To calculate statistics for BigDecimals, a MathContext to control the precision.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultSummaryCalculator

public DefaultSummaryCalculator()
Method Detail

clone

public Object clone()
             throws CloneNotSupportedException
Overrides:
clone in class Object
Throws:
CloneNotSupportedException

addValue

public void addValue(Object object)
Description copied from interface: SummaryCalculator
Adds a value for calculation.

Specified by:
addValue in interface SummaryCalculator
Parameters:
object - the value

addValue

@Deprecated
public void addValue(IPivotDataModel dataModel,
                                PivotField field,
                                int rowIndex,
                                int columnIndex,
                                Object value)
Deprecated. replaced by addValue(PivotValueProvider, PivotField, Values, Values, Object)

Adds a value for calculation. Different from addValue(Object) which only takes the value as parameter, this method provides PivotDataModel, PivotField and row/column index of the cell as in the DataTableModel.

Specified by:
addValue in interface SummaryCalculator
Parameters:
dataModel - the PivotDataModel
field - the PivotField
rowIndex - the row index as in the PivotDataModel#getDataTableModel. It could be -1 if the row index is not available.
columnIndex - the column index as in the PivotDataModel#getDataTableModel. It could be -1 if the column index is not available.
value - the value

addValue

@Deprecated
public void addValue(PivotValueProvider valueProvider,
                                PivotField field,
                                Object value)
Deprecated. replaced by addValue(PivotValueProvider, PivotField, Values, Values, Object)

Adds a value for calculation..

Specified by:
addValue in interface SummaryCalculator
Parameters:
valueProvider - the PivotValueProvider
field - the PivotField
value - the value

addValue

public void addValue(PivotValueProvider dataModel,
                     PivotField field,
                     Values rowValues,
                     Values columnValues,
                     Object object)
Description copied from interface: SummaryCalculator
Adds a value for calculation. Different from SummaryCalculator.addValue(Object) which only takes the value as parameter, this method provides PivotValueProvider, PivotField and row/column values of the cell as in the DataTableModel.

To get the row indices in the original table model, please try to use the following code.

 ((IPivotDataModel) dataModel).getDataAt(((DefaultValues) rowValues).toCompoundKey(), ((DefaultValues) columnValues).toCompoundKey());
 

Specified by:
addValue in interface SummaryCalculator
Parameters:
dataModel - the PivotDataModel
field - the PivotField
rowValues - the row values corresponding to the cell to calculate.
columnValues - the column values corresponding to the cell to calculate.
object - the value

clear

public void clear()
Description copied from interface: SummaryCalculator
Clears all previous added values.

Specified by:
clear in interface SummaryCalculator

getCount

public long getCount()
Description copied from interface: SummaryCalculator
Gets the number of values that have been added so far.

Specified by:
getCount in interface SummaryCalculator
Returns:
the number of values that have been added so far.

getNumberOfSummaries

public int getNumberOfSummaries()
Description copied from interface: SummaryCalculator
Gets the number of summary types.

Specified by:
getNumberOfSummaries in interface SummaryCalculator
Returns:
the number of statistic types.

getSummaryName

public String getSummaryName(Locale locale,
                             int summaryType)
Description copied from interface: SummaryCalculator
Gets the summary name for the specified type.

Specified by:
getSummaryName in interface SummaryCalculator
Parameters:
locale - the current locale
Returns:
the summary name.

getSummaryResult

public Object getSummaryResult(int type)
Description copied from interface: SummaryCalculator
Gets the summary result for the specified type.

Specified by:
getSummaryResult in interface SummaryCalculator
Returns:
the summary result for the specified type.

isBiasCorrected

public boolean isBiasCorrected()
Checks if the biasCorrect is set.

Returns:
true or false.

setBiasCorrected

public void setBiasCorrected(boolean biasCorrected)
Sets the biasCorrected flag. If true, variance will be calcuated dividing by n - 1. Otherwise, it will devide by n. Default is true.

Parameters:
biasCorrected - true or false.

getMathContext

public MathContext getMathContext()
Gets the MathContext.

Returns:
the MathContext.

setMathContext

public void setMathContext(MathContext mathContext)
To calculate statistics for BigDecimals, a MathContext to control the precision. You can set it using this method. This context is used in Mean, Var, StdDev calculation only for BigDecimal.

Parameters:
mathContext - the MathContext.

getAllowedSummaries

public int[] getAllowedSummaries(Class<?> type)
Description copied from interface: SummaryCalculator
Gets the allowed summary types for a data type.

Specified by:
getAllowedSummaries in interface SummaryCalculator
Parameters:
type - the data type.
Returns:
the allowed summary types.

getAllowedSummaries

public int[] getAllowedSummaries(Class<?> type,
                                 ConverterContext context)
Gets the allowed summary types.

Specified by:
getAllowedSummaries in interface SummaryCalculator
Parameters:
type - the data type.
context - the converter context. If the type is the same and you want it to have different summary types, you can use the converter context to make them different.
Returns:
the allowed summary types.

JIDE 3.5.15