org.jboss.resteasy.reactive.common.util.UnmodifiableMultivaluedMap Maven / Gradle / Ivy
package org.jboss.resteasy.reactive.common.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jakarta.ws.rs.core.MultivaluedMap;
/**
* Unmodifiable implementation of {@link jakarta.ws.rs.core.MultivaluedMap} interface
*
* @author Martin Stefanko
* @version $Revision: 1 $
*/
public class UnmodifiableMultivaluedMap implements MultivaluedMap {
private final MultivaluedMap delegate;
private volatile Set>> entrySet;
public UnmodifiableMultivaluedMap(final MultivaluedMap delegate) {
this(delegate, true);
}
public UnmodifiableMultivaluedMap(final MultivaluedMap delegate, final boolean eagerlyInitializeEntrySet) {
this.delegate = delegate;
if (eagerlyInitializeEntrySet) {
this.entrySet = buildEntrySet();
}
}
private Set>> buildEntrySet() {
Set>> entrySetDelegator = delegate.entrySet();
Set>> entrySetToReturn = new HashSet<>();
for (final java.util.Map.Entry> entry : entrySetDelegator) {
entrySetToReturn.add(new Entry>() {
@Override
public K getKey() {
return entry.getKey();
}
@Override
public List getValue() {
List value = entry.getValue();
return value == null ? null : Collections.unmodifiableList(value);
}
@Override
public List setValue(List value) {
throw new UnsupportedOperationException();
}
});
}
return Collections.unmodifiableSet(entrySetToReturn);
}
@Override
public void putSingle(K k, V v) {
throw new UnsupportedOperationException();
}
@Override
public void add(K k, V v) {
throw new UnsupportedOperationException();
}
@Override
public V getFirst(K key) {
return delegate.getFirst(key);
}
@SuppressWarnings(value = "unchecked")
@Override
public void addAll(K k, V... vs) {
throw new UnsupportedOperationException();
}
@Override
public void addAll(K k, List list) {
throw new UnsupportedOperationException();
}
@Override
public void addFirst(K k, V v) {
throw new UnsupportedOperationException();
}
@Override
public boolean equalsIgnoreValueOrder(MultivaluedMap omap) {
return delegate.equalsIgnoreValueOrder(omap);
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public Set>> entrySet() {
if (this.entrySet == null) {
this.entrySet = buildEntrySet();
}
return this.entrySet;
}
@Override
public List get(Object key) {
List list = delegate.get(key);
return list == null ? null : Collections.unmodifiableList(list);
}
@Override
public Set keySet() {
return Collections.unmodifiableSet(delegate.keySet());
}
@Override
public List put(K key, List value) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map extends K, ? extends List> m) {
throw new UnsupportedOperationException();
}
@Override
public List remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public Collection> values() {
return Collections.unmodifiableCollection(delegate.values());
}
@Override
public boolean containsKey(Object o) {
return delegate.containsKey(o);
}
@Override
public boolean containsValue(Object o) {
return delegate.containsValue(o);
}
@Override
public int size() {
return delegate.size();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public String toString() {
return delegate.toString();
}
}