20
20
import java .io .InputStream ;
21
21
import java .io .Reader ;
22
22
import java .util .Properties ;
23
+ import java .util .function .Function ;
23
24
24
25
import org .springframework .beans .factory .FactoryBean ;
25
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 used to create a default {@link PropertiesBuilder} instance.
43
50
*
@@ -162,25 +169,16 @@ public PropertiesBuilder(@NonNull PropertiesBuilder builder) {
162
169
this (builder .build ());
163
170
}
164
171
165
- /**
166
- * @inheritDoc
167
- */
168
172
@ Override
169
- public Properties getObject () throws Exception {
173
+ public @ NonNull Properties getObject () throws Exception {
170
174
return build ();
171
175
}
172
176
173
- /**
174
- * @inheritDoc
175
- */
176
177
@ Override
177
- public Class <?> getObjectType () {
178
+ public @ NonNull Class <?> getObjectType () {
178
179
return this .properties != null ? this .properties .getClass () : Properties .class ;
179
180
}
180
181
181
- /**
182
- * @inheritDoc
183
- */
184
182
@ Override
185
183
public boolean isSingleton () {
186
184
return true ;
@@ -194,7 +192,7 @@ public boolean isSingleton() {
194
192
* @return a reference to this {@link PropertiesBuilder}.
195
193
* @see java.util.Properties
196
194
*/
197
- public PropertiesBuilder add (Properties properties ) {
195
+ public @ NonNull PropertiesBuilder add (@ Nullable Properties properties ) {
198
196
199
197
if (!CollectionUtils .isEmpty (properties )) {
200
198
this .properties .putAll (properties );
@@ -211,7 +209,7 @@ public PropertiesBuilder add(Properties properties) {
211
209
* @return a reference to this {@link PropertiesBuilder}.
212
210
* @see org.springframework.data.gemfire.util.PropertiesBuilder
213
211
*/
214
- public PropertiesBuilder add (PropertiesBuilder builder ) {
212
+ public @ NonNull PropertiesBuilder add (@ Nullable PropertiesBuilder builder ) {
215
213
return builder != null ? add (builder .build ()) : this ;
216
214
}
217
215
@@ -223,8 +221,8 @@ public PropertiesBuilder add(PropertiesBuilder builder) {
223
221
* @return a reference to this {@link PropertiesBuilder}.
224
222
* @see #setProperty(String, String)
225
223
*/
226
- public PropertiesBuilder setProperty (String name , Object value ) {
227
- return value != null ? setProperty (name , value . toString ( )) : this ;
224
+ public @ NonNull PropertiesBuilder setProperty (@ NonNull String name , @ NonNull Object value ) {
225
+ return value != null ? setProperty (name , TO_STRING_FUNCTION . apply ( value )) : this ;
228
226
}
229
227
230
228
/**
@@ -237,7 +235,7 @@ public PropertiesBuilder setProperty(String name, Object value) {
237
235
* @see org.springframework.util.StringUtils#arrayToCommaDelimitedString(Object[])
238
236
* @see #setProperty(String, String)
239
237
*/
240
- public PropertiesBuilder setProperty (String name , Object [] values ) {
238
+ public @ NonNull PropertiesBuilder setProperty (@ NonNull String name , @ Nullable Object [] values ) {
241
239
return !ObjectUtils .isEmpty (values ) ? setProperty (name , StringUtils .arrayToCommaDelimitedString (values )) : this ;
242
240
}
243
241
@@ -252,7 +250,7 @@ public PropertiesBuilder setProperty(String name, Object[] values) {
252
250
* @throws IllegalArgumentException if the property name is not specified.
253
251
* @see java.util.Properties#setProperty(String, String)
254
252
*/
255
- public PropertiesBuilder setProperty (String name , String value ) {
253
+ public @ NonNull PropertiesBuilder setProperty (@ NonNull String name , @ NonNull String value ) {
256
254
257
255
Assert .hasText (name , String .format ("Name [%s] must be specified" , name ));
258
256
@@ -275,7 +273,7 @@ public PropertiesBuilder setProperty(String name, String value) {
275
273
* @return a reference to this {@link PropertiesBuilder}.
276
274
* @see #setProperty(String, Object)
277
275
*/
278
- public <T > PropertiesBuilder setPropertyIfNotDefault (String name , Object value , T defaultValue ) {
276
+ public @ NonNull <T > PropertiesBuilder setPropertyIfNotDefault (@ NonNull String name , Object value , T defaultValue ) {
279
277
return defaultValue == null || !defaultValue .equals (value ) ? setProperty (name , value ) : this ;
280
278
}
281
279
@@ -286,7 +284,7 @@ public <T> PropertiesBuilder setPropertyIfNotDefault(String name, Object value,
286
284
* @return a reference to this {@link PropertiesBuilder}.
287
285
* @throws IllegalArgumentException if the property name is not specified.
288
286
*/
289
- public PropertiesBuilder unsetProperty (String name ) {
287
+ public @ NonNull PropertiesBuilder unsetProperty (@ NonNull String name ) {
290
288
291
289
Assert .hasText (name , String .format ("Name [%s] mut be specified" , name ));
292
290
@@ -303,7 +301,7 @@ public PropertiesBuilder unsetProperty(String name) {
303
301
* @param value {@link String} value for the property being set.
304
302
* @return a boolean value indicating whether the given {@link String} value is a valid {@link Properties} value.
305
303
*/
306
- protected boolean isValuable (String value ) {
304
+ protected boolean isValuable (@ Nullable String value ) {
307
305
return StringUtils .hasText (value ) && !"null" .equalsIgnoreCase (value .trim ());
308
306
}
309
307
@@ -313,7 +311,7 @@ protected boolean isValuable(String value) {
313
311
* @return the {@link Properties} object built by this builder.
314
312
* @see java.util.Properties
315
313
*/
316
- public Properties build () {
314
+ public @ NonNull Properties build () {
317
315
return this .properties ;
318
316
}
319
317
}
0 commit comments