Skip to content

Commit

Permalink
Resolve collisions in composite collections
Browse files Browse the repository at this point in the history
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
poutsma committed May 2, 2024
1 parent d5664ba commit 3897ea7
Show file tree
Hide file tree
Showing 11 changed files with 717 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(
* Return a (partially unmodifiable) map that combines the provided two
* maps. Invoking {@link Map#put(Object, Object)} or {@link Map#putAll(Map)}
* on the returned map results in an {@link UnsupportedOperationException}.
*
* <p>In the case of a key collision, {@code first} takes precedence over
* {@code second}. In other words, entries in {@code second} with a key
* that is also mapped by {@code first} are effectively ignored.
* @param first the first map to compose
* @param second the second map to compose
* @return a new map that composes the given two maps
Expand All @@ -531,6 +535,10 @@ public static <K, V> Map<K, V> compositeMap(Map<K,V> first, Map<K,V> second) {
* {@link UnsupportedOperationException} {@code putFunction} is
* {@code null}. The same applies to {@link Map#putAll(Map)} and
* {@code putAllFunction}.
*
* <p>In the case of a key collision, {@code first} takes precedence over
* {@code second}. In other words, entries in {@code second} with a key
* that is also mapped by {@code first} are effectively ignored.
* @param first the first map to compose
* @param second the second map to compose
* @param putFunction applied when {@code Map::put} is invoked. If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class CompositeMap<K, V> implements Map<K, V> {
Assert.notNull(first, "First must not be null");
Assert.notNull(second, "Second must not be null");
this.first = first;
this.second = second;
this.second = new FilteredMap<>(second, key -> !this.first.containsKey(key));
this.putFunction = putFunction;
this.putAllFunction = putAllFunction;
}
Expand Down
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();
}
}
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 spring-core/src/main/java/org/springframework/util/FilteredMap.java
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);
}

}
Loading

0 comments on commit 3897ea7

Please sign in to comment.