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

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

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

import com.ajjpj.afoundation.collection.tuples.ATuple2;

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().getKey ();
                    }

                    @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().getValue ();
                    }

                    @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 AMapEntry kv = it.next();

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

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

                            @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