01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.backend.list.memorybased.expression.compare;
16:
17: import java.util.Comparator;
18: import org.araneaframework.backend.list.memorybased.Expression;
19: import org.araneaframework.backend.list.memorybased.ExpressionEvaluationException;
20: import org.araneaframework.backend.list.memorybased.expression.CompositeExpression;
21: import org.araneaframework.backend.list.memorybased.expression.StringExpression;
22: import org.araneaframework.backend.list.memorybased.expression.VariableResolver;
23: import org.araneaframework.uilib.list.util.ComparatorFactory;
24: import org.araneaframework.uilib.list.util.comparator.NullComparator;
25: import org.araneaframework.uilib.list.util.comparator.StringComparator;
26:
27: public abstract class ComparableExpression implements
28: CompositeExpression, StringExpression {
29:
30: protected Expression expr1;
31:
32: protected Expression expr2;
33:
34: protected Comparator comparator;
35:
36: public ComparableExpression(Expression expr1, Expression expr2,
37: Comparator comparator) {
38: if (expr1 == null || expr2 == null) {
39: throw new IllegalArgumentException(
40: "Operands must be provided");
41: }
42: this .expr1 = expr1;
43: this .expr2 = expr2;
44: this .comparator = comparator;
45: }
46:
47: public ComparableExpression(Expression expr1, Expression expr2) {
48: this (expr1, expr2, ComparatorFactory.getDefault());
49: }
50:
51: public Comparator getComparator() {
52: return this .comparator;
53: }
54:
55: public final Object evaluate(VariableResolver resolver)
56: throws ExpressionEvaluationException {
57: Object value1 = this .expr1.evaluate(resolver);
58: Object value2 = this .expr2.evaluate(resolver);
59: return doEvaluate(value1, value2) ? Boolean.TRUE
60: : Boolean.FALSE;
61: }
62:
63: protected abstract boolean doEvaluate(Object value1, Object value2);
64:
65: public Expression[] getChildren() {
66: return new Expression[] { this .expr1, this .expr2 };
67: }
68:
69: public boolean getIgnoreCase() {
70: Comparator comp = this .comparator;
71: if (comp instanceof NullComparator) {
72: comp = ((NullComparator) comp).getNotNullComparator();
73: }
74:
75: if (comp instanceof StringComparator) {
76: return ((StringComparator) comp).getIgnoreCase();
77: }
78: return false;
79: }
80: }
|