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

com.ajjpj.afoundation.collection.immutable.MapValueCollection 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.ACollectionHelper;
import com.ajjpj.afoundation.function.AFunction1;
import com.ajjpj.afoundation.function.APartialFunction;

import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;


/**
 * @author arno
 */
class MapValueCollection extends AbstractACollection> { //TODO other more efficient internal representation?
    private final AMap inner;

    public MapValueCollection (AMap inner) {
        this.inner = inner;
    }

    @Override protected AList createInternal (Collection elements) {
        return AList.create (elements);
    }
    @Override public ACollection clear () {
        return AList.nil();
    }
    @Override public int size () {
        return inner.size ();
    }
    @Override public boolean contains (V el) {
        for (V candidate: this) {
            if (Objects.equals (candidate, el)) {
                return true;
            }
        }
        return false;
    }
    @SuppressWarnings ("unchecked")
    @Override public  ACollection flatten () {
        return AList.create ((Iterable) ACollectionHelper.flatten ((Iterable>) this));
    }
    @Override public  ACollection map (AFunction1 f) throws E {
        return AList.create (ACollectionHelper.map (this, f));
    }
    @SuppressWarnings ("unchecked")
    @Override public  ACollection flatMap (AFunction1, E> f) throws E {
        return AList.create (ACollectionHelper.flatMap (this, f));
    }

    @Override public  ACollection collect (APartialFunction pf) throws E {
        return AList.create (ACollectionHelper.collect (this, pf));
    }

    @Override public Iterator iterator () {
        return new Iterator () {
            private final Iterator> iter = inner.iterator ();

            @Override public boolean hasNext () {
                return iter.hasNext ();
            }
            @Override public V next () {
                return iter.next ().getValue ();
            }
            @Override public void remove () {
                throw new UnsupportedOperationException ();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy