Skip to content

Commit

Permalink
Use composite collections in attribute merging
Browse files Browse the repository at this point in the history
This commit introduces composite collections (i.e. Collection, Set, Map)
and uses these composites in request predicates, where before new
collections were instantiated.

Closes gh-32245
  • Loading branch information
poutsma committed Feb 22, 2024
1 parent 89d746d commit aee03c5
Show file tree
Hide file tree
Showing 9 changed files with 953 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import org.springframework.lang.Nullable;

Expand Down Expand Up @@ -506,4 +508,42 @@ public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(
return new UnmodifiableMultiValueMap<>(targetMap);
}

/**
* 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}.
* @param first the first map to compose
* @param second the second map to compose
* @return a new map that composes the given two maps
* @since 6.2
*/
public static <K, V> Map<K, V> compositeMap(Map<K,V> first, Map<K,V> second) {
return new CompositeMap<>(first, second);
}

/**
* Return a map that combines the provided maps. Invoking
* {@link Map#put(Object, Object)} on the returned map will apply
* {@code putFunction}, or will throw an
* {@link UnsupportedOperationException} {@code putFunction} is
* {@code null}. The same applies to {@link Map#putAll(Map)} and
* {@code putAllFunction}.
* @param first the first map to compose
* @param second the second map to compose
* @param putFunction applied when {@code Map::put} is invoked. If
* {@code null}, {@code Map::put} throws an
* {@code UnsupportedOperationException}.
* @param putAllFunction applied when {@code Map::putAll} is invoked. If
* {@code null}, {@code Map::putAll} throws an
* {@code UnsupportedOperationException}.
* @return a new map that composes the give maps
* @since 6.2
*/
public static <K, V> Map<K, V> compositeMap(Map<K,V> first, Map<K,V> second,
@Nullable BiFunction<K, V, V> putFunction,
@Nullable Consumer<Map<K, V>> putAllFunction) {

return new CompositeMap<>(first, second, putFunction, putAllFunction);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;


/**
* Composite collection that combines two other collections. This type is only
* exposed through {@link CompositeMap#values()}.
*
* @author Arjen Poutsma
* @since 6.2
* @param <E> the type of elements maintained by this collection
*/
class CompositeCollection<E> implements Collection<E> {

private final Collection<E> first;

private final Collection<E> second;


CompositeCollection(Collection<E> first, Collection<E> second) {
Assert.notNull(first, "First must not be null");
Assert.notNull(second, "Second must not be null");
this.first = first;
this.second = second;
}

@Override
public int size() {
return this.first.size() + this.second.size();
}

@Override
public boolean isEmpty() {
return this.first.isEmpty() && this.second.isEmpty();
}

@Override
public boolean contains(Object o) {
if (this.first.contains(o)) {
return true;
}
else {
return this.second.contains(o);
}
}

@Override
public Iterator<E> iterator() {
CompositeIterator<E> iterator = new CompositeIterator<>();
iterator.add(this.first.iterator());
iterator.add(this.second.iterator());
return iterator;
}

@Override
public Object[] toArray() {
Object[] result = new Object[size()];
Object[] firstArray = this.first.toArray();
Object[] secondArray = this.second.toArray();
System.arraycopy(firstArray, 0, result, 0, firstArray.length);
System.arraycopy(secondArray, 0, result, firstArray.length, secondArray.length);
return result;
}

@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int size = this.size();
T[] result;
if (a.length >= size) {
result = a;
}
else {
result = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
}

int idx = 0;
for (E e : this) {
result[idx++] = (T) e;
}
if (result.length > size) {
result[size] = null;
}
return result;
}

@Override
public boolean add(E e) {
throw new UnsupportedOperationException();
}

@Override
public boolean remove(Object o) {
boolean firstResult = this.first.remove(o);
boolean secondResult = this.second.remove(o);
return firstResult || secondResult;
}

@Override
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!contains(o)) {
return false;
}
}
return true;
}

@Override
public boolean addAll(Collection<? extends E> c) {
boolean changed = false;
for (E e : c) {
if (add(e)) {
changed = true;
}
}
return changed;
}

@Override
public boolean removeAll(Collection<?> c) {
if (c.isEmpty()) {
return false;
}
boolean firstResult = this.first.removeAll(c);
boolean secondResult = this.second.removeAll(c);

return firstResult || secondResult;
}

@Override
public boolean retainAll(Collection<?> c) {
boolean firstResult = this.first.retainAll(c);
boolean secondResult = this.second.retainAll(c);

return firstResult || secondResult;
}

@Override
public void clear() {
this.first.clear();
this.second.clear();
}
}
Loading

0 comments on commit aee03c5

Please sign in to comment.