1 /***
2 * Copyright 2003, 2004, 2005. CodeStreet LLC.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 package com.codestreet.selector.parser;
16
17 import java.util.Map;
18
19 /***
20 * Typesafe enumeration of valid arithmetic operators.
21 *
22 * @author Jawaid Hakim.
23 */
24 abstract class ArithOpImpl
25 {
26 /***
27 * Ctor.
28 *
29 * @param operator
30 * Operator.
31 */
32 private ArithOpImpl(final String operator)
33 {
34 operator_ = operator;
35 }
36
37 public String toString()
38 {
39 return operator_;
40 }
41
42 public abstract Object eval(final Map identifiers, final IExpression lhs,
43 final IExpression rhs);
44
45 public abstract Object eval(final IValueProvider provider,
46 final Object corr, final IExpression lhs, final IExpression rhs);
47
48 /***
49 * + operator.
50 */
51 public static final ArithOpImpl PLUS = new ArithOpImpl("+")
52 {
53 public Object eval(final Map identifiers, final IExpression lhs,
54 final IExpression rhs)
55 {
56 Object oLhs = lhs.eval(identifiers);
57 if (!(oLhs instanceof Number))
58 return Result.RESULT_UNKNOWN;
59 Number lhsVal = (Number) oLhs;
60
61 Object oRhs = rhs.eval(identifiers);
62 if (!(oRhs instanceof Number))
63 return Result.RESULT_UNKNOWN;
64 Number rhsVal = (Number) oRhs;
65
66 return new NumericValue(new Double(lhsVal.doubleValue() + rhsVal.doubleValue()));
67 }
68
69 public Object eval(final IValueProvider provider, final Object corr,
70 final IExpression lhs, final IExpression rhs)
71 {
72 Object oLhs = lhs.eval(provider, corr);
73 if (!(oLhs instanceof NumericValue))
74 return Result.RESULT_UNKNOWN;
75 NumericValue lhsVal = (NumericValue) oLhs;
76
77 Object oRhs = rhs.eval(provider, corr);
78 if (!(oRhs instanceof NumericValue))
79 return Result.RESULT_UNKNOWN;
80 NumericValue rhsVal = (NumericValue) oRhs;
81
82 return new NumericValue(new Double(lhsVal.doubleValue() + rhsVal.doubleValue()));
83 }
84 };
85
86 /***
87 * - operator.
88 */
89 public static final ArithOpImpl MINUS = new ArithOpImpl("-")
90 {
91 public Object eval(final Map identifiers, final IExpression lhs,
92 final IExpression rhs)
93 {
94 Object oLhs = lhs.eval(identifiers);
95 if (!(oLhs instanceof NumericValue))
96 return Result.RESULT_UNKNOWN;
97 NumericValue lhsVal = (NumericValue) oLhs;
98
99 Object oRhs = rhs.eval(identifiers);
100 if (!(oRhs instanceof NumericValue))
101 return Result.RESULT_UNKNOWN;
102 NumericValue rhsVal = (NumericValue) oRhs;
103
104 return new NumericValue(new Double(lhsVal.doubleValue() - rhsVal.doubleValue()));
105 }
106
107 public Object eval(final IValueProvider provider, final Object corr,
108 final IExpression lhs, final IExpression rhs)
109 {
110 Object oLhs = lhs.eval(provider, corr);
111 if (!(oLhs instanceof NumericValue))
112 return Result.RESULT_UNKNOWN;
113 NumericValue lhsVal = (NumericValue) oLhs;
114
115 Object oRhs = rhs.eval(provider, corr);
116 if (!(oRhs instanceof NumericValue))
117 return Result.RESULT_UNKNOWN;
118 NumericValue rhsVal = (NumericValue) oRhs;
119
120 return new NumericValue(new Double(lhsVal.doubleValue() - rhsVal.doubleValue()));
121 }
122 };
123
124 /***
125 * * operator.
126 */
127 public static final ArithOpImpl MULT = new ArithOpImpl("*")
128 {
129 public Object eval(final Map identifiers, final IExpression lhs,
130 final IExpression rhs)
131 {
132 Object oLhs = lhs.eval(identifiers);
133 if (!(oLhs instanceof NumericValue))
134 return Result.RESULT_UNKNOWN;
135 NumericValue lhsVal = (NumericValue) oLhs;
136
137 Object oRhs = rhs.eval(identifiers);
138 if (!(oRhs instanceof NumericValue))
139 return Result.RESULT_UNKNOWN;
140 NumericValue rhsVal = (NumericValue) oRhs;
141
142 return new NumericValue(new Double(lhsVal.doubleValue() * rhsVal.doubleValue()));
143 }
144
145 public Object eval(final IValueProvider provider, final Object corr,
146 final IExpression lhs, final IExpression rhs)
147 {
148 Object oLhs = lhs.eval(provider, corr);
149 if (!(oLhs instanceof NumericValue))
150 return Result.RESULT_UNKNOWN;
151 NumericValue lhsVal = (NumericValue) oLhs;
152
153 Object oRhs = rhs.eval(provider, corr);
154 if (!(oRhs instanceof NumericValue))
155 return Result.RESULT_UNKNOWN;
156 NumericValue rhsVal = (NumericValue) oRhs;
157
158 return new NumericValue(new Double(lhsVal.doubleValue() * rhsVal.doubleValue()));
159 }
160 };
161
162 /***
163 * / operator.
164 */
165 public static final ArithOpImpl DIV = new ArithOpImpl("/")
166 {
167 public Object eval(final Map identifiers, final IExpression lhs,
168 final IExpression rhs)
169 {
170 Object oLhs = lhs.eval(identifiers);
171 if (!(oLhs instanceof NumericValue))
172 return Result.RESULT_UNKNOWN;
173 NumericValue lhsVal = (NumericValue) oLhs;
174
175 Object oRhs = rhs.eval(identifiers);
176 if (!(oRhs instanceof NumericValue))
177 return Result.RESULT_UNKNOWN;
178 NumericValue rhsVal = (NumericValue) oRhs;
179
180 return new NumericValue(new Double(lhsVal.doubleValue() / rhsVal.doubleValue()));
181 }
182
183 public Object eval(final IValueProvider provider, final Object corr,
184 final IExpression lhs, final IExpression rhs)
185 {
186 Object oLhs = lhs.eval(provider, corr);
187 if (!(oLhs instanceof NumericValue))
188 return Result.RESULT_UNKNOWN;
189 NumericValue lhsVal = (NumericValue) oLhs;
190
191 Object oRhs = rhs.eval(provider, corr);
192 if (!(oRhs instanceof NumericValue))
193 return Result.RESULT_UNKNOWN;
194 NumericValue rhsVal = (NumericValue) oRhs;
195
196 return new NumericValue(new Double(lhsVal.doubleValue() / rhsVal.doubleValue()));
197 }
198 };
199
200 /***
201 * unary - operator.
202 */
203 public static final ArithOpImpl NEG = new ArithOpImpl("-")
204 {
205 public Object eval(final Map identifiers, final IExpression lhs,
206 final IExpression rhs)
207 {
208 Object oRhs = rhs.eval(identifiers);
209 if (!(oRhs instanceof NumericValue))
210 return Result.RESULT_UNKNOWN;
211 NumericValue rhsVal = (NumericValue) oRhs;
212
213 return new NumericValue(new Double(-rhsVal.doubleValue()));
214 }
215
216 public Object eval(final IValueProvider provider, final Object corr,
217 final IExpression lhs, final IExpression rhs)
218 {
219 Object oRhs = rhs.eval(provider, corr);
220 if (!(oRhs instanceof NumericValue))
221 return Result.RESULT_UNKNOWN;
222 NumericValue rhsVal = (NumericValue) oRhs;
223
224 return new NumericValue(new Double(-rhsVal.doubleValue()));
225 }
226 };
227
228 private final String operator_;
229 }
This page was automatically generated by Maven