01: /*
02: * Copyright 2006, 2007 Odysseus Software GmbH
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: */
16: package de.odysseus.el.tree.impl.ast;
17:
18: import javax.el.ELContext;
19: import javax.el.ELException;
20:
21: import de.odysseus.el.misc.BooleanOperations;
22: import de.odysseus.el.misc.NumberOperations;
23: import de.odysseus.el.tree.Bindings;
24:
25: public final class AstUnary extends AstRightValue {
26: public interface Operator {
27: public Object apply(Object o);
28: }
29:
30: public static final Operator EMPTY = new Operator() {
31: public Object apply(Object o) {
32: return BooleanOperations.empty(o);
33: }
34:
35: @Override
36: public String toString() {
37: return "empty";
38: }
39: };
40: public static final Operator NEG = new Operator() {
41: public Object apply(Object o) {
42: return NumberOperations.neg(o);
43: }
44:
45: @Override
46: public String toString() {
47: return "-";
48: }
49: };
50: public static final Operator NOT = new Operator() {
51: public Object apply(Object o) {
52: return BooleanOperations.not(o);
53: }
54:
55: @Override
56: public String toString() {
57: return "!";
58: }
59: };
60:
61: private final Operator operator;
62: private final AstNode child;
63:
64: public AstUnary(AstNode child, AstUnary.Operator operator) {
65: this .child = child;
66: this .operator = operator;
67: }
68:
69: public Operator getOperator() {
70: return operator;
71: }
72:
73: @Override
74: public Object eval(Bindings bindings, ELContext context)
75: throws ELException {
76: return operator.apply(child.eval(bindings, context));
77: }
78:
79: @Override
80: public String toString() {
81: return "'" + operator.toString() + "'";
82: }
83:
84: @Override
85: public void appendStructure(StringBuilder b, Bindings bindings) {
86: b.append(operator);
87: b.append(' ');
88: child.appendStructure(b, bindings);
89: }
90:
91: public int getCardinality() {
92: return 1;
93: }
94:
95: public AstNode getChild(int i) {
96: return i == 0 ? child : null;
97: }
98: }
|