View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
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
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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  
17  package org.springframework.expression;
18  
19  import org.springframework.core.convert.TypeDescriptor;
20  import org.springframework.util.ObjectUtils;
21  
22  /**
23   * Encapsulates an object and a {@link TypeDescriptor} that describes it.
24   * The type descriptor can contain generic declarations that would not
25   * be accessible through a simple {@code getClass()} call on the object.
26   *
27   * @author Andy Clement
28   * @author Juergen Hoeller
29   * @since 3.0
30   */
31  public class TypedValue {
32  
33  	public static final TypedValue NULL = new TypedValue(null);
34  
35  
36  	private final Object value;
37  
38  	private TypeDescriptor typeDescriptor;
39  
40  
41  	/**
42  	 * Create a {@link TypedValue} for a simple object. The {@link TypeDescriptor}
43  	 * is inferred from the object, so no generic declarations are preserved.
44  	 * @param value the object value
45  	 */
46  	public TypedValue(Object value) {
47  		this.value = value;
48  		this.typeDescriptor = null;  // initialized when/if requested
49  	}
50  
51  	/**
52  	 * Create a {@link TypedValue} for a particular value with a particular
53  	 * {@link TypeDescriptor} which may contain additional generic declarations.
54  	 * @param value the object value
55  	 * @param typeDescriptor a type descriptor describing the type of the value
56  	 */
57  	public TypedValue(Object value, TypeDescriptor typeDescriptor) {
58  		this.value = value;
59  		this.typeDescriptor = typeDescriptor;
60  	}
61  
62  
63  	public Object getValue() {
64  		return this.value;
65  	}
66  
67  	public TypeDescriptor getTypeDescriptor() {
68  		if (this.typeDescriptor == null && this.value != null) {
69  			this.typeDescriptor = TypeDescriptor.forObject(this.value);
70  		}
71  		return this.typeDescriptor;
72  	}
73  
74  
75  	@Override
76  	public boolean equals(Object other) {
77  		if (this == other) {
78  			return true;
79  		}
80  		if (!(other instanceof TypedValue)) {
81  			return false;
82  		}
83  		TypedValue otherTv = (TypedValue) other;
84  		// Avoid TypeDescriptor initialization if not necessary
85  		return (ObjectUtils.nullSafeEquals(this.value, otherTv.value) &&
86  				((this.typeDescriptor == null && otherTv.typeDescriptor == null) ||
87  						ObjectUtils.nullSafeEquals(getTypeDescriptor(), otherTv.getTypeDescriptor())));
88  	}
89  
90  	@Override
91  	public int hashCode() {
92  		return ObjectUtils.nullSafeHashCode(this.value);
93  	}
94  
95  	@Override
96  	public String toString() {
97  		return "TypedValue: '" + this.value + "' of [" + getTypeDescriptor() + "]";
98  	}
99  
100 }