Skip to content

Commit f3ed04c

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 <pstrawderman@netflix.com> (cherry picked from commit 3b6be3d)
1 parent 46ee944 commit f3ed04c

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
@@ -145,26 +145,32 @@ public String toString() {
145145

146146
@Override
147147
public Set<K> keySet() {
148-
if (this.keySet == null) {
149-
this.keySet = Collections.unmodifiableSet(this.delegate.keySet());
148+
Set<K> keySet = this.keySet;
149+
if (keySet == null) {
150+
keySet = Collections.unmodifiableSet(this.delegate.keySet());
151+
this.keySet = keySet;
150152
}
151-
return this.keySet;
153+
return keySet;
152154
}
153155

154156
@Override
155157
public Set<Entry<K, List<V>>> entrySet() {
156-
if (this.entrySet == null) {
157-
this.entrySet = new UnmodifiableEntrySet<>(this.delegate.entrySet());
158+
Set<Entry<K, List<V>>> entrySet = this.entrySet;
159+
if (entrySet == null) {
160+
entrySet = new UnmodifiableEntrySet<>(this.delegate.entrySet());
161+
this.entrySet = entrySet;
158162
}
159-
return this.entrySet;
163+
return entrySet;
160164
}
161165

162166
@Override
163167
public Collection<List<V>> values() {
164-
if (this.values == null) {
165-
this.values = new UnmodifiableValueCollection<>(this.delegate.values());
168+
Collection<List<V>> values = this.values;
169+
if (values == null) {
170+
values = new UnmodifiableValueCollection<>(this.delegate.values());
171+
this.values = values;
166172
}
167-
return this.values;
173+
return values;
168174
}
169175

170176
// unsupported

0 commit comments

Comments
 (0)