View Javadoc
1 /*** 2 * AvgOperator.java 3 * 4 * $Author: mballesteros $ 5 * $Date: 2003/11/28 19:18:03 $ 6 * $Revision: 1.1 $ 7 */ 8 package net.sf.jec.namedop; 9 10 import java.util.Comparator; 11 12 import net.sf.jec.EvaluationException; 13 import net.sf.jec.Operator; 14 15 /*** ContextComparator applies the Operator passed to its constructor to each 16 * Object, given its context, and compares the results. Thus the result of this 17 * comparisson is just one order relation and is not consistent with equals. 18 *<p> 19 * <b>Note:</b>this comparator imposes orderings that are inconsistent with equals 20 *</p> 21 * @author mballesteros 22 */ 23 public class ContextComparator implements Comparator { 24 25 private final Object rootCtx; 26 private final Operator operator; 27 28 /*** Creates a new ContextComparator given the Operator and context Object 29 * needed to complete the expression whose result will be compared. 30 * @param rootCtx Context object. 31 * @param operator Operator. 32 */ 33 public ContextComparator(Object rootCtx, Operator operator) { 34 this.rootCtx = rootCtx; 35 this.operator = operator; 36 } 37 38 /*** Compares its two arguments for order. 39 * 40 * @param o1 First argument to compare. 41 * @param o2 Second argument to compare. 42 * @return Returns a negative integer, zero, or a positive integer as the 43 * first argument is less than, equal to, or greater than the second. 44 * @throws ClassCastException when the Operator results are not Comparable or when 45 * one EvaluationException is thrown from the evaluation of the operator. 46 */ 47 public int compare(Object o1, Object o2) throws ClassCastException { 48 try { 49 return ((Comparable) operator.apply(rootCtx, o1)) 50 .compareTo((Comparable) operator.apply(rootCtx, o2)); 51 } catch (EvaluationException e) { 52 throw new ClassCastException(this.getClass() + " can't compare arguments."); 53 } 54 } 55 }

This page was automatically generated by Maven