org.jboss.resteasy.reactive.common.util.MultivaluedTreeMap Maven / Gradle / Ivy
package org.jboss.resteasy.reactive.common.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;
import jakarta.ws.rs.core.MultivaluedMap;
/**
* @author Bill Burke
*/
public class MultivaluedTreeMap implements QuarkusMultivaluedMap, Serializable {
private static final long serialVersionUID = -5819694356897323354L;
private final Map> map;
public MultivaluedTreeMap() {
map = new TreeMap<>();
}
/**
* Used to create a CaseInsensitiveMap.
*
* @param keyComparator key comparator
*/
public MultivaluedTreeMap(final Comparator keyComparator) {
map = new TreeMap>(keyComparator);
}
public MultivaluedTreeMap(final Map map) {
this();
for (K key : map.keySet()) {
add(key, map.get(key));
}
}
public void add(K key, V value) {
List list = getOrCreate(key);
list.add(value);
}
public V getFirst(K key) {
List list = get(key);
if (list == null || list.size() == 0) {
return null;
}
return list.get(0);
}
public void putSingle(K key, V value) {
List list = getOrCreate(key);
list.clear();
list.add(value);
}
private List getOrCreate(K key) {
List list = this.get(key);
if (list == null) {
list = createValueList(key);
// do not call the version that copies
map.put(key, list);
}
return list;
}
private List createValueList(K key) {
return new ArrayList(1);
}
private List createValueList(List values) {
return new ArrayList(values);
}
public MultivaluedTreeMap clone() {
return clone(this);
}
public static MultivaluedTreeMap clone(MultivaluedMap src) {
MultivaluedTreeMap clone = new MultivaluedTreeMap();
copy(src, clone);
return clone;
}
public static void copy(MultivaluedMap src, MultivaluedMap dest) {
for (K key : src.keySet()) {
List value = src.get(key);
List newValue = new ArrayList(value);
dest.put(key, newValue);
}
}
public static void addAll(MultivaluedMap src, MultivaluedMap dest) {
for (K key : src.keySet()) {
List srcList = src.get(key);
List destList = dest.get(key);
if (destList == null) {
destList = new ArrayList(srcList.size());
dest.put(key, destList);
}
destList.addAll(srcList);
}
}
@Override
public String toString() {
return "[" + toString(this, ",") + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public static String toString(MultivaluedMap, ?> map, String delimiter) {
StringBuilder result = new StringBuilder();
MultivaluedMap, ?> params = map;
String delim = ""; //$NON-NLS-1$
for (Object name : params.keySet()) {
for (Object value : params.get(name)) {
result.append(delim);
if (name == null) {
result.append("null"); //$NON-NLS-1$
} else {
result.append(name.toString());
}
if (value != null) {
result.append('=');
result.append(value.toString());
}
delim = delimiter;
}
}
return result.toString();
}
public void clear() {
map.clear();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
public Set>> entrySet() {
return map.entrySet();
}
public boolean equals(Object o) {
return map.equals(o);
}
public List get(Object key) {
return map.get(key);
}
public int hashCode() {
return map.hashCode();
}
public boolean isEmpty() {
return map.isEmpty();
}
public Set keySet() {
return map.keySet();
}
public List put(K key, List value) {
if (value != null) {
// do not use mutable external data storage internally
value = createValueList(value);
}
return map.put(key, value);
}
public void putAll(Map extends K, ? extends List> t) {
for (Entry extends K, ? extends List> entry : t.entrySet()) {
// call the version that copies
put(entry.getKey(), entry.getValue());
}
}
public List remove(Object key) {
return map.remove(key);
}
public int size() {
return map.size();
}
public Collection> values() {
return map.values();
}
@SuppressWarnings(value = "unchecked")
@Override
public void addAll(K key, V... newValues) {
for (V value : newValues) {
add(key, value);
}
}
@Override
public void addAll(K key, List valueList) {
for (V value : valueList) {
add(key, value);
}
}
@Override
public void addFirst(K key, V value) {
List list = get(key);
if (list == null) {
add(key, value);
return;
} else {
list.add(0, value);
}
}
@Override
public boolean equalsIgnoreValueOrder(MultivaluedMap omap) {
if (this == omap) {
return true;
}
if (!keySet().equals(omap.keySet())) {
return false;
}
for (Map.Entry> e : entrySet()) {
List olist = omap.get(e.getKey());
if (e.getValue().size() != olist.size()) {
return false;
}
for (V v : e.getValue()) {
if (!olist.contains(v)) {
return false;
}
}
}
return true;
}
@Override
public void forEach(BiConsumer super K, ? super List> action) {
map.forEach(action);
}
}