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;
017:
018: import javax.el.ELContext;
019: import javax.el.ELException;
020:
021: import de.odysseus.el.misc.LocalMessages;
022: import de.odysseus.el.misc.TypeConversions;
023:
024: /**
025: * Object wrapper expression.
026: *
027: * @author Christoph Beck
028: */
029: public final class ObjectValueExpression extends
030: javax.el.ValueExpression {
031: private static final long serialVersionUID = 1L;
032:
033: private final Object object;
034: private final Class<?> type;
035:
036: /**
037: * Wrap an object into a value expression.
038: * @param object the object to wrap
039: * @param type the expected type this object will be coerced in {@link #getValue(ELContext)}.
040: */
041: public ObjectValueExpression(Object object, Class<?> type) {
042: super ();
043:
044: this .object = object;
045: this .type = type;
046:
047: if (type == null) {
048: throw new NullPointerException(LocalMessages
049: .get("error.value.notype"));
050: }
051: }
052:
053: /**
054: * Two object value expressions are equal if and only if their wrapped objects are equal.
055: */
056: @Override
057: public boolean equals(Object obj) {
058: if (obj != null && obj.getClass() == getClass()) {
059: ObjectValueExpression other = (ObjectValueExpression) obj;
060: if (type != other.type) {
061: return false;
062: }
063: return object == other.object || object != null
064: && object.equals(other.object);
065: }
066: return false;
067: }
068:
069: @Override
070: public int hashCode() {
071: return object == null ? 0 : object.hashCode();
072: }
073:
074: /**
075: * Answer the wrapped object, coerced to the expected type.
076: */
077: @Override
078: public Object getValue(ELContext context) {
079: return TypeConversions.coerceToType(object, type);
080: }
081:
082: /**
083: * Answer <code>null</code>.
084: */
085: @Override
086: public String getExpressionString() {
087: return null;
088: }
089:
090: /**
091: * Answer <code>false</code>.
092: */
093: @Override
094: public boolean isLiteralText() {
095: return false;
096: }
097:
098: /**
099: * Answer <code>null</code>.
100: */
101: @Override
102: public Class<?> getType(ELContext context) {
103: return null;
104: }
105:
106: /**
107: * Answer <code>true</code>.
108: */
109: @Override
110: public boolean isReadOnly(ELContext context) {
111: return true;
112: }
113:
114: /**
115: * Throw an exception.
116: */
117: @Override
118: public void setValue(ELContext context, Object value) {
119: throw new ELException(LocalMessages
120: .get("error.value.set.rvalue"));
121: }
122:
123: @Override
124: public String toString() {
125: return "ValueExpression(" + object + ")";
126: }
127:
128: @Override
129: public Class<?> getExpectedType() {
130: return type;
131: }
132: }
|