01: /*
02: * Copyright 2004-2007 the original author or authors.
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 org.springframework.binding.expression.support;
17:
18: import org.springframework.binding.expression.EvaluationContext;
19: import org.springframework.binding.expression.EvaluationException;
20: import org.springframework.binding.expression.Expression;
21: import org.springframework.util.ObjectUtils;
22:
23: /**
24: * A simple expression evaluator that just returns a fixed result on each
25: * evaluation.
26: *
27: * @author Keith Donald
28: */
29: public class StaticExpression implements Expression {
30:
31: /**
32: * The value expression.
33: */
34: private Object value;
35:
36: /**
37: * Create a static evaluator for the given value.
38: * @param value the value
39: */
40: public StaticExpression(Object value) {
41: this .value = value;
42: }
43:
44: public int hashCode() {
45: if (value == null) {
46: return 0;
47: } else {
48: return value.hashCode();
49: }
50: }
51:
52: public boolean equals(Object o) {
53: if (!(o instanceof StaticExpression)) {
54: return false;
55: }
56: StaticExpression other = (StaticExpression) o;
57: return ObjectUtils.nullSafeEquals(value, other.value);
58: }
59:
60: public Object evaluate(Object target, EvaluationContext context)
61: throws EvaluationException {
62: return value;
63: }
64:
65: public String toString() {
66: return String.valueOf(value);
67: }
68: }
|