package com.onaro.util.jfc.grouping; /** * Summarizes values that can be compare and chooses the lowest of highest as the summart. * E.g. if a priority column has this summarizer configured to lowest: * High, Low, Med, Low, High -> Low */ public class ExtremeValueSummarizer implements Summarizer { public static final String POLICY_LOWEST = "lowest"; //$NON-NLS-1$ public static final String POLICY_HIGHEST = "highest"; //$NON-NLS-1$ private int column; private boolean lowestValue; /** * Create a ExtremeValueSummarizer * @param column the index of the column to summarize * @param policy can be "highest" or "lowest" */ public ExtremeValueSummarizer(int column, String policy) { this.column = column; this.lowestValue = POLICY_LOWEST.equalsIgnoreCase(policy); } public void updateSummary(Node node) { int childCount = node.getChildCount(); if (childCount > 0) { node.setValueAt(column, find(node, childCount)); } } /** * Finds the lowest or highest value of the nodes in the group */ @SuppressWarnings("unchecked") private Comparable find(Node node, int childCount) { Comparable extreme = null; for (int child = 0; child < childCount; ++child) { Object obj = node.getChild(child).getValueAt(column); if (obj instanceof Comparable) { extreme = compareWithExtreem(extreme, (Comparable) obj); } else { //if the value can not be compare. give up and show nothing return null; } } return extreme; } private Comparable compareWithExtreem(Comparable extreme, Comparable value) { if (extreme != null) { int rv = extreme.compareTo(value); if(lowestValue) { //return the smaller comparable return rv <= 0 ? extreme : value; }else{ //return the larger comparable return rv >= 0 ? extreme : value; } }else{ return value; } } }