From 0eb64bdcde3f8885f17a3446ac521f090aee84e3 Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Fri, 14 Nov 2025 12:55:11 -0800 Subject: [PATCH] Fix single-check idiom in UnmodifiableMultiValueMap Read the respective fields only once in values, entrySet, and keySet methods. Signed-off-by: Patrick Strawderman --- .../util/UnmodifiableMultiValueMap.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/UnmodifiableMultiValueMap.java b/spring-core/src/main/java/org/springframework/util/UnmodifiableMultiValueMap.java index 8db025670c2c..54766301b7a6 100644 --- a/spring-core/src/main/java/org/springframework/util/UnmodifiableMultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/UnmodifiableMultiValueMap.java @@ -141,26 +141,32 @@ public String toString() { @Override public Set keySet() { - if (this.keySet == null) { - this.keySet = Collections.unmodifiableSet(this.delegate.keySet()); + Set keySet = this.keySet; + if (keySet == null) { + keySet = Collections.unmodifiableSet(this.delegate.keySet()); + this.keySet = keySet; } - return this.keySet; + return keySet; } @Override public Set>> entrySet() { - if (this.entrySet == null) { - this.entrySet = new UnmodifiableEntrySet<>(this.delegate.entrySet()); + Set>> entrySet = this.entrySet; + if (entrySet == null) { + entrySet = new UnmodifiableEntrySet<>(this.delegate.entrySet()); + this.entrySet = entrySet; } - return this.entrySet; + return entrySet; } @Override public Collection> values() { - if (this.values == null) { - this.values = new UnmodifiableValueCollection<>(this.delegate.values()); + Collection> values = this.values; + if (values == null) { + values = new UnmodifiableValueCollection<>(this.delegate.values()); + this.values = values; } - return this.values; + return values; } // unsupported @@ -406,7 +412,6 @@ public void clear() { throw new UnsupportedOperationException(); } - private static class UnmodifiableEntrySpliterator implements Spliterator>> { private final Spliterator>> delegate;