org.jboss.resteasy.util.CaseInsensitiveMap Maven / Gradle / Ivy
The newest version!
package org.jboss.resteasy.util;
import org.jboss.resteasy.specimpl.MultivaluedTreeMap;
import javax.ws.rs.core.MultivaluedHashMap;
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.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author Bill Burke
* @version $Revision: 1 $
*/
@SuppressWarnings("unchecked")
//public class CaseInsensitiveMap implements MultivaluedMap, Serializable
public class CaseInsensitiveMap extends MultivaluedTreeMap
{
public static final Comparator CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator, java.io.Serializable {
public int compare(String s1, String s2) {
if (s1 == s2) return 0;
int n1 = 0;
// null check is different than JDK version of this method
if (s1 != null) n1 = s1.length();
int n2 = 0;
if (s2 != null) n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
return n1 - n2;
}
}
public CaseInsensitiveMap()
{
super(CASE_INSENSITIVE_ORDER);
}
/*
private static class KeySetWrapper implements Set
{
private Set keys;
public KeySetWrapper(Set keys)
{
this.keys = keys;
}
public int size()
{
return keys.size();
}
public boolean isEmpty()
{
return keys.isEmpty();
}
public boolean contains(Object o)
{
return keys.contains(new CaseInsensitiveKey((String) o));
}
public Iterator iterator()
{
return new Iterator()
{
private Iterator it = keys.iterator();
public boolean hasNext()
{
return it.hasNext();
}
public String next()
{
return it.next().key;
}
public void remove()
{
it.remove();
}
};
}
public Object[] toArray()
{
return toArray(new String[keys.size()]);
}
public T[] toArray(T[] ks)
{
int i = 0;
for (CaseInsensitiveKey key : keys)
{
ks[i++] = (T) key.key;
}
return ks;
}
public boolean add(String key)
{
return keys.add(new CaseInsensitiveKey(key));
}
public boolean remove(Object o)
{
return keys.remove(new CaseInsensitiveKey(o.toString()));
}
public boolean containsAll(Collection> objects)
{
HashSet objs = new HashSet();
for (Object o : objects)
{
objs.add(new CaseInsensitiveKey(o.toString()));
}
return keys.containsAll(objs);
}
public boolean addAll(Collection extends String> objects)
{
HashSet objs = new HashSet();
for (Object o : objects)
{
objs.add(new CaseInsensitiveKey(o.toString()));
}
return keys.addAll(objs);
}
public boolean retainAll(Collection> objects)
{
HashSet objs = new HashSet();
for (Object o : objects)
{
objs.add(new CaseInsensitiveKey(o.toString()));
}
return keys.retainAll(objects);
}
public boolean removeAll(Collection> objects)
{
HashSet objs = new HashSet();
for (Object o : objects)
{
objs.add(new CaseInsensitiveKey(o.toString()));
}
return keys.removeAll(objects);
}
public void clear()
{
keys.clear();
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || !(o instanceof Set)) return false;
Set strings = (Set) o;
if (size() != strings.size()) return false;
for (String string : strings)
{
if (!contains(string)) return false;
}
return true;
}
@Override
public int hashCode()
{
return keys.hashCode();
}
}
private static class EntrySetWrapper implements Set>
{
private Set> entrySet;
private EntrySetWrapper(Set> entrySet)
{
this.entrySet = entrySet;
}
public int size()
{
return entrySet.size();
}
public boolean isEmpty()
{
return entrySet.isEmpty();
}
public boolean contains(Object o)
{
if (!(o instanceof Entry)) return false;
return entrySet.contains(new EntryWrapper((Entry) o));
}
public Iterator> iterator()
{
return new Iterator>()
{
Iterator> it = entrySet.iterator();
public boolean hasNext()
{
return it.hasNext();
}
public Entry next()
{
return new EntryDelegate(it.next());
}
public void remove()
{
it.remove();
}
};
}
public Object[] toArray()
{
Entry[] array = new Entry[entrySet.size()];
return toArray(array);
}
public T[] toArray(T[] ts)
{
Entry[] array = (Entry[]) ts;
int i = 0;
for (Entry entry : entrySet)
{
array[i++] = new EntryDelegate(entry);
}
return (T[]) array;
}
public boolean add(Entry stringVEntry)
{
entrySet.add(new EntryWrapper(stringVEntry));
return false;
}
public boolean remove(Object o)
{
return entrySet.remove(new EntryWrapper((Entry) o));
}
public boolean containsAll(Collection> objects)
{
Collection> list = (Collection>) objects;
HashSet> set = new HashSet>();
for (Entry entry : list)
{
set.add(new EntryWrapper(entry));
}
return entrySet.containsAll(set);
}
public boolean addAll(Collection extends Entry> entries)
{
HashSet> set = new HashSet>();
for (Entry entry : entries)
{
set.add(new EntryWrapper(entry));
}
return entrySet.addAll(set);
}
public boolean retainAll(Collection> objects)
{
Collection> list = (Collection>) objects;
HashSet> set = new HashSet>();
for (Entry entry : list)
{
set.add(new EntryWrapper(entry));
}
return entrySet.retainAll(set);
}
public boolean removeAll(Collection> objects)
{
Collection> list = (Collection>) objects;
HashSet> set = new HashSet>();
for (Entry entry : list)
{
set.add(new EntryWrapper(entry));
}
return entrySet.removeAll(set);
}
public void clear()
{
entrySet.clear();
}
private class EntryWrapper implements Entry
{
private CaseInsensitiveKey key;
private T value;
public EntryWrapper(Entry entry)
{
key = new CaseInsensitiveKey(entry.getKey());
value = entry.getValue();
}
public CaseInsensitiveKey getKey()
{
return key;
}
public T getValue()
{
return value;
}
public T setValue(T v)
{
T tmp = value;
value = v;
return tmp;
}
}
private class EntryDelegate implements Entry
{
private Entry entry;
private EntryDelegate(Entry entry)
{
this.entry = entry;
}
public final String getKey()
{
return entry.getKey().key;
}
public final T getValue()
{
return entry.getValue();
}
public final T setValue(T v)
{
return entry.setValue(v);
}
}
}
private static class CaseInsensitiveKey implements Serializable
{
private static final long serialVersionUID = 6249456709345532524L;
private String key;
private int hashCode = 0;
private String tlc;
private CaseInsensitiveKey(String key)
{
this.key = key;
if (key != null)
{
tlc = key.toLowerCase();
hashCode = tlc.hashCode();
}
}
public final boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CaseInsensitiveKey that = (CaseInsensitiveKey) o;
if (tlc == that.tlc) return true;
if (tlc == null || that.tlc == null) return false;
return tlc.equals(that.tlc);
}
public final int hashCode()
{
return hashCode;
}
public final String toString()
{
return key;
}
}
private MultivaluedMap map = new MultivaluedHashMap();
public void putSingle(String key, V value)
{
map.putSingle(new CaseInsensitiveKey(key), value);
}
public void add(String key, V value)
{
map.add(new CaseInsensitiveKey(key), value);
}
public V getFirst(String key)
{
return map.getFirst(new CaseInsensitiveKey(key));
}
public final int size()
{
return map.size();
}
public boolean isEmpty()
{
return map.isEmpty();
}
public boolean containsKey(Object o)
{
String key = o == null ? null : o.toString();
return map.containsKey(new CaseInsensitiveKey(key));
}
public boolean containsValue(Object o)
{
return map.containsValue(o);
}
public List get(Object o)
{
String key = o == null ? null : o.toString();
return map.get(new CaseInsensitiveKey(key));
}
public List put(String s, List vs)
{
return map.put(new CaseInsensitiveKey(s), vs);
}
public List remove(Object o)
{
String key = o == null ? null : o.toString();
return map.remove(new CaseInsensitiveKey(key));
}
private List getMapList(CaseInsensitiveKey key)
{
List list = map.get(key);
if (list == null)
map.put(key, list = new ArrayList());
return list;
}
public final void putAll(Map otherMap)
{
if (otherMap instanceof CaseInsensitiveMap)
{
CaseInsensitiveMap otherCaseInsensitiveMap = ((CaseInsensitiveMap) otherMap);
Set>> es = otherCaseInsensitiveMap.map.entrySet();
for (Entry> entry : es)
{
getMapList(entry.getKey()).addAll(entry.getValue());
}
}
else
{
for (Map.Entry> entry : (Set>>) otherMap.entrySet())
{
getMapList(new CaseInsensitiveKey(entry.getKey())).addAll(entry.getValue());
}
}
}
public void clear()
{
map.clear();
}
public Set keySet()
{
return new KeySetWrapper(map.keySet());
}
public Collection> values()
{
return map.values();
}
public Set>> entrySet()
{
return new EntrySetWrapper>(map.entrySet());
}
@Override
public void addAll(String key, V... newValues)
{
for (V value : newValues)
{
add(key, value);
}
}
@Override
public void addAll(String key, List valueList)
{
for (V value : valueList)
{
add(key, value);
}
}
@Override
public void addFirst(String 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 (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;
}
*/
}