1 /***
2 * MaxOperator.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.Collection;
11 import java.util.Collections;
12 import java.util.Map;
13
14 import net.sf.jec.EvaluationException;
15
16 /*** MaxOperator acts over a collection of elements, applying its argument operator
17 * to each element, and returning the maximum result value.
18 *
19 * @author mballesteros
20 */
21 public class MaxOperator extends GroupOperator {
22
23 private boolean throwExceptions;
24
25 /*** Creates a new MaxOperator
26 */
27 public MaxOperator() { }
28
29 /*** Returns a function representation String for this operator
30 * @return a String representation of this operator.
31 */
32 public String toFunctionString() {
33 return "Max(" + argOps[0]
34 + (nestedOp == null ? "" : ", " + nestedOp) + ")";
35 }
36
37 /*** Returns an expression String representation for this operator
38 * @return a String representation of this operator.
39 */
40 public String toExpressionString() {
41 return (nestedOp == null ? "" : nestedOp.toString()) + ".max(" + argOps[0] + ")";
42 }
43
44
45 /*** Returns the associated value to the given context and root context
46 * @param rootCtx The root context, needed for operators that require
47 * expression evaluation from root point. Example: Indexer operators
48 * @param ctx The current context where the operator will work over
49 * @return the result Object of applying THIS operator to the given context.
50 * @throws EvaluationException when something goes wrong applying this operator.
51 */
52 protected Object directMap(Object rootCtx, Object ctx) throws EvaluationException {
53 if (ctx instanceof Collection) {
54 try {
55 Object max = Collections.max((Collection) ctx, new ContextComparator(rootCtx, argOps[0]));
56 return argOps[0].apply(rootCtx, max);
57 } catch (ClassCastException e) {
58 if (this.throwExceptions) {
59 throw new EvaluationException("MaxOperator: Argument is not Comparable.");
60 } else {
61 return null;
62 }
63 }
64 } else if (ctx instanceof Map) {
65 return applyToGroup(rootCtx, (Map) ctx );
66 } else {
67 return null;
68 }
69 }
70 }
This page was automatically generated by Maven