14
14
* limitations under the License.
15
15
*
16
16
*/
17
-
18
17
package org .springframework .data .gemfire .util ;
19
18
20
19
import java .io .IOException ;
21
20
import java .io .InputStream ;
22
21
import java .io .Reader ;
23
22
import java .util .Properties ;
23
+ import java .util .function .Function ;
24
24
25
25
import org .springframework .beans .factory .FactoryBean ;
26
+ import org .springframework .lang .NonNull ;
27
+ import org .springframework .lang .Nullable ;
26
28
import org .springframework .util .Assert ;
27
29
import org .springframework .util .ObjectUtils ;
28
30
import org .springframework .util .StringUtils ;
38
40
@ SuppressWarnings ("unused" )
39
41
public class PropertiesBuilder implements FactoryBean <Properties > {
40
42
43
+ private static final Function <Object , String > TO_STRING_FUNCTION = target ->
44
+ target instanceof Class ? ((Class <?>) target ).getName ()
45
+ : target != null ? target .toString ()
46
+ : String .valueOf (target );
47
+
41
48
/**
42
49
* Factory method to create a default {@link PropertiesBuilder} instance.
43
50
*
@@ -160,28 +167,16 @@ public PropertiesBuilder(PropertiesBuilder builder) {
160
167
this (builder .build ());
161
168
}
162
169
163
- /*
164
- * (non-Javadoc)
165
- * @see org.springframework.beans.factory.FactoryBean#getObject()
166
- */
167
170
@ Override
168
- public Properties getObject () throws Exception {
171
+ public @ NonNull Properties getObject () throws Exception {
169
172
return build ();
170
173
}
171
174
172
- /*
173
- * (non-Javadoc)
174
- * @see org.springframework.beans.factory.FactoryBean#getObjectType()
175
- */
176
175
@ Override
177
- public Class <?> getObjectType () {
176
+ public @ NonNull Class <?> getObjectType () {
178
177
return this .properties != null ? this .properties .getClass () : Properties .class ;
179
178
}
180
179
181
- /*
182
- * (non-Javadoc)
183
- * @see org.springframework.beans.factory.FactoryBean#isSingleton()
184
- */
185
180
@ Override
186
181
public boolean isSingleton () {
187
182
return true ;
@@ -195,7 +190,7 @@ public boolean isSingleton() {
195
190
* @return a reference to this {@link PropertiesBuilder}.
196
191
* @see java.util.Properties
197
192
*/
198
- public PropertiesBuilder add (Properties properties ) {
193
+ public @ NonNull PropertiesBuilder add (@ Nullable Properties properties ) {
199
194
200
195
if (!CollectionUtils .isEmpty (properties )) {
201
196
this .properties .putAll (properties );
@@ -212,7 +207,7 @@ public PropertiesBuilder add(Properties properties) {
212
207
* @return a reference to this {@link PropertiesBuilder}.
213
208
* @see org.springframework.data.gemfire.util.PropertiesBuilder
214
209
*/
215
- public PropertiesBuilder add (PropertiesBuilder builder ) {
210
+ public @ NonNull PropertiesBuilder add (@ Nullable PropertiesBuilder builder ) {
216
211
return builder != null ? add (builder .build ()) : this ;
217
212
}
218
213
@@ -224,8 +219,8 @@ public PropertiesBuilder add(PropertiesBuilder builder) {
224
219
* @return a reference to this {@link PropertiesBuilder}.
225
220
* @see #setProperty(String, String)
226
221
*/
227
- public PropertiesBuilder setProperty (String name , Object value ) {
228
- return value != null ? setProperty (name , value . toString ( )) : this ;
222
+ public @ NonNull PropertiesBuilder setProperty (@ NonNull String name , @ NonNull Object value ) {
223
+ return value != null ? setProperty (name , TO_STRING_FUNCTION . apply ( value )) : this ;
229
224
}
230
225
231
226
/**
@@ -238,7 +233,7 @@ public PropertiesBuilder setProperty(String name, Object value) {
238
233
* @see org.springframework.util.StringUtils#arrayToCommaDelimitedString(Object[])
239
234
* @see #setProperty(String, String)
240
235
*/
241
- public PropertiesBuilder setProperty (String name , Object [] values ) {
236
+ public @ NonNull PropertiesBuilder setProperty (@ NonNull String name , @ Nullable Object [] values ) {
242
237
return !ObjectUtils .isEmpty (values ) ? setProperty (name , StringUtils .arrayToCommaDelimitedString (values )) : this ;
243
238
}
244
239
@@ -253,7 +248,7 @@ public PropertiesBuilder setProperty(String name, Object[] values) {
253
248
* @throws IllegalArgumentException if the property name is not specified.
254
249
* @see java.util.Properties#setProperty(String, String)
255
250
*/
256
- public PropertiesBuilder setProperty (String name , String value ) {
251
+ public @ NonNull PropertiesBuilder setProperty (@ NonNull String name , @ NonNull String value ) {
257
252
258
253
Assert .hasText (name , String .format ("Name [%s] must be specified" , name ));
259
254
@@ -276,7 +271,7 @@ public PropertiesBuilder setProperty(String name, String value) {
276
271
* @return a reference to this {@link PropertiesBuilder}.
277
272
* @see #setProperty(String, Object)
278
273
*/
279
- public <T > PropertiesBuilder setPropertyIfNotDefault (String name , Object value , T defaultValue ) {
274
+ public @ NonNull <T > PropertiesBuilder setPropertyIfNotDefault (@ NonNull String name , Object value , T defaultValue ) {
280
275
return defaultValue == null || !defaultValue .equals (value ) ? setProperty (name , value ) : this ;
281
276
}
282
277
@@ -287,7 +282,7 @@ public <T> PropertiesBuilder setPropertyIfNotDefault(String name, Object value,
287
282
* @return a reference to this {@link PropertiesBuilder}.
288
283
* @throws IllegalArgumentException if the property name is not specified.
289
284
*/
290
- public PropertiesBuilder unsetProperty (String name ) {
285
+ public @ NonNull PropertiesBuilder unsetProperty (@ NonNull String name ) {
291
286
292
287
Assert .hasText (name , String .format ("Name [%s] mut be specified" , name ));
293
288
@@ -304,7 +299,7 @@ public PropertiesBuilder unsetProperty(String name) {
304
299
* @param value {@link String} value for the property being set.
305
300
* @return a boolean value indicating whether the given {@link String} value is a valid {@link Properties} value.
306
301
*/
307
- protected boolean isValuable (String value ) {
302
+ protected boolean isValuable (@ Nullable String value ) {
308
303
return StringUtils .hasText (value ) && !"null" .equalsIgnoreCase (value .trim ());
309
304
}
310
305
@@ -314,7 +309,7 @@ protected boolean isValuable(String value) {
314
309
* @return the {@link Properties} object built by this builder.
315
310
* @see java.util.Properties
316
311
*/
317
- public Properties build () {
312
+ public @ NonNull Properties build () {
318
313
return this .properties ;
319
314
}
320
315
}
0 commit comments