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

com.ajjpj.afoundation.collection.immutable.MapAsSetWrapper 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.collection.AEquality;
import com.ajjpj.afoundation.function.*;

import java.io.Serializable;
import java.util.*;


/**
 * @author arno
 */
abstract class MapAsSetWrapper> implements ASet, Serializable {
    private final AMap inner;

    @SuppressWarnings ("unchecked")
    protected MapAsSetWrapper (AMap inner) {
        this.inner = (AMap) inner;
    }

    @Override public C clear () {
        return wrapAsSet (inner.clear ());
    }

    @Override public AEquality equalityForEquals () {
        return inner.keyEquality();
    }

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

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

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

    @SuppressWarnings ("unchecked")
    @Override public C added (K el) {
        return wrapAsSet (inner.updated (el, Boolean.TRUE));
    }

    @SuppressWarnings ("unchecked")
    @Override public C removed (K el) {
        return wrapAsSet (inner.removed (el));
    }

    @Override public boolean contains (K el) {
        return inner.containsKey (el);
    }

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

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

    @Override public String mkString () {
        return ACollectionHelper.mkString (this);
    }

    @Override public String mkString (String separator) {
        return ACollectionHelper.mkString (this, separator);
    }

    @Override public String mkString (String prefix, String separator, String suffix) {
        return ACollectionHelper.mkString (this, prefix, separator, suffix);
    }

    @Override public AList toList () {
        return AList.create (this);
    }

    @Override public ASet toSet () {
        return this;
    }

    @SuppressWarnings ("unchecked")
    @Override public ASet toSet (AEquality equality) {
        if (equality.equals (equalityForEquals ())) {
            return this;
        }

        return AHashSet.create (equality, this);
    }

    @Override public Collection asJavaUtilCollection () {
        return asJavaUtilSet ();
    }

    @Override public Set asJavaUtilSet () {
        return inner.asJavaUtilMap ().keySet ();
    }

    @Override public String toString () {
        return mkString ("[", ", ", "]");
    }

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

    @SuppressWarnings("SimplifiableIfStatement")
    @Override
    public boolean equals(Object o) {
        if(o == this) {
            return true;
        }
        if(! (o instanceof MapAsSetWrapper)) {
            return false;
        }
        return inner.equals(((MapAsSetWrapper) o).inner);
    }

    //-------------------------------------- collection transformations

    @SuppressWarnings ("unchecked")
    protected  ASet createInternal (Iterable elements) {
        AMap result = inner.clear ();

        for (X el: elements) {
            result = result.updated (el, Boolean.TRUE);
        }
        return (ASet) wrapAsSet (result);
    }

    protected abstract C wrapAsSet (AMap inner);

    @Override public  ASet filter (APredicate pred) throws E {
        return createInternal (ACollectionHelper.filter (this, pred));
    }

    @SuppressWarnings ("unchecked")
    @Override public  ASet flatten () {
        return (ASet) createInternal (ACollectionHelper.flatten ((Iterable>) this));
    }

    @SuppressWarnings ("unchecked")
    @Override public  AMap> groupBy (AFunction1 f) throws E {
        return groupBy (f, inner.keyEquality ());
    }

    @SuppressWarnings ("unchecked")
    @Override public  AMap> groupBy (AFunction1 f, AEquality keyEquality) throws E {
        AMap> result = (AMap>) (keyEquality.equals (inner.keyEquality ()) ? inner.clear () : AHashMap.empty (keyEquality));

        final ASet emptySet = createInternal (Collections.emptyList ());

        for(K o: this) {
            final X key = f.apply(o);
            final ASet perKey = result
                    .get(key)
                    .getOrElse (emptySet)
                    .added (o);
            result = result.updated (key, perKey);
        }
        return result;
    }

    @Override public  void foreach(AStatement1 f) throws E {
        for (K el: this) {
            f.apply (el);
        }
    }

    @Override public  ASet map (AFunction1 f) throws E {
        return ACollectionHelper.asASetView (ACollectionHelper.map (this, f));
    }

    @Override public  ASet flatMap (AFunction1, E> f) throws E {
        return ACollectionHelper.asASetView (ACollectionHelper.flatMap (this, f));
    }

    @Override public  ASet collect (APartialFunction pf) throws E {
        return ACollectionHelper.asASetView (ACollectionHelper.collect (this, pf));
    }

    @Override public  R foldLeft (R startValue, AFunction2 f) throws E {
        return ACollectionHelper.foldLeft (this, startValue, f);
    }

    @Override public  AOption find (APredicate pred) throws E {
        return ACollectionHelper.find (this, pred);
    }

    @Override public  boolean forAll (APredicate pred) throws E {
        return ACollectionHelper.forAll (this, pred);
    }

    @Override public  boolean exists (APredicate pred) throws E {
        return ACollectionHelper.exists (this, pred);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy