public class DefaultSummaryCalculator extends Object implements SummaryCalculator, Cloneable
DefaultSummaryCalculator sc = 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, Strings and
Booleans. If you need to calculate other data types, you can create your own SummaryCalculator
extending
DefaultSummaryCalculator
.ALLOWED_SUMMARIES_ALL, ALLOWED_SUMMARIES_COUNT, ALLOWED_SUMMARIES_MAX_MIN_COUNT, SUMMARY_CONSTANT, SUMMARY_COUNT, SUMMARY_CUSTOM, SUMMARY_MAX, SUMMARY_MEAN, SUMMARY_MIN, SUMMARY_NONE, SUMMARY_RESERVED_MAX, SUMMARY_STDDEV, SUMMARY_SUM, SUMMARY_VAR
Constructor and Description |
---|
DefaultSummaryCalculator() |
Modifier and Type | Method and Description |
---|---|
void |
addValue(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.
|
public Object clone() throws CloneNotSupportedException
clone
in class Object
CloneNotSupportedException
public void addValue(Object object)
SummaryCalculator
addValue
in interface SummaryCalculator
object
- the valuepublic void clear()
SummaryCalculator
clear
in interface SummaryCalculator
public long getCount()
SummaryCalculator
getCount
in interface SummaryCalculator
public int getNumberOfSummaries()
SummaryCalculator
getNumberOfSummaries
in interface SummaryCalculator
public String getSummaryName(Locale locale, int summaryType)
SummaryCalculator
getSummaryName
in interface SummaryCalculator
locale
- the current localesummaryType
- the summary typepublic Object getSummaryResult(int type)
SummaryCalculator
getSummaryResult
in interface SummaryCalculator
type
- the summary typepublic boolean isBiasCorrected()
public void setBiasCorrected(boolean biasCorrected)
biasCorrected
- true or false.public MathContext getMathContext()
public void setMathContext(MathContext mathContext)
mathContext
- the MathContext.public int[] getAllowedSummaries(Class<?> type)
SummaryCalculator
getAllowedSummaries
in interface SummaryCalculator
type
- the data type.public int[] getAllowedSummaries(Class<?> type, ConverterContext context)
getAllowedSummaries
in interface SummaryCalculator
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.