33
44package com .azure .core .util ;
55
6+ import java .util .Optional ;
67import java .util .concurrent .ConcurrentHashMap ;
78import java .util .concurrent .ConcurrentMap ;
89import java .util .function .Function ;
@@ -214,7 +215,7 @@ public class Configuration implements Cloneable {
214215 @ SuppressWarnings ("StaticInitializerReferencesSubClass" )
215216 public static final Configuration NONE = new NoopConfiguration ();
216217
217- private final ConcurrentMap <String , String > configurations ;
218+ private final ConcurrentMap <String , Optional < String > > configurations ;
218219
219220 /**
220221 * Constructs a configuration containing the known Azure properties constants.
@@ -224,7 +225,7 @@ public Configuration() {
224225 loadBaseConfiguration (this );
225226 }
226227
227- private Configuration (ConcurrentMap <String , String > configurations ) {
228+ private Configuration (ConcurrentMap <String , Optional < String > > configurations ) {
228229 this .configurations = new ConcurrentHashMap <>(configurations );
229230 }
230231
@@ -300,18 +301,15 @@ public <T> T get(String name, Function<String, T> converter) {
300301 * variable, in that order, if found, otherwise null.
301302 */
302303 private String getOrLoad (String name ) {
303- String value = configurations .get (name );
304- if (value != null ) {
305- return value ;
304+ Optional < String > cached = configurations .get (name );
305+ if (cached != null ) {
306+ return cached . orElse ( null ) ;
306307 }
307308
308- value = load (name );
309- if (value != null ) {
310- configurations .put (name , value );
311- return value ;
312- }
309+ String value = load (name );
310+ configurations .put (name , Optional .ofNullable (value ));
313311
314- return null ;
312+ return value ;
315313 }
316314
317315 /*
@@ -350,7 +348,7 @@ String loadFromProperties(String name) {
350348 * @return The updated Configuration object.
351349 */
352350 public Configuration put (String name , String value ) {
353- configurations .put (name , value );
351+ configurations .put (name , Optional . of ( value ) );
354352 return this ;
355353 }
356354
@@ -363,7 +361,8 @@ public Configuration put(String name, String value) {
363361 * @return The configuration if it previously existed, otherwise null.
364362 */
365363 public String remove (String name ) {
366- return configurations .remove (name );
364+ Optional <String > value = configurations .remove (name );
365+ return (value != null && value .isPresent ()) ? value .get () : null ;
367366 }
368367
369368 /**
@@ -376,7 +375,8 @@ public String remove(String name) {
376375 * @return True if the configuration exists, otherwise false.
377376 */
378377 public boolean contains (String name ) {
379- return configurations .containsKey (name );
378+ Optional <String > value = configurations .get (name );
379+ return value != null && value .isPresent ();
380380 }
381381
382382 /**
0 commit comments