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

com.ajjpj.abase.collection.ACollectionHelper 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;

import com.ajjpj.abase.collection.immutable.*;
import com.ajjpj.abase.function.AFunction1;
import com.ajjpj.abase.function.APredicate;

import java.util.*;


/**
 * This class consists of a number of useful methods that operate on a variety of collection types.
 *
 * @author arno
 */
public class ACollectionHelper {
    /**
     * Returns a string representation of a collection, separating elements with a comma.
     */
    public static String mkString(Iterable iterable) {
        return mkString(iterable, ", ");
    }

    /**
     * Returns a string representation of a collection, separating elements with a separator.
     */
    public static String mkString(Iterable iterable, String separator) {
        return mkString(iterable, "", separator, "");
    }

    /**
     * Returns a string representation of a collection, separating elements with a separator and putting
     *  prefix before the first and a suffix after the last element.
     */
    public static String mkString(Iterable iterable, String prefix, String separator, String suffix) {
        final StringBuilder result = new StringBuilder(prefix);

        boolean first = true;
        for(Object o: iterable) {
            if(first) {
                first = false;
            }
            else {
                result.append(separator);
            }
            result.append(o);
        }

        result.append(suffix);
        return result.toString();
    }

    public static  AOption find(Iterable coll, APredicate pred) throws E { //TODO junit, use in alist etc.
        for(T o: coll) {
            if(pred.apply(o)) {
                return AOption.some(o);
            }
        }
        return AOption.none();
    }

    public static  boolean forAll(Iterable coll, APredicate pred) throws E { //TODO junit
        for(T o: coll) {
            if(!pred.apply(o)) {
                return false;
            }
        }
        return true;
    }

    public static  boolean exists(Iterable coll, APredicate pred) throws E { //TODO junit
        for(T o: coll) {
            if(pred.apply(o)) {
                return true;
            }
        }
        return false;
    }

    public static  Collection map(Iterable coll, AFunction1 f) throws E { //TODO junit, javadoc
        final List result = new ArrayList<>();

        for(T o: coll) {
            result.add(f.apply(o));
        }

        return result;
    }

    public static  Collection flatMap(Iterable coll, AFunction1, T, E> f) throws E { //TODO junit, javadoc
        final List result = new ArrayList<>();

        for(T o: coll) {
            for(X el: f.apply(o)) {
                result.add(el);
            }
        }

        return result;
    }

    public static  Collection flatten(Iterable> coll) { //TODO junit, javadoc
        final List result = new ArrayList<>();
        for(Iterable o: coll) {
            for(T el: o) {
                result.add(el);
            }
        }
        return result;
    }

    public static  Collection filter(Iterable coll, APredicate pred) throws E { //TODO junit
        final List result = new ArrayList<>();
        for(T o: coll) {
            if(pred.apply(o)) {
                result.add(o);
            }
        }
        return result;
    }

    public static  Map> groupBy(Iterable coll, AFunction1 f) throws E { //TOD javadoc, junit
        final Map> result = new HashMap<>();
        for(T o: coll) {
            final X key = f.apply(o);
            Collection perKey = result.get(key);
            if(perKey == null) {
                perKey = new ArrayList<>();
                result.put(key, perKey);
            }
            perKey.add(o);
        }
        return result;
    }

    public static  Map, Collection> groupBy(Iterable coll, AFunction1 f, AEquality keyEquality) throws E { //TODO javadoc, junit
        final Map, Collection> result = new HashMap<>();
        for(T o: coll) {
            final AEqualsWrapper key = new AEqualsWrapper<>(keyEquality, f.apply(o));
            Collection perKey = result.get(key);
            if(perKey == null) {
                perKey = new ArrayList<>();
                result.put(key, perKey);
            }
            perKey.add(o);
        }
        return result;
    }

    /**
     * Copies the content of an Iterable into an (immutable) ACollection instance. Subsequent
     *  changes to the underlying collection have no effect on the returned ACollection instance.

* * The returned collection has list semantics with regard to map()> and other modifying methods; * duplicate values are allowed. */ public static ACollection> asACollectionCopy(Collection c) { //TODO Junit return asACollectionView(asJavaUtilCollection(c)); } /** * Wraps the content of a java.util.Collection in an ACollection instance. While the returned * instance itself has no mutator methods, changes to the underlying collection are reflected in the wrapping * ACollection instance.

* * The returned collection has list semantics with regard to map() and other modifying methods; duplicate * values are allowed. */ @SuppressWarnings("unchecked") public static ACollection> asACollectionView(Collection c) { //TODO junit return new ACollectionWrapper(c); } private static class ACollectionWrapper> implements ACollection { //TODO rename X to C --> why does that cause compiler errors? private final Collection inner; ACollectionWrapper(Collection inner) { this.inner = inner; } @Override public int size() { return inner.size(); } @Override public boolean isEmpty() { return inner.isEmpty(); } @Override public boolean nonEmpty() { return !inner.isEmpty(); } @Override public ACollection> map(AFunction1 f) throws E { return new ACollectionWrapper<>(ACollectionHelper.map(inner, f)); } @Override public AFilterMonadic> flatMap(AFunction1, T, E> f) throws E { return new ACollectionWrapper<>(ACollectionHelper.flatMap(inner, f)); } @Override public ACollection> flatten() { return new ACollectionWrapper<>(ACollectionHelper.flatten((Iterable>) inner)); } @SuppressWarnings("unchecked") @Override public X filter(APredicate pred) throws E { return (X) new ACollectionWrapper(ACollectionHelper.filter(inner, pred)); } @Override public AOption find(APredicate pred) throws E { return ACollectionHelper.find(inner, pred); } @Override public boolean forAll(APredicate pred) throws E { return ACollectionHelper.forAll(inner, pred); } @Override public boolean exists(APredicate pred) throws E { return ACollectionHelper.exists(inner, pred); } @Override public AMap groupBy(AFunction1 f) throws E { //TODO junit return groupBy(f, AEquality.EQUALS); } @Override public AMap groupBy(AFunction1 f, AEquality keyEquality) throws E { //TODO junit final Map, Collection> raw = ACollectionHelper.groupBy(inner, f, keyEquality); AMap result = AHashMap.empty(keyEquality); for(Map.Entry, Collection> entry: raw.entrySet()) { final X value = (X) new ACollectionWrapper(entry.getValue()); result = result.updated(entry.getKey().value, value); } return result; } @Override public AList toList() { return AList.create(inner); } @Override public AHashSet toSet() { return AHashSet.create(inner); } @Override public AHashSet toSet(AEquality equality) { return AHashSet.create(equality, inner); } @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 Iterator iterator() { return inner.iterator(); } } /** * Copies an Iterable into a Collection. */ public static Collection asJavaUtilCollection(Iterable c) { //TODO JUnit return asJavaUtilCollection(c.iterator()); } /** * Copies the elements from an Iterator into a Collection. */ public static Collection asJavaUtilCollection(Iterator c) { final List result = new ArrayList<>(); while(c.hasNext()) { result.add(c.next()); } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy