KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > TypedStringValue


1 /*
2  * Copyright 2002-2007 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.beans.factory.config;
18
19 import org.springframework.beans.BeanMetadataElement;
20 import org.springframework.util.Assert;
21 import org.springframework.util.ClassUtils;
22 import org.springframework.util.ObjectUtils;
23
24 /**
25  * Holder for a typed String value. Can be added to bean definitions
26  * in order to explicitly specify a target type for a String value,
27  * for example for collection elements.
28  *
29  * <p>This holder will just store the String value and the target type.
30  * The actual conversion will be performed by the bean factory.
31  *
32  * @author Juergen Hoeller
33  * @since 1.2
34  * @see BeanDefinition#getPropertyValues
35  * @see org.springframework.beans.MutablePropertyValues#addPropertyValue
36  */

37 public class TypedStringValue implements BeanMetadataElement {
38
39     private String JavaDoc value;
40
41     private Object JavaDoc targetType;
42
43     private Object JavaDoc source;
44
45
46     /**
47      * Create a new {@link TypedStringValue} for the given String value.
48      * @param value the String value
49      */

50     public TypedStringValue(String JavaDoc value) {
51         setValue(value);
52     }
53
54     /**
55      * Create a new {@link TypedStringValue} for the given String value
56      * and target type.
57      * @param value the String value
58      * @param targetType the type to convert to
59      */

60     public TypedStringValue(String JavaDoc value, Class JavaDoc targetType) {
61         setValue(value);
62         setTargetType(targetType);
63     }
64
65     /**
66      * Create a new {@link TypedStringValue} for the given String value
67      * and target type.
68      * @param value the String value
69      * @param targetTypeName the type to convert to
70      */

71     public TypedStringValue(String JavaDoc value, String JavaDoc targetTypeName) {
72         setValue(value);
73         setTargetTypeName(targetTypeName);
74     }
75
76
77     /**
78      * Set the String value.
79      * Only necessary for manipulating a registered value,
80      * for example in BeanFactoryPostProcessors.
81      * @see PropertyPlaceholderConfigurer
82      */

83     public void setValue(String JavaDoc value) {
84         this.value = value;
85     }
86
87     /**
88      * Return the String value.
89      */

90     public String JavaDoc getValue() {
91         return this.value;
92     }
93
94     /**
95      * Set the type to convert to.
96      * Only necessary for manipulating a registered value,
97      * for example in BeanFactoryPostProcessors.
98      * @see PropertyPlaceholderConfigurer
99      */

100     public void setTargetType(Class JavaDoc targetType) {
101         Assert.notNull(targetType, "'targetType' must not be null");
102         this.targetType = targetType;
103     }
104
105     /**
106      * Return the type to convert to.
107      */

108     public Class JavaDoc getTargetType() {
109         if (!(this.targetType instanceof Class JavaDoc)) {
110             throw new IllegalStateException JavaDoc("Typed String value does not carry a resolved target type");
111         }
112         return (Class JavaDoc) this.targetType;
113     }
114
115     /**
116      * Specify the type to convert to.
117      */

118     public void setTargetTypeName(String JavaDoc targetTypeName) {
119         Assert.notNull(targetTypeName, "'targetTypeName' must not be null");
120         this.targetType = targetTypeName;
121     }
122
123     /**
124      * Return the type to convert to.
125      */

126     public String JavaDoc getTargetTypeName() {
127         if (this.targetType instanceof Class JavaDoc) {
128             return ((Class JavaDoc) this.targetType).getName();
129         }
130         else {
131             return (String JavaDoc) this.targetType;
132         }
133     }
134
135     /**
136      * Return whether this typed String value carries a target type .
137      */

138     public boolean hasTargetType() {
139         return (this.targetType instanceof Class JavaDoc);
140     }
141
142     /**
143      * Determine the type to convert to, resolving it from a specified class name
144      * if necessary. Will also reload a specified Class from its name when called
145      * with the target type already resolved.
146      * @param classLoader the ClassLoader to use for resolving a (potential) class name
147      * @return the resolved type to convert to
148      * @throws ClassNotFoundException if the type cannot be resolved
149      */

150     public Class JavaDoc resolveTargetType(ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
151         if (this.targetType == null) {
152             return null;
153         }
154         Class JavaDoc resolvedClass = ClassUtils.forName(getTargetTypeName(), classLoader);
155         this.targetType = resolvedClass;
156         return resolvedClass;
157     }
158
159
160     /**
161      * Set the configuration source <code>Object</code> for this metadata element.
162      * <p>The exact type of the object will depend on the configuration mechanism used.
163      */

164     public void setSource(Object JavaDoc source) {
165         this.source = source;
166     }
167
168     public Object JavaDoc getSource() {
169         return this.source;
170     }
171
172
173     public boolean equals(Object JavaDoc other) {
174         if (this == other) {
175             return true;
176         }
177         if (!(other instanceof TypedStringValue)) {
178             return false;
179         }
180         TypedStringValue otherValue = (TypedStringValue) other;
181         return (ObjectUtils.nullSafeEquals(this.value, otherValue.value) &&
182                 ObjectUtils.nullSafeEquals(this.targetType, otherValue.targetType));
183     }
184
185     public int hashCode() {
186         return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.targetType);
187     }
188
189     public String JavaDoc toString() {
190         return "TypedStringValue: value [" + this.value + "], target type [" + this.targetType + "]";
191     }
192
193 }
194
Popular Tags