11/*
2- * Copyright (c) 2020, 2023 Oracle and/or its affiliates.
2+ * Copyright (c) 2020, 2024 Oracle and/or its affiliates.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -112,10 +112,10 @@ public ConfigValue getConfigValue(String key) {
112112 profiledKey = "%" + configProfile + "." + key ;
113113 }
114114 if (configProfile == null ) {
115- return findConfigValue (key , Optional . empty () )
115+ return findConfigValue (key , null )
116116 .orElseGet (() -> new ConfigValueImpl (key , null , null , null , 0 ));
117117 }
118- return findConfigValue (key , Optional . of ( profiledKey ) )
118+ return findConfigValue (key , profiledKey )
119119 .orElseGet (() -> new ConfigValueImpl (key , null , null , null , 0 ));
120120 }
121121
@@ -129,15 +129,11 @@ public <T> T getValue(String propertyName, Class<T> propertyType) {
129129 @ SuppressWarnings ("unchecked" )
130130 @ Override
131131 public <T > Optional <T > getOptionalValue (String propertyName , Class <T > propertyType ) {
132- String profiledPropertyName = null ;
133- if (configProfile != null ) {
134- profiledPropertyName = "%" + configProfile + "." + propertyName ;
135- }
136132 if (configProfile == null ) {
137- return optionalValue (propertyName , propertyType , Optional .empty ());
133+ return optionalValue (propertyName , propertyType , null );
134+ } else {
135+ return optionalValue (propertyName , propertyType , "%" + configProfile + "." + propertyName );
138136 }
139-
140- return optionalValue (propertyName , propertyType , Optional .of (profiledPropertyName ));
141137 }
142138
143139 @ SuppressWarnings ("unchecked" )
@@ -192,13 +188,13 @@ private <T> Optional<T> arrayValue(String propertyName, Class<T> propertyType) {
192188 }
193189 }
194190
195- private <T > Optional <T > optionalValue (String propertyName , Class <T > propertyType , Optional < String > profiledPropertyName ) {
191+ private <T > Optional <T > optionalValue (String propertyName , Class <T > propertyType , String profiledPropertyName ) {
196192 // let's resolve arrays
197193 if (propertyType .isArray ()) {
198194 Optional <T > array = Optional .empty ();
199- if (profiledPropertyName . isPresent () ) {
195+ if (profiledPropertyName != null ) {
200196 // Try first with profiled property
201- array = arrayValue (profiledPropertyName . get () , propertyType );
197+ array = arrayValue (profiledPropertyName , propertyType );
202198 }
203199 if (array .isEmpty ()) {
204200 array = arrayValue (propertyName , propertyType );
@@ -334,14 +330,14 @@ private <T> T convert(String propertyName, Class<T> type, String value) {
334330 }
335331 }
336332
337- private Optional <ConfigValue > findConfigValue (String propertyName , Optional < String > profiledPropertyName ) {
333+ private Optional <ConfigValue > findConfigValue (String propertyName , String profiledPropertyName ) {
338334 for (ConfigSource source : sources ) {
339335 String selectedProperty = null ;
340336 String value = null ;
341- if (profiledPropertyName . isPresent () ) {
337+ if (profiledPropertyName != null ) {
342338 // Try profiled property first
343- selectedProperty = profiledPropertyName . get () ;
344- value = source .getValue (profiledPropertyName . get () );
339+ selectedProperty = profiledPropertyName ;
340+ value = source .getValue (profiledPropertyName );
345341 }
346342 if (value == null ) {
347343 selectedProperty = propertyName ;
@@ -355,26 +351,27 @@ private Optional<ConfigValue> findConfigValue(String propertyName, Optional<Stri
355351 String rawValue = value ;
356352 String name = source .getName ();
357353 int ordinal = source .getOrdinal ();
358- final String propName = selectedProperty ;
354+ // Required final variable, as it is used in lambdas
355+ final String selectedPropertyFinal = selectedProperty ;
359356 if (value .isEmpty ()) {
360357 if (LOGGER .isLoggable (Level .TRACE )) {
361- LOGGER .log (Level .TRACE , "Found property " + propName
358+ LOGGER .log (Level .TRACE , "Found property " + selectedPropertyFinal
362359 + " in source " + source .getName ()
363360 + " and it is empty (removed)" );
364361 }
365- return Optional .of ( new ConfigValueImpl ( propName , null , rawValue , name , ordinal ) );
362+ return Optional .empty ( );
366363 }
367364
368365 if (LOGGER .isLoggable (Level .TRACE )) {
369- LOGGER .log (Level .TRACE , "Found property " + propName + " in source " + source .getName ());
366+ LOGGER .log (Level .TRACE , "Found property " + selectedPropertyFinal + " in source " + source .getName ());
370367 }
371368
372369 try {
373- return applyFilters (propName , value )
374- .map (it -> resolveReferences (propName , it ))
375- .map (it -> new ConfigValueImpl (propName , it , rawValue , name , ordinal ));
370+ return applyFilters (selectedPropertyFinal , value )
371+ .map (it -> resolveReferences (selectedPropertyFinal , it ))
372+ .map (it -> new ConfigValueImpl (selectedPropertyFinal , it , rawValue , name , ordinal ));
376373 } catch (NoSuchElementException e ) {
377- return Optional .of (new ConfigValueImpl (propName , null , rawValue , name , ordinal ));
374+ return Optional .of (new ConfigValueImpl (selectedPropertyFinal , null , rawValue , name , ordinal ));
378375 }
379376 }
380377
0 commit comments