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

org.incava.ijdk.util.Collections Maven / Gradle / Ivy

There is a newer version: 3.9.0
Show newest version
package org.incava.ijdk.util;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.incava.ijdk.lang.Objects;

public class Collections extends Objects {
    /**
     * Creates a string from the collection, with each element separated by the delimiter.
     *
     * @param  the element type
     * @param coll the collection
     * @param delimiter the delimiter
     * @return the joined string
     */
    public static  String join(Collection coll, String delimiter) {
        if (coll == null) {
            return null;
        }
        StringBuilder sb = null;
        for (Object obj : coll) {
            if (sb == null) {
                sb = new StringBuilder();
            }
            else if (delimiter != null) {
                sb.append(delimiter);
            }
            sb.append(String.valueOf(obj));
        }
        return sb == null ? "" : sb.toString();
    }

    /**
     * Returns a set of the elements in common.
     *
     * @param  the element type
     * @param a the first collection
     * @param b the first collection
     * @return a set of the common elements
     */
    public static  Set intersection(Collection a, Collection b) {
        Set intSet = new HashSet(a);
        intSet.retainAll(b);        
        return intSet;
    }

    /**
     * Returns a set of the elements in common.
     *
     * @param  the element type
     * @param colls the collections to take the intersection of
     * @return a set of the common elements
     */
    @SafeVarargs
    public static  Set intersection(Collection ... colls) {
        Set intSet = null;

        for (Collection coll : colls) {
            if (intSet == null) {
                intSet = new HashSet(coll);
            }
            else {
                intSet.retainAll(coll);
            }
        }
        return intSet;
    }

    /**
     * Returns whether the collection (if not null) contains the given value.
     * Returns false if coll is null.
     *
     * @param  the element type
     * @param coll the collection
     * @param val the value to search for
     * @return whether the element is in the collection
     */
    public static  boolean contains(Collection coll, Type val) {
        return coll != null && coll.contains(val);
    }

    /**
     * Returns whether all elements in tgt are in src.
     * If src or tgt is null, false is returned.
     *
     * @param  the element type
     * @param src the first (source) collection
     * @param tgt the target (sought) collection
     * @return whether tgt is in src
     */
    public static  boolean hasAll(Collection src, Collection tgt) {
        return checkHas(src, tgt, true);
    }   

    /**
     * Returns whether any element in tgt is in src.
     * If src or tgt is null, false is returned.
     *
     * @param  the element type
     * @param src the first (source) collection
     * @param tgt the target (sought) collection
     * @return whether any element in tgt is in src
     */
    public static  boolean hasAny(Collection src, Collection tgt) {
        return checkHas(src, tgt, false);
    }

    /**
     * Returns whether there are any elements in coll, which can be null.
     *
     * @param  the element type
     * @param coll the collection
     * @return whether any element in is in coll
     */
    public static  boolean any(Collection coll) {
        return coll != null && !coll.isEmpty();
    }

    private static  boolean checkHas(Collection src, Collection tgt, boolean forAll) {
        if (src == null || tgt == null) {
            return false;
        }

        for (Type elmt : tgt) {
            if (forAll != src.contains(elmt)) {
                return !forAll;
            }
        }

        return forAll;
    }    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy