Skip to content

Commit 3b6be3d

Browse files
kilinksbrannen
authored andcommitted
Fix single-check idiom in UnmodifiableMultiValueMap
Read the respective fields only once in the values(), entrySet(), and keySet() methods. Closes gh-35822 Signed-off-by: Patrick Strawderman <[email protected]>
1 parent 6115c39 commit 3b6be3d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

spring-core/src/main/java/org/springframework/util/UnmodifiableMultiValueMap.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,32 @@ public String toString() {
141141

142142
@Override
143143
public Set<K> keySet() {
144-
if (this.keySet == null) {
145-
this.keySet = Collections.unmodifiableSet(this.delegate.keySet());
144+
Set<K> keySet = this.keySet;
145+
if (keySet == null) {
146+
keySet = Collections.unmodifiableSet(this.delegate.keySet());
147+
this.keySet = keySet;
146148
}
147-
return this.keySet;
149+
return keySet;
148150
}
149151

150152
@Override
151153
public Set<Entry<K, List<V>>> entrySet() {
152-
if (this.entrySet == null) {
153-
this.entrySet = new UnmodifiableEntrySet<>(this.delegate.entrySet());
154+
Set<Entry<K, List<V>>> entrySet = this.entrySet;
155+
if (entrySet == null) {
156+
entrySet = new UnmodifiableEntrySet<>(this.delegate.entrySet());
157+
this.entrySet = entrySet;
154158
}
155-
return this.entrySet;
159+
return entrySet;
156160
}
157161

158162
@Override
159163
public Collection<List<V>> values() {
160-
if (this.values == null) {
161-
this.values = new UnmodifiableValueCollection<>(this.delegate.values());
164+
Collection<List<V>> values = this.values;
165+
if (values == null) {
166+
values = new UnmodifiableValueCollection<>(this.delegate.values());
167+
this.values = values;
162168
}
163-
return this.values;
169+
return values;
164170
}
165171

166172
// unsupported

0 commit comments

Comments
 (0)