-
Notifications
You must be signed in to change notification settings - Fork 38k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
11 changed files
with
717 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
spring-core/src/main/java/org/springframework/util/FilteredCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.util; | ||
|
||
import java.util.AbstractCollection; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Collection that filters out values that do not match a predicate. | ||
* This type is used by {@link CompositeMap}. | ||
* @author Arjen Poutsma | ||
* @since 6.2 | ||
* @param <E> the type of elements maintained by this collection | ||
*/ | ||
class FilteredCollection<E> extends AbstractCollection<E> { | ||
|
||
private final Collection<E> delegate; | ||
|
||
private final Predicate<E> filter; | ||
|
||
|
||
public FilteredCollection(Collection<E> delegate, Predicate<E> filter) { | ||
Assert.notNull(delegate, "Delegate must not be null"); | ||
Assert.notNull(filter, "Filter must not be null"); | ||
|
||
this.delegate = delegate; | ||
this.filter = filter; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
int size = 0; | ||
for (E e : this.delegate) { | ||
if (this.filter.test(e)) { | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
@Override | ||
public Iterator<E> iterator() { | ||
return new FilteredIterator<>(this.delegate.iterator(), this.filter); | ||
} | ||
|
||
@Override | ||
public boolean add(E e) { | ||
boolean added = this.delegate.add(e); | ||
return added && this.filter.test(e); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean remove(Object o) { | ||
boolean removed = this.delegate.remove(o); | ||
return removed && this.filter.test((E) o); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean contains(Object o) { | ||
if (this.delegate.contains(o)) { | ||
return this.filter.test((E) o); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
this.delegate.clear(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
spring-core/src/main/java/org/springframework/util/FilteredIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.util; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Predicate; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Iterator that filters out values that do not match a predicate. | ||
* This type is used by {@link CompositeMap}. | ||
* @author Arjen Poutsma | ||
* @since 6.2 | ||
* @param <E> the type of elements returned by this iterator | ||
*/ | ||
final class FilteredIterator<E> implements Iterator<E> { | ||
|
||
private final Iterator<E> delegate; | ||
|
||
private final Predicate<E> filter; | ||
|
||
@Nullable | ||
private E next; | ||
|
||
private boolean nextSet; | ||
|
||
|
||
public FilteredIterator(Iterator<E> delegate, Predicate<E> filter) { | ||
Assert.notNull(delegate, "Delegate must not be null"); | ||
Assert.notNull(filter, "Filter must not be null"); | ||
|
||
this.delegate = delegate; | ||
this.filter = filter; | ||
} | ||
|
||
|
||
@Override | ||
public boolean hasNext() { | ||
if (this.nextSet) { | ||
return true; | ||
} | ||
else { | ||
return setNext(); | ||
} | ||
} | ||
|
||
@Override | ||
public E next() { | ||
if (!this.nextSet) { | ||
if (!setNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
} | ||
this.nextSet = false; | ||
Assert.state(this.next != null, "Next should not be null"); | ||
return this.next; | ||
} | ||
|
||
private boolean setNext() { | ||
while (this.delegate.hasNext()) { | ||
E next = this.delegate.next(); | ||
if (this.filter.test(next)) { | ||
this.next = next; | ||
this.nextSet = true; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
spring-core/src/main/java/org/springframework/util/FilteredMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.util; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Map that filters out values that do not match a predicate. | ||
* This type is used by {@link CompositeMap}. | ||
* @author Arjen Poutsma | ||
* @since 6.2 | ||
* @param <K> the type of keys maintained by this map | ||
* @param <V> the type of mapped values | ||
*/ | ||
final class FilteredMap<K, V> extends AbstractMap<K, V> { | ||
|
||
private final Map<K, V> delegate; | ||
|
||
private final Predicate<K> filter; | ||
|
||
|
||
public FilteredMap(Map<K, V> delegate, Predicate<K> filter) { | ||
Assert.notNull(delegate, "Delegate must not be null"); | ||
Assert.notNull(filter, "Filter must not be null"); | ||
|
||
this.delegate = delegate; | ||
this.filter = filter; | ||
} | ||
|
||
@Override | ||
public Set<Entry<K, V>> entrySet() { | ||
return new FilteredSet<>(this.delegate.entrySet(), entry -> this.filter.test(entry.getKey())); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
int size = 0; | ||
for (K k : keySet()) { | ||
if (this.filter.test(k)) { | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean containsKey(Object key) { | ||
if (this.delegate.containsKey(key)) { | ||
return this.filter.test((K) key); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
@Nullable | ||
public V get(Object key) { | ||
V value = this.delegate.get(key); | ||
if (value != null && this.filter.test((K) key)) { | ||
return value; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public V put(K key, V value) { | ||
V oldValue = this.delegate.put(key, value); | ||
if (oldValue != null && this.filter.test(key)) { | ||
return oldValue; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
@Nullable | ||
public V remove(Object key) { | ||
V oldValue = this.delegate.remove(key); | ||
if (oldValue != null && this.filter.test((K) key)) { | ||
return oldValue; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
this.delegate.clear(); | ||
} | ||
|
||
@Override | ||
public Set<K> keySet() { | ||
return new FilteredSet<>(this.delegate.keySet(), this.filter); | ||
} | ||
|
||
} |
Oops, something went wrong.