All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ajjpj.abase.collection.immutable.JavaUtilMapWrapper Maven / Gradle / Ivy

Go to download

a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations

There is a newer version: 1.0-pre11
Show newest version
package com.ajjpj.abase.collection.immutable;

import com.ajjpj.abase.collection.APair;

import java.util.*;

/**
 * @author arno
 */
class JavaUtilMapWrapper implements java.util.Map {
    private final AMap inner;

    JavaUtilMapWrapper(AMap inner) {
        this.inner = inner;
    }

    @Override
    public int size() {
        return inner.size();
    }

    @Override
    public boolean isEmpty() {
        return inner.isEmpty();
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean containsKey(Object key) {
        return inner.containsKey((K) key);
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean containsValue(Object value) {
        return inner.containsValue((V) value);
    }

    @SuppressWarnings("unchecked")
    @Override
    public V get(Object key) {
        return inner.get((K) key).getOrElse(null);
    }

    /**
     * There is usually a performance gain to be had by overriding this default implementation
     */
    @Override
    public Set keySet() {
        return new AbstractSet() {
            @Override
            public Iterator iterator() {
                return new Iterator() {
                    final Iterator> it = inner.iterator();

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public K next() {
                        return it.next()._1;
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }

            @Override
            public int size() {
                return inner.size();
            }
        };
    }

    @Override
    public Collection values() {
        return new AbstractCollection() {
            @Override
            public Iterator iterator() {
                return new Iterator() {
                    final Iterator> it = inner.iterator();

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public V next() {
                        return it.next()._2;
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }

            @Override
            public int size() {
                return inner.size();
            }
        };
    }

    @Override
    public Set> entrySet() {
        return new AbstractSet>() {
            @Override
            public Iterator> iterator() {
                return new Iterator>() {
                    final Iterator> it = inner.iterator();

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public Entry next() {
                        final APair kv = it.next();

                        return new Entry() {
                            @Override
                            public K getKey() {
                                return kv._1;
                            }

                            @Override
                            public V getValue() {
                                return kv._2;
                            }

                            @Override
                            public V setValue(V value) {
                                throw new UnsupportedOperationException();
                            }
                        };
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }

            @Override
            public int size() {
                return inner.size();
            }
        };
    }

    //------------- mutators

    @Override
    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }
    @Override
    public V remove(Object key) {
        throw new UnsupportedOperationException();
    }
    @Override
    public void putAll(Map m) {
        throw new UnsupportedOperationException();
    }
    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy