24
24
import java .util .Set ;
25
25
import java .util .stream .Stream ;
26
26
27
+ import org .springframework .boot .env .OriginTrackedMapPropertySource ;
27
28
import org .springframework .core .env .EnumerablePropertySource ;
28
29
import org .springframework .core .env .MapPropertySource ;
29
30
import org .springframework .core .env .PropertySource ;
44
45
class SpringIterableConfigurationPropertySource extends SpringConfigurationPropertySource
45
46
implements IterableConfigurationPropertySource {
46
47
47
- private volatile Object cacheKey ;
48
+ private volatile CacheKey cacheKey ;
48
49
49
50
private volatile Cache cache ;
50
51
@@ -173,21 +174,37 @@ private static final class CacheKey {
173
174
174
175
private final Object key ;
175
176
176
- private CacheKey (Object key ) {
177
+ private final int size ;
178
+
179
+ private final boolean unmodifiableKey ;
180
+
181
+ private CacheKey (Object key , boolean unmodifiableKey ) {
177
182
this .key = key ;
183
+ this .size = calculateSize (key );
184
+ this .unmodifiableKey = unmodifiableKey ;
178
185
}
179
186
180
187
public CacheKey copy () {
181
- return new CacheKey (copyKey (this .key ));
188
+ return new CacheKey (copyKey (this .key ), this . unmodifiableKey );
182
189
}
183
190
184
191
private Object copyKey (Object key ) {
192
+ if (this .unmodifiableKey ) {
193
+ return key ;
194
+ }
185
195
if (key instanceof Set ) {
186
196
return new HashSet <Object >((Set <?>) key );
187
197
}
188
198
return ((String []) key ).clone ();
189
199
}
190
200
201
+ private int calculateSize (Object key ) {
202
+ if (key instanceof Set ) {
203
+ return ((Set <?>) key ).size ();
204
+ }
205
+ return ((String []) key ).length ;
206
+ }
207
+
191
208
@ Override
192
209
public boolean equals (Object obj ) {
193
210
if (this == obj ) {
@@ -196,7 +213,11 @@ public boolean equals(Object obj) {
196
213
if (obj == null || getClass () != obj .getClass ()) {
197
214
return false ;
198
215
}
199
- return ObjectUtils .nullSafeEquals (this .key , ((CacheKey ) obj ).key );
216
+ CacheKey otherCacheKey = (CacheKey ) obj ;
217
+ if (this .size != otherCacheKey .size ) {
218
+ return false ;
219
+ }
220
+ return ObjectUtils .nullSafeEquals (this .key , otherCacheKey .key );
200
221
}
201
222
202
223
@ Override
@@ -206,9 +227,10 @@ public int hashCode() {
206
227
207
228
public static CacheKey get (EnumerablePropertySource <?> source ) {
208
229
if (source instanceof MapPropertySource ) {
209
- return new CacheKey (((MapPropertySource ) source ).getSource ().keySet ());
230
+ return new CacheKey (((MapPropertySource ) source ).getSource ().keySet (),
231
+ source instanceof OriginTrackedMapPropertySource );
210
232
}
211
- return new CacheKey (source .getPropertyNames ());
233
+ return new CacheKey (source .getPropertyNames (), false );
212
234
}
213
235
214
236
}
0 commit comments