Skip to content

Commit 3897ea7

Browse files
committed
Resolve collisions in composite collections
Before this commit, creating a CompositeMap from two maps with the same key has strange results, such as entrySet returning duplicate entries with the same key. After this commit, we give precedence to the first map by filtering out all entries in the second map that are also mapped by the first map. See gh-32245
1 parent d5664ba commit 3897ea7

File tree

11 files changed

+717
-1
lines changed

11 files changed

+717
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(
515515
* Return a (partially unmodifiable) map that combines the provided two
516516
* maps. Invoking {@link Map#put(Object, Object)} or {@link Map#putAll(Map)}
517517
* on the returned map results in an {@link UnsupportedOperationException}.
518+
*
519+
* <p>In the case of a key collision, {@code first} takes precedence over
520+
* {@code second}. In other words, entries in {@code second} with a key
521+
* that is also mapped by {@code first} are effectively ignored.
518522
* @param first the first map to compose
519523
* @param second the second map to compose
520524
* @return a new map that composes the given two maps
@@ -531,6 +535,10 @@ public static <K, V> Map<K, V> compositeMap(Map<K,V> first, Map<K,V> second) {
531535
* {@link UnsupportedOperationException} {@code putFunction} is
532536
* {@code null}. The same applies to {@link Map#putAll(Map)} and
533537
* {@code putAllFunction}.
538+
*
539+
* <p>In the case of a key collision, {@code first} takes precedence over
540+
* {@code second}. In other words, entries in {@code second} with a key
541+
* that is also mapped by {@code first} are effectively ignored.
534542
* @param first the first map to compose
535543
* @param second the second map to compose
536544
* @param putFunction applied when {@code Map::put} is invoked. If

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class CompositeMap<K, V> implements Map<K, V> {
5858
Assert.notNull(first, "First must not be null");
5959
Assert.notNull(second, "Second must not be null");
6060
this.first = first;
61-
this.second = second;
61+
this.second = new FilteredMap<>(second, key -> !this.first.containsKey(key));
6262
this.putFunction = putFunction;
6363
this.putAllFunction = putAllFunction;
6464
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.AbstractCollection;
20+
import java.util.Collection;
21+
import java.util.Iterator;
22+
import java.util.function.Predicate;
23+
24+
/**
25+
* Collection that filters out values that do not match a predicate.
26+
* This type is used by {@link CompositeMap}.
27+
* @author Arjen Poutsma
28+
* @since 6.2
29+
* @param <E> the type of elements maintained by this collection
30+
*/
31+
class FilteredCollection<E> extends AbstractCollection<E> {
32+
33+
private final Collection<E> delegate;
34+
35+
private final Predicate<E> filter;
36+
37+
38+
public FilteredCollection(Collection<E> delegate, Predicate<E> filter) {
39+
Assert.notNull(delegate, "Delegate must not be null");
40+
Assert.notNull(filter, "Filter must not be null");
41+
42+
this.delegate = delegate;
43+
this.filter = filter;
44+
}
45+
46+
@Override
47+
public int size() {
48+
int size = 0;
49+
for (E e : this.delegate) {
50+
if (this.filter.test(e)) {
51+
size++;
52+
}
53+
}
54+
return size;
55+
}
56+
57+
@Override
58+
public Iterator<E> iterator() {
59+
return new FilteredIterator<>(this.delegate.iterator(), this.filter);
60+
}
61+
62+
@Override
63+
public boolean add(E e) {
64+
boolean added = this.delegate.add(e);
65+
return added && this.filter.test(e);
66+
}
67+
68+
@Override
69+
@SuppressWarnings("unchecked")
70+
public boolean remove(Object o) {
71+
boolean removed = this.delegate.remove(o);
72+
return removed && this.filter.test((E) o);
73+
}
74+
75+
@Override
76+
@SuppressWarnings("unchecked")
77+
public boolean contains(Object o) {
78+
if (this.delegate.contains(o)) {
79+
return this.filter.test((E) o);
80+
}
81+
else {
82+
return false;
83+
}
84+
}
85+
86+
@Override
87+
public void clear() {
88+
this.delegate.clear();
89+
}
90+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.Iterator;
20+
import java.util.NoSuchElementException;
21+
import java.util.function.Predicate;
22+
23+
import org.springframework.lang.Nullable;
24+
25+
/**
26+
* Iterator that filters out values that do not match a predicate.
27+
* This type is used by {@link CompositeMap}.
28+
* @author Arjen Poutsma
29+
* @since 6.2
30+
* @param <E> the type of elements returned by this iterator
31+
*/
32+
final class FilteredIterator<E> implements Iterator<E> {
33+
34+
private final Iterator<E> delegate;
35+
36+
private final Predicate<E> filter;
37+
38+
@Nullable
39+
private E next;
40+
41+
private boolean nextSet;
42+
43+
44+
public FilteredIterator(Iterator<E> delegate, Predicate<E> filter) {
45+
Assert.notNull(delegate, "Delegate must not be null");
46+
Assert.notNull(filter, "Filter must not be null");
47+
48+
this.delegate = delegate;
49+
this.filter = filter;
50+
}
51+
52+
53+
@Override
54+
public boolean hasNext() {
55+
if (this.nextSet) {
56+
return true;
57+
}
58+
else {
59+
return setNext();
60+
}
61+
}
62+
63+
@Override
64+
public E next() {
65+
if (!this.nextSet) {
66+
if (!setNext()) {
67+
throw new NoSuchElementException();
68+
}
69+
}
70+
this.nextSet = false;
71+
Assert.state(this.next != null, "Next should not be null");
72+
return this.next;
73+
}
74+
75+
private boolean setNext() {
76+
while (this.delegate.hasNext()) {
77+
E next = this.delegate.next();
78+
if (this.filter.test(next)) {
79+
this.next = next;
80+
this.nextSet = true;
81+
return true;
82+
}
83+
}
84+
return false;
85+
}
86+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.AbstractMap;
20+
import java.util.Map;
21+
import java.util.Set;
22+
import java.util.function.Predicate;
23+
24+
import org.springframework.lang.Nullable;
25+
26+
/**
27+
* Map that filters out values that do not match a predicate.
28+
* This type is used by {@link CompositeMap}.
29+
* @author Arjen Poutsma
30+
* @since 6.2
31+
* @param <K> the type of keys maintained by this map
32+
* @param <V> the type of mapped values
33+
*/
34+
final class FilteredMap<K, V> extends AbstractMap<K, V> {
35+
36+
private final Map<K, V> delegate;
37+
38+
private final Predicate<K> filter;
39+
40+
41+
public FilteredMap(Map<K, V> delegate, Predicate<K> filter) {
42+
Assert.notNull(delegate, "Delegate must not be null");
43+
Assert.notNull(filter, "Filter must not be null");
44+
45+
this.delegate = delegate;
46+
this.filter = filter;
47+
}
48+
49+
@Override
50+
public Set<Entry<K, V>> entrySet() {
51+
return new FilteredSet<>(this.delegate.entrySet(), entry -> this.filter.test(entry.getKey()));
52+
}
53+
54+
@Override
55+
public int size() {
56+
int size = 0;
57+
for (K k : keySet()) {
58+
if (this.filter.test(k)) {
59+
size++;
60+
}
61+
}
62+
return size;
63+
}
64+
65+
@Override
66+
@SuppressWarnings("unchecked")
67+
public boolean containsKey(Object key) {
68+
if (this.delegate.containsKey(key)) {
69+
return this.filter.test((K) key);
70+
}
71+
else {
72+
return false;
73+
}
74+
}
75+
76+
@Override
77+
@SuppressWarnings("unchecked")
78+
@Nullable
79+
public V get(Object key) {
80+
V value = this.delegate.get(key);
81+
if (value != null && this.filter.test((K) key)) {
82+
return value;
83+
}
84+
else {
85+
return null;
86+
}
87+
}
88+
89+
@Override
90+
@Nullable
91+
public V put(K key, V value) {
92+
V oldValue = this.delegate.put(key, value);
93+
if (oldValue != null && this.filter.test(key)) {
94+
return oldValue;
95+
}
96+
else {
97+
return null;
98+
}
99+
}
100+
101+
@Override
102+
@SuppressWarnings("unchecked")
103+
@Nullable
104+
public V remove(Object key) {
105+
V oldValue = this.delegate.remove(key);
106+
if (oldValue != null && this.filter.test((K) key)) {
107+
return oldValue;
108+
}
109+
else {
110+
return null;
111+
}
112+
}
113+
114+
@Override
115+
public void clear() {
116+
this.delegate.clear();
117+
}
118+
119+
@Override
120+
public Set<K> keySet() {
121+
return new FilteredSet<>(this.delegate.keySet(), this.filter);
122+
}
123+
124+
}

0 commit comments

Comments
 (0)