package io.netty.util.collection;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
public final class IntCollections {
private static final IntObjectMap<Object> EMPTY_MAP = new EmptyMap();
private IntCollections() {
}
@SuppressWarnings("unchecked")
public static <V> IntObjectMap<V> emptyMap() {
return (IntObjectMap<V>) EMPTY_MAP;
}
public static <V> IntObjectMap<V> unmodifiableMap(final IntObjectMap<V> map) {
return new UnmodifiableMap<V>(map);
}
private static final class EmptyMap implements IntObjectMap<Object> {
@Override
public Object get(int key) {
return null;
}
@Override
public Object put(int key, Object value) {
throw new UnsupportedOperationException("put");
}
@Override
public Object remove(int key) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean containsKey(Object key) {
return false;
}
@Override
public void clear() {
}
@Override
public Set<Integer> keySet() {
return Collections.emptySet();
}
@Override
public boolean containsKey(int key) {
return false;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public Iterable<PrimitiveEntry<Object>> entries() {
return Collections.emptySet();
}
@Override
public Object get(Object key) {
return null;
}
@Override
public Object put(Integer key, Object value) {
throw new UnsupportedOperationException();
}
@Override
public Object remove(Object key) {
return null;
}
@Override
public void putAll(Map<? extends Integer, ?> m) {
throw new UnsupportedOperationException();
}
@Override
public Collection<Object> values() {
return Collections.emptyList();
}
@Override
public Set<Entry<Integer, Object>> entrySet() {
return Collections.emptySet();
}
}
private static final class UnmodifiableMap<V> implements IntObjectMap<V> {
private final IntObjectMap<V> map;
private Set<Integer> keySet;
private Set<Entry<Integer, V>> entrySet;
private Collection<V> values;
private Iterable<PrimitiveEntry<V>> entries;
UnmodifiableMap(IntObjectMap<V> map) {
this.map = map;
}
@Override
public V get(int key) {
return map.get(key);
}
@Override
public V put(int key, V value) {
throw new UnsupportedOperationException("put");
}
@Override
public V remove(int key) {
throw new UnsupportedOperationException("remove");
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public void clear() {
throw new UnsupportedOperationException("clear");
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public V get(Object key) {
return map.get(key);
}
@Override
public V put(Integer key, V value) {
throw new UnsupportedOperationException("put");
}
@Override
public V remove(Object key) {
throw new UnsupportedOperationException("remove");
}
@Override
public void putAll(Map<? extends Integer, ? extends V> m) {
throw new UnsupportedOperationException("putAll");
}
@Override
public Iterable<PrimitiveEntry<V>> entries() {
if (entries == null) {
entries = new Iterable<PrimitiveEntry<V>>() {
@Override
public Iterator<PrimitiveEntry<V>> iterator() {
return new IteratorImpl(map.entries().iterator());
}
};
}
return entries;
}
@Override
public Set<Integer> keySet() {
if (keySet == null) {
keySet = Collections.unmodifiableSet(map.keySet());
}
return keySet;
}
@Override
public Set<Entry<Integer, V>> entrySet() {
if (entrySet == null) {
entrySet = Collections.unmodifiableSet(map.entrySet());
}
return entrySet;
}
@Override
public Collection<V> values() {
if (values == null) {
values = Collections.unmodifiableCollection(map.values());
}
return values;
}
private class IteratorImpl implements Iterator<PrimitiveEntry<V>> {
final Iterator<PrimitiveEntry<V>> iter;
IteratorImpl(Iterator<PrimitiveEntry<V>> iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public PrimitiveEntry<V> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return new EntryImpl(iter.next());
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
private class EntryImpl implements PrimitiveEntry<V> {
private final PrimitiveEntry<V> entry;
EntryImpl(PrimitiveEntry<V> entry) {
this.entry = entry;
}
@Override
public int key() {
return entry.key();
}
@Override
public V value() {
return entry.value();
}
@Override
public void setValue(V value) {
throw new UnsupportedOperationException("setValue");
}
}
}
}