com.amazonaws.serverless.proxy.model.MultiValuedTreeMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-serverless-java-container-core Show documentation
Show all versions of aws-serverless-java-container-core Show documentation
Allows Java applications written for a servlet container to run in AWS Lambda
package com.amazonaws.serverless.proxy.model;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.ws.rs.core.MultivaluedMap;
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;
/**
* Simple implementation of a multi valued tree map to use for case-insensitive headers
*
* @param The type for the map key
* @param The type for the map values
*/
public class MultiValuedTreeMap implements MultivaluedMap, Serializable, Cloneable {
private static final long serialVersionUID = 42L;
private final Map> map;
public MultiValuedTreeMap() {
map = new TreeMap<>();
}
public MultiValuedTreeMap(Comparator comparator) {
map = new TreeMap<>(comparator);
}
@Override
public void add(Key key, Value value) {
List values = findKey(key);
values.add(value);
}
@Override
public Value getFirst(Key key) {
List values = get(key);
if (values == null || values.size() == 0) {
return null;
}
return values.get(0);
}
@Override
public void putSingle(Key key, Value value) {
List values = findKey(key);
values.clear();
values.add(value);
}
@Override
public void clear() {
map.clear();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public Set>> entrySet() {
return map.entrySet();
}
public boolean equals(Object o) {
return map.equals(o);
}
@Override
public List get(Object key) {
return map.get(key);
}
public int hashCode() {
return map.hashCode();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public List put(Key key, List value) {
return map.put(key, value);
}
@Override
public void putAll(Map extends Key, ? extends List> t) {
map.putAll(t);
}
@Override
public List remove(Object key) {
return map.remove(key);
}
@Override
public int size() {
return map.size();
}
@Override
public Collection> values() {
return map.values();
}
@Override
public void addAll(Key key, Value... newValues) {
for (Value value : newValues) {
add(key, value);
}
}
@Override
public void addAll(Key key, List valueList) {
for (Value value : valueList) {
add(key, value);
}
}
@Override
public void addFirst(Key key, Value value) {
List values = get(key);
if (values == null) {
add(key, value);
return;
} else {
values.add(0, value);
}
}
@Override
public boolean equalsIgnoreValueOrder(MultivaluedMap vmap) {
if (this == vmap) {
return true;
}
if (!keySet().equals(vmap.keySet())) {
return false;
}
for (Map.Entry> e : entrySet()) {
List olist = vmap.get(e.getKey());
if (e.getValue().size() != olist.size()) {
return false;
}
for (Value v : e.getValue()) {
if (!olist.contains(v)) {
return false;
}
}
}
return true;
}
private List findKey(Key key) {
List values = this.get(key);
if (values == null) {
values = new ArrayList<>();
put(key, values);
}
return values;
}
@Override
@SuppressFBWarnings("CN_IDIOM_NO_SUPER_CALL")
public MultiValuedTreeMap clone() {
MultiValuedTreeMap clone = new MultiValuedTreeMap<>();
for (Key key : keySet()) {
List value = get(key);
List newValue = new ArrayList<>(value);
clone.put(key, newValue);
}
return clone;
}
public String toString() {
StringBuilder result = new StringBuilder();
String delim = ",";
for (Object name : keySet()) {
for (Object value : 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());
}
}
}
return "[" + result.toString() + "]";
}
}