Skip to content

Commit

Permalink
Restore independent LinkedMultiValueMap implementation (without base …
Browse files Browse the repository at this point in the history
…class)

Closes gh-25960

(cherry picked from commit 82835b9)
  • Loading branch information
jhoeller committed Oct 26, 2020
1 parent e913167 commit d5e637a
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 4 deletions.
Expand Up @@ -17,10 +17,14 @@
package org.springframework.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.lang.Nullable;

/**
* Simple implementation of {@link MultiValueMap} that wraps a {@link LinkedHashMap},
Expand All @@ -35,16 +39,18 @@
* @param <K> the key type
* @param <V> the value element type
*/
public class LinkedMultiValueMap<K, V> extends MultiValueMapAdapter<K, V> implements Serializable, Cloneable {
public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializable, Cloneable {

private static final long serialVersionUID = 3801124242820219131L;

private final Map<K, List<V>> targetMap;


/**
* Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}.
*/
public LinkedMultiValueMap() {
super(new LinkedHashMap<>());
this.targetMap = new LinkedHashMap<>();
}

/**
Expand All @@ -53,7 +59,7 @@ public LinkedMultiValueMap() {
* @param initialCapacity the initial capacity
*/
public LinkedMultiValueMap(int initialCapacity) {
super(new LinkedHashMap<>(initialCapacity));
this.targetMap = new LinkedHashMap<>(initialCapacity);
}

/**
Expand All @@ -65,7 +71,140 @@ public LinkedMultiValueMap(int initialCapacity) {
* @see #deepCopy()
*/
public LinkedMultiValueMap(Map<K, List<V>> otherMap) {
super(new LinkedHashMap<>(otherMap));
this.targetMap = new LinkedHashMap<>(otherMap);
}


// MultiValueMap implementation

@Override
@Nullable
public V getFirst(K key) {
List<V> values = this.targetMap.get(key);
return (values != null && !values.isEmpty() ? values.get(0) : null);
}

@Override
public void add(K key, @Nullable V value) {
List<V> values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}

@Override
public void addAll(K key, List<? extends V> values) {
List<V> currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
currentValues.addAll(values);
}

@Override
public void addAll(MultiValueMap<K, V> values) {
for (Entry<K, List<V>> entry : values.entrySet()) {
addAll(entry.getKey(), entry.getValue());
}
}

@Override
public void set(K key, @Nullable V value) {
List<V> values = new LinkedList<>();
values.add(value);
this.targetMap.put(key, values);
}

@Override
public void setAll(Map<K, V> values) {
values.forEach(this::set);
}

@Override
public Map<K, V> toSingleValueMap() {
Map<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
this.targetMap.forEach((key, values) -> {
if (values != null && !values.isEmpty()) {
singleValueMap.put(key, values.get(0));
}
});
return singleValueMap;
}


// Map implementation

@Override
public int size() {
return this.targetMap.size();
}

@Override
public boolean isEmpty() {
return this.targetMap.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return this.targetMap.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}

@Override
@Nullable
public List<V> get(Object key) {
return this.targetMap.get(key);
}

@Override
@Nullable
public List<V> put(K key, List<V> value) {
return this.targetMap.put(key, value);
}

@Override
@Nullable
public List<V> remove(Object key) {
return this.targetMap.remove(key);
}

@Override
public void putAll(Map<? extends K, ? extends List<V>> map) {
this.targetMap.putAll(map);
}

@Override
public void clear() {
this.targetMap.clear();
}

@Override
public Set<K> keySet() {
return this.targetMap.keySet();
}

@Override
public Collection<List<V>> values() {
return this.targetMap.values();
}

@Override
public Set<Entry<K, List<V>>> entrySet() {
return this.targetMap.entrySet();
}

@Override
public boolean equals(@Nullable Object other) {
return (this == other || this.targetMap.equals(other));
}

@Override
public int hashCode() {
return this.targetMap.hashCode();
}

@Override
public String toString() {
return this.targetMap.toString();
}


Expand Down
Expand Up @@ -48,6 +48,8 @@ class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializable {
}


// MultiValueMap implementation

@Override
@Nullable
public V getFirst(K key) {
Expand Down Expand Up @@ -97,6 +99,9 @@ public Map<K, V> toSingleValueMap() {
return singleValueMap;
}


// Map implementation

@Override
public int size() {
return this.targetMap.size();
Expand Down

0 comments on commit d5e637a

Please sign in to comment.