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.ACollection;
import com.ajjpj.abase.collection.immutable.AOption;
import com.ajjpj.abase.collection.immutable.AbstractACollection;
import com.ajjpj.abase.function.AFunction1;
import com.ajjpj.abase.function.AFunction2;
import com.ajjpj.abase.function.APredicate;

import java.lang.reflect.Array;
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();
    }

    /**
     * Returns an element of a collection that matches a predicate, if any, or AOption.none() if there is no match.
     */
    public static  AOption find(Iterable coll, APredicate pred) throws E {
        for(T o: coll) {
            if(pred.apply(o)) {
                return AOption.some(o);
            }
        }
        return AOption.none();
    }

    /**
     * Matches a predicate against collection elements, and returns true iff it matches them all.
     */
    public static  boolean forAll(Iterable coll, APredicate pred) throws E {
        for(T o: coll) {
            if(!pred.apply(o)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Matches a predicate against collection elements, and returns true iff it matches at least one of them.
     */
    public static  boolean exists(Iterable coll, APredicate pred) throws E {
        for(T o: coll) {
            if(pred.apply(o)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Applies a transformation function to all elements of a collection, creating a new collection from the results.
     */
    public static  Collection map(Iterable coll, AFunction1 f) throws E {
        final List result = new ArrayList<>();

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

        return result;
    }

    /**
     * Applies a transformation function to all elements of a collection, creating a new collection from the results.
     */
    public static  List map(List coll, AFunction1 f) throws E {
        final List result = createEmptyListOfType (coll, true);

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

        return result;
    }

    /**
     * Applies a transformation function to all elements of a collection, creating a new collection from the results.
     */
    public static  Set map (Set coll, AFunction1 f) throws E {
        final Set result = createEmptySetOfType(coll, true);

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

        return result;
    }

    @SuppressWarnings ("unchecked")
    private static  List createEmptyListOfType (List original, boolean sameSize) {
        if (original instanceof ArrayList) {
            return sameSize ? new ArrayList (original.size ()) : new ArrayList ();
        }

        try {
            return original.getClass ().newInstance ();
        }
        catch (Exception e) {
            return sameSize ? new ArrayList (original.size ()) : new ArrayList ();
        }
    }

    @SuppressWarnings ("unchecked")
    private static  Set createEmptySetOfType (Set original, boolean sameSize) {
        if (original instanceof HashSet) {
            return sameSize ? new HashSet (original.size ()) : new HashSet ();
        }

        try {
            return original.getClass ().newInstance ();
        }
        catch (Exception e) {
            return sameSize ? new HashSet (original.size ()) : new HashSet ();
        }
    }

    /**
     * Same as map(), except that the transformation function returns collections and all the results are
     *  flattened into a single collection.
     */
    public static  Collection flatMap(Iterable coll, AFunction1, E> f) throws E {
        final List result = new ArrayList<>();

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

        return result;
    }

    /**
     * Same as map(), except that the transformation function returns collections and all the results are
     *  flattened into a single collection.
     */
    public static  List flatMapList(Iterable coll, AFunction1, E> f) throws E {
        final List result = new ArrayList<>();

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

        return result;
    }

    /**
     * Same as map(), except that the transformation function returns collections and all the results are
     *  flattened into a single collection.
     */
    public static  Set flatMapSet(Iterable coll, AFunction1, E> f) throws E {
        final Set result = new HashSet<>();

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

        return result;
    }

    /**
     * Takes a collection of collections and creates a new collection from the elements, leaving out the innermost
     *  level of collection.
     */
    public static  Collection flatten(Iterable> coll) {
        final List result = new ArrayList<>();
        for(Iterable o: coll) {
            for(T el: o) {
                result.add(el);
            }
        }
        return result;
    }

    /**
     * Takes a collection of collections and creates a new collection from the elements, leaving out the innermost
     *  level of collection.
     */
    public static  List flattenList(Iterable> coll) {
        final List result = new ArrayList<>();
        for(Iterable o: coll) {
            for(T el: o) {
                result.add(el);
            }
        }
        return result;
    }

    /**
     * Takes a collection of collections and creates a new collection from the elements, leaving out the innermost
     *  level of collection.
     */
    public static  Set flattenSet(Iterable> coll) {
        final Set result = new HashSet<>();
        for(Iterable o: coll) {
            for(T el: o) {
                result.add(el);
            }
        }
        return result;
    }

    /**
     * Matches all elements of a collection against a predicate, creating a new collection from those that match.
     */
    public static  Collection filter(Iterable coll, APredicate pred) throws E {
        final List result = new ArrayList<>();
        for(T o: coll) {
            if(pred.apply(o)) {
                result.add(o);
            }
        }
        return result;
    }

    /**
     * Matches all elements of a collection against a predicate, creating a new collection from those that match.
     */
    public static  List filter(List coll, APredicate pred) throws E {
        final List result = createEmptyListOfType (coll, false);
        for(T o: coll) {
            if(pred.apply(o)) {
                result.add(o);
            }
        }
        return result;
    }

    /**
     * Matches all elements of a collection against a predicate, creating a new collection from those that match.
     */
    public static  Set filter(Set coll, APredicate pred) throws E {
        final Set result = createEmptySetOfType(coll, false);
        for(T o: coll) {
            if(pred.apply(o)) {
                result.add(o);
            }
        }
        return result;
    }

    /**
     * Creates a Map from a collection. Each element's key is determined by applying a function to the element. All
     *  elements with the same key are stored as that key's value in the returned Map.
     */
    public static  Map> groupBy (Iterable coll, AFunction1 f) throws E {
        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;
    }

    /**
     * Creates a Map from a collection. Each element's key is determined by applying a function to the element. All
     *  elements with the same key are stored as that key's value in the returned Map.
     */
    public static  Map> groupBy (List coll, AFunction1 f) throws E {
        final Map> result = new HashMap<>();
        for(T o: coll) {
            final X key = f.apply(o);
            List perKey = result.get(key);
            if(perKey == null) {
                perKey = createEmptyListOfType (coll, false);
                result.put(key, perKey);
            }
            perKey.add(o);
        }
        return result;
    }

    /**
     * Creates a Map from a collection. Each element's key is determined by applying a function to the element. All
     *  elements with the same key are stored as that key's value in the returned Map.
     */
    public static  Map> groupBy (Set coll, AFunction1 f) throws E {
        final Map> result = new HashMap<>();
        for(T o: coll) {
            final X key = f.apply(o);
            Set perKey = result.get(key);
            if(perKey == null) {
                perKey = createEmptySetOfType(coll, false);
                result.put(key, perKey);
            }
            perKey.add(o);
        }
        return result;
    }

    /**
     * Creates a Map from a collection. Each element's key is determined by applying a function to the element. All
     *  elements with the same key are stored as that key's value in the returned Map.

* * This method gives control over the equalityForEquals strategy used to determine if two keys are 'equal'. To accomodate that, * the keys are wrapped in AEqualsWrapper.

* * This method is rather technical in nature, and it is probably more useful as a foundation for generic code than * for direct use by applications. */ public static Map, Collection> groupBy(Iterable coll, AFunction1 f, AEquality keyEquality) throws E { 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; } /** * Creates a Map from a collection. Each element's key is determined by applying a function to the element. All * elements with the same key are stored as that key's value in the returned Map.

* * This method gives control over the equalityForEquals strategy used to determine if two keys are 'equal'. To accomodate that, * the keys are wrapped in AEqualsWrapper.

* * This method is rather technical in nature, and it is probably more useful as a foundation for generic code than * for direct use by applications. */ public static Map, List> groupBy(List coll, AFunction1 f, AEquality keyEquality) throws E { final Map, List> result = new HashMap<>(); for(T o: coll) { final AEqualsWrapper key = new AEqualsWrapper<>(keyEquality, f.apply(o)); List perKey = result.get(key); if(perKey == null) { perKey = createEmptyListOfType (coll, false); result.put(key, perKey); } perKey.add(o); } return result; } /** * Creates a Map from a collection. Each element's key is determined by applying a function to the element. All * elements with the same key are stored as that key's value in the returned Map.

* * This method gives control over the equalityForEquals strategy used to determine if two keys are 'equal'. To accomodate that, * the keys are wrapped in AEqualsWrapper.

* * This method is rather technical in nature, and it is probably more useful as a foundation for generic code than * for direct use by applications. */ public static Map, Set> groupBy (Set coll, AFunction1 f, AEquality keyEquality) throws E { final Map, Set> result = new HashMap<>(); for(T o: coll) { final AEqualsWrapper key = new AEqualsWrapper<>(keyEquality, f.apply(o)); Set perKey = result.get(key); if(perKey == null) { perKey = createEmptySetOfType(coll, false); result.put(key, perKey); } perKey.add(o); } return result; } /** * Applies a binary operator to a start value and all elements of this sequence, going left to right. * * @param element type of the collection * @param result type */ public static R foldLeft (Iterable coll, R startValue, AFunction2 f) throws E { R result = startValue; for (T e: coll) { result = f.apply (result, e); } return result; } /** * Applies a binary operator to a start value and all elements of this list, going left to right. * * @param element type of the collection * @param result type * */ public static R foldRight (List coll, R startValue, AFunction2 f) throws E { R result = startValue; ListIterator i = coll.listIterator(coll.size()); while ( i.hasPrevious() ) { result = f.apply (result, i.previous()); } 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 ACollectionWrapper asACollectionCopy(Collection c) { return asACollectionView(new ArrayList<>(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 ACollectionWrapper asACollectionView(Collection c) { return new ACollectionWrapper(c); } /** * Copies the content of an array into an (immutable) ACollection instance. Subsequent * changes to the underlying array 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. */ @SuppressWarnings("unchecked") public static AArrayWrapper asArrayCopy(T[] c) { final T[] newArray = (T[]) Array.newInstance(c.getClass().getComponentType(), c.length); System.arraycopy(c, 0, newArray, 0, c.length); return new AArrayWrapper<>(newArray); } /** * Wraps the content of an array in an ACollection instance. While the returned * instance itself has no mutator methods, changes to the underlying array 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. */ public static AArrayWrapper asArrayView(T[] c) { return new AArrayWrapper<>(c); } public static class ACollectionWrapper extends AbstractACollection> { private final Collection inner; private ACollectionWrapper(Collection inner) { this.inner = inner; } @Override protected ACollectionWrapper createInternal(Collection elements) { return new ACollectionWrapper<>(elements); } @Override protected AEquality equalityForEquals() { return AEquality.EQUALS; } @Override public int size() { return inner.size(); } @Override public ACollection map(AFunction1 f) throws E { return new ACollectionWrapper<>(ACollectionHelper.map(inner, f)); } @Override public ACollection flatMap(AFunction1, E> f) throws E { return new ACollectionWrapper<>(ACollectionHelper.flatMap (inner, f)); } @SuppressWarnings("unchecked") @Override public ACollection flatten() { return new ACollectionWrapper<>(ACollectionHelper.flatten ((Iterable>) inner)); } @Override public R foldLeft (R startValue, AFunction2 f) throws E { return ACollectionHelper.foldLeft (inner, startValue, f); } @SuppressWarnings("NullableProblems") @Override public Iterator iterator() { return inner.iterator(); } } public static class AArrayWrapper extends AbstractACollection> { private final T[] inner; private AArrayWrapper(T[] inner) { this.inner = inner; } @SuppressWarnings("unchecked") @Override protected AArrayWrapper createInternal(Collection elements) { final T[] result = (T[]) Array.newInstance(inner.getClass().getComponentType(), elements.size()); int idx = 0; for(T o: elements) { result[idx++] = o; } return new AArrayWrapper<>(result); } @Override protected AEquality equalityForEquals() { return AEquality.EQUALS; } @Override public int size() { return inner.length; } /** * Returns ACollectionWrapper instead of AArrayWrapper because Java can not instantiate an array for a component type that is available only as a generic parameter. */ @SuppressWarnings("unchecked") @Override public ACollectionWrapper flatten() { return new ACollectionWrapper<>(ACollectionHelper.flatten(Arrays.asList((Iterable[]) inner))); } /** * Returns ACollectionWrapper instead of AArrayWrapper because Java can not instantiate an array for a component type that is available only as a generic parameter. */ @Override public ACollectionWrapper map(AFunction1 f) throws E { return new ACollectionWrapper<>(ACollectionHelper.map (Arrays.asList (inner), f)); } /** * Returns ACollectionWrapper instead of AArrayWrapper because Java can not instantiate an array for a component type that is available only as a generic parameter. */ @Override public ACollectionWrapper flatMap(AFunction1, E> f) throws E { return new ACollectionWrapper<>(ACollectionHelper.flatMap (Arrays.asList (inner), f)); } @Override public R foldLeft (R startValue, AFunction2 f) throws E { return ACollectionHelper.foldLeft (Arrays.asList (inner), startValue, f); } @SuppressWarnings("NullableProblems") @Override public Iterator iterator() { return new Iterator() { int idx = 0; @Override public boolean hasNext() { return idx < inner.length; } @Override public T next() { final T result = inner[idx]; idx += 1; return result; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } } /** * Returns a Collection with the exact same elements as an Iterable, copying only if the parameter is not a collection. */ public static List asJavaUtilList(Iterable c) { if(c instanceof List) { return (List) c; } return asJavaUtilCollection(c.iterator()); } /** * Returns a Collection with the exact same elements as an Iterable, copying only if the parameter is not a collection. */ public static Collection asJavaUtilCollection(Iterable c) { if(c instanceof Collection) { return (Collection) c; } return asJavaUtilCollection(c.iterator()); } /** * Copies the elements from an Iterator into a Collection. */ public static List asJavaUtilCollection(Iterator c) { final List result = new ArrayList<>(); while(c.hasNext()) { result.add(c.next()); } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy