15
15
*/
16
16
package io .smallrye .config ;
17
17
18
+ import static io .smallrye .config .EnvConfigSource .ResizableIntArray .MINUS_ONE ;
19
+ import static io .smallrye .config .EnvConfigSource .ResizableIntArray .ZERO ;
18
20
import static io .smallrye .config .ProfileConfigSourceInterceptor .activeName ;
19
21
import static io .smallrye .config .common .utils .ConfigSourceUtil .CONFIG_ORDINAL_KEY ;
20
22
import static io .smallrye .config .common .utils .ConfigSourceUtil .hasProfiledName ;
28
30
import java .io .Serializable ;
29
31
import java .security .PrivilegedAction ;
30
32
import java .util .ArrayList ;
31
- import java .util .Collections ;
33
+ import java .util .Arrays ;
32
34
import java .util .HashMap ;
33
35
import java .util .HashSet ;
34
36
import java .util .Iterator ;
35
37
import java .util .List ;
36
38
import java .util .Map ;
37
- import java .util .Optional ;
38
39
import java .util .Set ;
39
40
import java .util .function .BiConsumer ;
40
41
import java .util .function .Supplier ;
@@ -150,19 +151,8 @@ void matchEnvWithProperties(final List<Map.Entry<String, Supplier<Iterator<Strin
150
151
String prefix = property .getKey ();
151
152
if (StringUtil .isInPath (prefix , activeEnvName )) {
152
153
Iterator <String > names = property .getValue ().get ();
153
- // Priority to match exact key in case multiple candidates (with map patterns)
154
154
while (names .hasNext ()) {
155
- String name = names .next ();
156
- int exactLength = activeEnvName .length () - prefix .length () - 1 ;
157
- if (name .length () == exactLength && matchEnvWithProperty (prefix , name , envName , activeEnvName )) {
158
- break match ;
159
- }
160
- }
161
- // Check everything else
162
- names = property .getValue ().get ();
163
- while (names .hasNext ()) {
164
- String name = names .next ();
165
- if (matchEnvWithProperty (prefix , name , envName , activeEnvName )) {
155
+ if (matchEnvWithProperty (prefix , names .next (), envName , activeEnvName )) {
166
156
break match ;
167
157
}
168
158
}
@@ -173,26 +163,38 @@ void matchEnvWithProperties(final List<Map.Entry<String, Supplier<Iterator<Strin
173
163
174
164
private boolean matchEnvWithProperty (final String prefix , final String property , final String envName ,
175
165
final String activeEnvName ) {
176
- Optional <List <Integer >> prefixDashes = indexOfDashes (
177
- prefix , 0 , prefix .length (),
178
- activeEnvName , 0 , prefix .length ());
179
- Optional <List <Integer >> nameDashes = indexOfDashes (
166
+ int [] prefixDashes = indexOfDashes (prefix , 0 , prefix .length (), activeEnvName , 0 , prefix .length ());
167
+ if (prefixDashes [0 ] == -1 ) {
168
+ return false ;
169
+ }
170
+
171
+ int [] nameDashes = indexOfDashes (
180
172
property , 0 , property .length (),
181
173
activeEnvName , prefix .isEmpty () ? 0 : prefix .length () + 1 ,
182
174
prefix .isEmpty () ? activeEnvName .length () : activeEnvName .length () - prefix .length () - 1 );
183
- if (prefixDashes .isPresent () && nameDashes .isPresent ()) {
184
- StringBuilder sb = new StringBuilder (activeEnvName );
185
- for (Integer dash : prefixDashes .get ()) {
175
+ if (nameDashes [0 ] == -1 ) {
176
+ return false ;
177
+ }
178
+
179
+ if (prefixDashes [0 ] == 0 && nameDashes [0 ] == 0 ) {
180
+ return false ;
181
+ }
182
+
183
+ StringBuilder sb = new StringBuilder (activeEnvName );
184
+ if (prefixDashes [0 ] != 0 ) {
185
+ for (int dash : prefixDashes ) {
186
186
sb .setCharAt (dash , '-' );
187
187
}
188
- for (Integer dash : nameDashes .get ()) {
188
+ }
189
+ if (nameDashes [0 ] != 0 ) {
190
+ for (int dash : nameDashes ) {
189
191
sb .setCharAt (dash , '-' );
190
192
}
191
- if (! activeEnvName . contentEquals ( sb )) {
192
- envVars . getNames (). add (sb . toString ());
193
- envVars .getNames ().remove ( envName );
194
- return true ;
195
- }
193
+ }
194
+ if (! activeEnvName . contentEquals (sb )) {
195
+ envVars .getNames ().add ( sb . toString () );
196
+ envVars . getNames (). remove ( envName ) ;
197
+ return true ;
196
198
}
197
199
return false ;
198
200
}
@@ -210,31 +212,36 @@ private boolean matchEnvWithProperty(final String prefix, final String property,
210
212
*
211
213
* @param property the property name.
212
214
* @param envProperty the Environment Variable name.
213
- * @return an Optional List of indexes from the Environment Variable name that can match a <code>-</code> (dash);
214
- * an Optional with an empty list if properties match but no dashes are found;
215
- * an empty Optional if properties don't match.
215
+ * @return an array of int of indexes from the Environment Variable name that can match a <code>-</code> (dash);
216
+ * an array of int with the int element <code>0</code> if properties match but no dashes are found;
217
+ * an array of int with the int element <code>-1</code> if properties don't match.
216
218
*/
217
- static Optional < List < Integer >> indexOfDashes (final String property , final int offset , final int len ,
219
+ static int [] indexOfDashes (final String property , final int offset , final int len ,
218
220
final String envProperty , final int eoffset , final int elen ) {
219
221
if (property .isEmpty ()) {
220
- return Optional . of ( Collections . emptyList () );
222
+ return ZERO . get ( );
221
223
}
222
224
223
- List < Integer > dashesPosition = new ArrayList <>() ;
225
+ ResizableIntArray dashesPosition = ZERO ;
224
226
int matchPosition = eoffset + elen - 1 ;
225
227
for (int i = offset + len - 1 ; i >= offset ; i --) {
226
228
if (matchPosition == -1 ) {
227
- return Optional . empty ();
229
+ return MINUS_ONE . get ();
228
230
}
229
231
230
232
char c = property .charAt (i );
231
233
if (c == '.' || c == '-' ) {
232
234
char p = envProperty .charAt (matchPosition );
233
235
if (p != '.' && p != '-' ) { // a property coming from env can either be . or -
234
- return Optional . empty ();
236
+ return MINUS_ONE . get ();
235
237
}
236
238
if (c == '-' ) {
237
- dashesPosition .add (matchPosition );
239
+ if (dashesPosition == ZERO ) {
240
+ dashesPosition = new ResizableIntArray (1 );
241
+ } else {
242
+ dashesPosition .ensureCapacity (dashesPosition .get ().length + 1 );
243
+ }
244
+ dashesPosition .get ()[dashesPosition .get ().length - 1 ] = matchPosition ;
238
245
}
239
246
matchPosition --;
240
247
} else if (c == '*' ) { // it's a map - skip to next separator
@@ -253,16 +260,46 @@ static Optional<List<Integer>> indexOfDashes(final String property, final int of
253
260
matchPosition --;
254
261
}
255
262
} else if (c != envProperty .charAt (matchPosition )) {
256
- return Optional . empty ();
263
+ return MINUS_ONE . get ();
257
264
} else {
258
265
matchPosition --;
259
266
}
260
267
}
261
268
if (matchPosition >= eoffset ) {
262
- return Optional .empty ();
269
+ return MINUS_ONE .get ();
270
+ }
271
+
272
+ return dashesPosition .get ();
273
+ }
274
+
275
+ static class ResizableIntArray {
276
+ static final ResizableIntArray ZERO = with (0 );
277
+ static final ResizableIntArray MINUS_ONE = with (-1 );
278
+
279
+ private int [] array ;
280
+
281
+ ResizableIntArray (int initialSize ) {
282
+ this .array = new int [initialSize ];
263
283
}
264
284
265
- return Optional .of (dashesPosition );
285
+ int [] get () {
286
+ return array ;
287
+ }
288
+
289
+ void ensureCapacity (int capacity ) {
290
+ this .array = Arrays .copyOfRange (this .array , 0 , capacity );
291
+ }
292
+
293
+ private static ResizableIntArray with (int i ) {
294
+ ResizableIntArray minusOne = new ResizableIntArray (1 ) {
295
+ @ Override
296
+ void ensureCapacity (final int capacity ) {
297
+ throw new UnsupportedOperationException ();
298
+ }
299
+ };
300
+ minusOne .array [0 ] = i ;
301
+ return minusOne ;
302
+ }
266
303
}
267
304
268
305
Object writeReplace () {
0 commit comments