1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import org .apache .commons .logging .Log ;
20
20
import org .apache .commons .logging .LogFactory ;
21
+
21
22
import org .springframework .util .Assert ;
23
+ import org .springframework .util .ObjectUtils ;
22
24
23
25
/**
24
26
* Abstract base class representing a source of name/value property pairs. The underlying
55
57
*/
56
58
public abstract class PropertySource <T > {
57
59
58
- protected final Log logger = LogFactory .getLog (this . getClass ());
60
+ protected final Log logger = LogFactory .getLog (getClass ());
59
61
60
62
protected final String name ;
61
63
62
64
protected final T source ;
63
65
66
+
64
67
/**
65
68
* Create a new {@code PropertySource} with the given name and source object.
66
69
*/
@@ -74,15 +77,15 @@ public PropertySource(String name, T source) {
74
77
/**
75
78
* Create a new {@code PropertySource} with the given name and with a new {@code Object}
76
79
* instance as the underlying source.
77
- * <p>Often useful in testing scenarios when creating
78
- * anonymous implementations that never query an actual source, but rather return
79
- * hard-coded values.
80
+ * <p>Often useful in testing scenarios when creating anonymous implementations that
81
+ * never query an actual source but rather return hard-coded values.
80
82
*/
81
83
@ SuppressWarnings ("unchecked" )
82
84
public PropertySource (String name ) {
83
85
this (name , (T ) new Object ());
84
86
}
85
87
88
+
86
89
/**
87
90
* Return the name of this {@code PropertySource}
88
91
*/
@@ -94,91 +97,75 @@ public String getName() {
94
97
* Return the underlying source object for this {@code PropertySource}.
95
98
*/
96
99
public T getSource () {
97
- return source ;
100
+ return this . source ;
98
101
}
99
102
100
103
/**
101
104
* Return whether this {@code PropertySource} contains the given name.
102
- * <p>This implementation simply checks for a null return value
103
- * from {@link #getProperty(String)}. Subclasses may wish to
104
- * implement a more efficient algorithm if possible.
105
+ * <p>This implementation simply checks for a {@code null} return value
106
+ * from {@link #getProperty(String)}. Subclasses may wish to implement
107
+ * a more efficient algorithm if possible.
105
108
* @param name the property name to find
106
109
*/
107
110
public boolean containsProperty (String name ) {
108
- return this . getProperty (name ) != null ;
111
+ return ( getProperty (name ) != null ) ;
109
112
}
110
113
111
114
/**
112
- * Return the value associated with the given name, {@code null} if not found.
115
+ * Return the value associated with the given name,
116
+ * or {@code null} if not found.
113
117
* @param name the property to find
114
118
* @see PropertyResolver#getRequiredProperty(String)
115
119
*/
116
120
public abstract Object getProperty (String name );
117
121
118
- /**
119
- * Return a hashcode derived from the {@code name} property of this {@code PropertySource}
120
- * object.
121
- */
122
- @ Override
123
- public int hashCode () {
124
- final int prime = 31 ;
125
- int result = 1 ;
126
- result = prime * result + ((this .name == null ) ? 0 : this .name .hashCode ());
127
- return result ;
128
- }
129
122
130
123
/**
131
124
* This {@code PropertySource} object is equal to the given object if:
132
125
* <ul>
133
- * <li>they are the same instance
134
- * <li>the {@code name} properties for both objects are equal
126
+ * <li>they are the same instance
127
+ * <li>the {@code name} properties for both objects are equal
135
128
* </ul>
136
- *
137
- * <P>No properties other than {@code name} are evaluated.
129
+ * <p>No properties other than {@code name} are evaluated.
138
130
*/
139
131
@ Override
140
132
public boolean equals (Object obj ) {
141
- if (this == obj )
142
- return true ;
143
- if (obj == null )
144
- return false ;
145
- if (!(obj instanceof PropertySource ))
146
- return false ;
147
- PropertySource <?> other = (PropertySource <?>) obj ;
148
- if (this .name == null ) {
149
- if (other .name != null )
150
- return false ;
151
- } else if (!this .name .equals (other .name ))
152
- return false ;
153
- return true ;
133
+ return (this == obj || (obj instanceof PropertySource &&
134
+ ObjectUtils .nullSafeEquals (this .name , ((PropertySource <?>) obj ).name )));
135
+ }
136
+
137
+ /**
138
+ * Return a hash code derived from the {@code name} property
139
+ * of this {@code PropertySource} object.
140
+ */
141
+ @ Override
142
+ public int hashCode () {
143
+ return ObjectUtils .nullSafeHashCode (this .name );
154
144
}
155
145
156
146
/**
157
147
* Produce concise output (type and name) if the current log level does not include
158
- * debug. If debug is enabled, produce verbose output including hashcode of the
148
+ * debug. If debug is enabled, produce verbose output including the hash code of the
159
149
* PropertySource instance and every name/value property pair.
160
- *
161
- * This variable verbosity is useful as a property source such as system properties
150
+ * <p>This variable verbosity is useful as a property source such as system properties
162
151
* or environment variables may contain an arbitrary number of property pairs,
163
152
* potentially leading to difficult to read exception and log messages.
164
- *
165
153
* @see Log#isDebugEnabled()
166
154
*/
167
155
@ Override
168
156
public String toString () {
169
157
if (logger .isDebugEnabled ()) {
170
158
return String .format ("%s@%s [name='%s', properties=%s]" ,
171
- this .getClass ().getSimpleName (), System .identityHashCode (this ), this .name , this .source );
159
+ getClass ().getSimpleName (), System .identityHashCode (this ), this .name , this .source );
160
+ }
161
+ else {
162
+ return String .format ("%s [name='%s']" , getClass ().getSimpleName (), this .name );
172
163
}
173
-
174
- return String .format ("%s [name='%s']" ,
175
- this .getClass ().getSimpleName (), this .name );
176
164
}
177
165
178
166
179
167
/**
180
168
* Return a {@code PropertySource} implementation intended for collection comparison purposes only.
181
- *
182
169
* <p>Primarily for internal use, but given a collection of {@code PropertySource} objects, may be
183
170
* used as follows:
184
171
* <pre class="code">
@@ -189,11 +176,9 @@ public String toString() {
189
176
* assert sources.contains(PropertySource.named("sourceB"));
190
177
* assert !sources.contains(PropertySource.named("sourceC"));
191
178
* }</pre>
192
- *
193
179
* The returned {@code PropertySource} will throw {@code UnsupportedOperationException}
194
180
* if any methods other than {@code equals(Object)}, {@code hashCode()}, and {@code toString()}
195
181
* are called.
196
- *
197
182
* @param name the name of the comparison {@code PropertySource} to be created and returned.
198
183
*/
199
184
public static PropertySource <?> named (String name ) {
@@ -209,7 +194,6 @@ public static PropertySource<?> named(String name) {
209
194
* {@code ApplicationContext}. In such cases, a stub should be used to hold the
210
195
* intended default position/order of the property source, then be replaced
211
196
* during context refresh.
212
- *
213
197
* @see org.springframework.context.support.AbstractApplicationContext#initPropertySources()
214
198
* @see org.springframework.web.context.support.StandardServletEnvironment
215
199
* @see org.springframework.web.context.support.ServletContextPropertySource
@@ -221,7 +205,7 @@ public StubPropertySource(String name) {
221
205
}
222
206
223
207
/**
224
- * Always return {@code null}.
208
+ * Always returns {@code null}.
225
209
*/
226
210
@ Override
227
211
public String getProperty (String name ) {
@@ -236,8 +220,7 @@ public String getProperty(String name) {
236
220
static class ComparisonPropertySource extends StubPropertySource {
237
221
238
222
private static final String USAGE_ERROR =
239
- "ComparisonPropertySource instances are for collection comparison " +
240
- "use only" ;
223
+ "ComparisonPropertySource instances are for use with collection comparison only" ;
241
224
242
225
public ComparisonPropertySource (String name ) {
243
226
super (name );
0 commit comments