package com.onaro.util.jfc.grouping; import com.onaro.sanscreen.client.view.tabular.value.NumberValue; import com.onaro.util.jfc.grouping.GroupingModel.State.Row; public class PercentSummarizer implements Summarizer { /** * The column to which the percentage value is set (for the children of the node). */ private int column; /** * The source column from which the values are taken. */ private int sourceColumn; /** * Defined the format-name used by {@link NumberValue} to format the number. */ private String format = NumberValue.FORMAT_DEFAULT; /** * Initialize the collector for the given column. * @param column the colloumn to collect its values * @param delimiter used to separate the values in the string presentation of the summary */ public PercentSummarizer(int column, int sourceColumn, String format) { this.column = column; this.sourceColumn = sourceColumn; this.format = format; } public PercentSummarizer(int column, int sourceColumn) { this(column, sourceColumn, NumberValue.FORMAT_DEFAULT); } public void updateSummary(Node node) { int childCount = node.getChildCount(); if (childCount > 0) { double total = 0; for (int childInd = 0 ; childInd < childCount; ++childInd) { Node child = node.getChild(childInd); Object value = child.getValueAt(sourceColumn); total += getNumberValue(value); } if (total==0) return; for (int childInd = 0 ; childInd < childCount; ++childInd) { Node child = node.getChild(childInd); Object value = child.getValueAt(sourceColumn); double percent = getNumberValue(value) / total; NumberValue numberValue = new NumberValue(percent, null, format); if (child instanceof Row) { ((Row)child).setOverlayValueAt(column, numberValue); } else { child.setValueAt(column, numberValue); } } } } private double getNumberValue(Object value) { if (value instanceof Number) { return ((Number)value).doubleValue(); } else if (value instanceof NumberValue) { return ((NumberValue) value).getNumber(); } return 0; } }