001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.el.tree.impl.ast;
017:
018: import javax.el.ELContext;
019:
020: import de.odysseus.el.misc.BooleanOperations;
021: import de.odysseus.el.misc.NumberOperations;
022: import de.odysseus.el.tree.Bindings;
023:
024: public final class AstBinary extends AstRightValue {
025: public interface Operator {
026: public Object apply(Object o1, Object o2);
027: }
028:
029: public static final Operator ADD = new Operator() {
030: public Object apply(Object o1, Object o2) {
031: return NumberOperations.add(o1, o2);
032: }
033:
034: @Override
035: public String toString() {
036: return "+";
037: }
038: };
039: public static final Operator AND = new Operator() {
040: public Object apply(Object o1, Object o2) {
041: return BooleanOperations.and(o1, o2);
042: }
043:
044: @Override
045: public String toString() {
046: return "&&";
047: }
048: };
049: public static final Operator DIV = new Operator() {
050: public Object apply(Object o1, Object o2) {
051: return NumberOperations.div(o1, o2);
052: }
053:
054: @Override
055: public String toString() {
056: return "/";
057: }
058: };
059: public static final Operator EQ = new Operator() {
060: public Object apply(Object o1, Object o2) {
061: return BooleanOperations.eq(o1, o2);
062: }
063:
064: @Override
065: public String toString() {
066: return "==";
067: }
068: };
069: public static final Operator GE = new Operator() {
070: public Object apply(Object o1, Object o2) {
071: return BooleanOperations.ge(o1, o2);
072: }
073:
074: @Override
075: public String toString() {
076: return ">=";
077: }
078: };
079: public static final Operator GT = new Operator() {
080: public Object apply(Object o1, Object o2) {
081: return BooleanOperations.gt(o1, o2);
082: }
083:
084: @Override
085: public String toString() {
086: return ">";
087: }
088: };
089: public static final Operator LE = new Operator() {
090: public Object apply(Object o1, Object o2) {
091: return BooleanOperations.le(o1, o2);
092: }
093:
094: @Override
095: public String toString() {
096: return "<=";
097: }
098: };
099: public static final Operator LT = new Operator() {
100: public Object apply(Object o1, Object o2) {
101: return BooleanOperations.lt(o1, o2);
102: }
103:
104: @Override
105: public String toString() {
106: return "<";
107: }
108: };
109: public static final Operator MOD = new Operator() {
110: public Object apply(Object o1, Object o2) {
111: return NumberOperations.mod(o1, o2);
112: }
113:
114: @Override
115: public String toString() {
116: return "%";
117: }
118: };
119: public static final Operator MUL = new Operator() {
120: public Object apply(Object o1, Object o2) {
121: return NumberOperations.mul(o1, o2);
122: }
123:
124: @Override
125: public String toString() {
126: return "*";
127: }
128: };
129: public static final Operator NE = new Operator() {
130: public Object apply(Object o1, Object o2) {
131: return BooleanOperations.ne(o1, o2);
132: }
133:
134: @Override
135: public String toString() {
136: return "!=";
137: }
138: };
139: public static final Operator OR = new Operator() {
140: public Object apply(Object o1, Object o2) {
141: return BooleanOperations.or(o1, o2);
142: }
143:
144: @Override
145: public String toString() {
146: return "||";
147: }
148: };
149: public static final Operator SUB = new Operator() {
150: public Object apply(Object o1, Object o2) {
151: return NumberOperations.sub(o1, o2);
152: }
153:
154: @Override
155: public String toString() {
156: return "-";
157: }
158: };
159:
160: private final Operator operator;
161: private final AstNode left, right;
162:
163: public AstBinary(AstNode left, AstNode right, Operator operator) {
164: this .left = left;
165: this .right = right;
166: this .operator = operator;
167: }
168:
169: public Operator getOperator() {
170: return operator;
171: }
172:
173: @Override
174: public Object eval(Bindings bindings, ELContext context) {
175: return operator.apply(left.eval(bindings, context), right.eval(
176: bindings, context));
177: }
178:
179: @Override
180: public String toString() {
181: return "'" + operator.toString() + "'";
182: }
183:
184: @Override
185: public void appendStructure(StringBuilder b, Bindings bindings) {
186: left.appendStructure(b, bindings);
187: b.append(' ');
188: b.append(operator);
189: b.append(' ');
190: right.appendStructure(b, bindings);
191: }
192:
193: public int getCardinality() {
194: return 2;
195: }
196:
197: public AstNode getChild(int i) {
198: return i == 0 ? left : i == 1 ? right : null;
199: }
200: }
|