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

com.github.ormfux.common.utils.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
package com.github.ormfux.common.utils;

import static com.github.ormfux.common.utils.NullableUtils.isNull;
import static com.github.ormfux.common.utils.NullableUtils.nonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

public final class CollectionUtils {
    
    private CollectionUtils() {
        throw new IllegalAccessError(CollectionUtils.class.getSimpleName() + " class is not intended to be instantiated");
    }
    
    /**
     * Maps from source collection to a target collection.
     * 
     * @param  source type
     * @param  target type
     * @param source source collection
     * @param result target collection
     * @param functor mapping functor
     * @return target collection
     */
    public static  Collection map(final S[] source, final Collection result, final Function functor) {
        Objects.requireNonNull(result);
        Objects.requireNonNull(functor);
        
        for (final S sourceElement : source) {
            result.add(functor.apply(sourceElement));
        }
        
        return result;
    }
    
    /**
     * Maps from source collection to a target collection. ONly elements from the source collection that satisfy the filter
     * condition will be mapped and included in the result.
     * 
     * @param  source type
     * @param  target type
     * @param source source collection
     * @param result target collection
     * @param filterCondition the filter condition
     * @param mapFunction mapping functor
     * @return target collection
     */
    public static  Collection filterAndMap(final Collection source, final Collection result, final Predicate filterCondition, final Function mapFunction) {
        Objects.requireNonNull(result);
        Objects.requireNonNull(filterCondition);
        Objects.requireNonNull(mapFunction);
        
        for (final S sourceElement : source) {
            if (filterCondition.test(sourceElement)) {
                result.add(mapFunction.apply(sourceElement));
            }
        }
        
        return result;
    }
    
    
    /**
     * Maps from source collection to a target collection.
     * 
     * @param  source type
     * @param  target type
     * @param source source collection
     * @param result target collection
     * @param functor mapping functor
     * @return target collection
     */
    public static  Collection map(final Collection source, final Collection result, final Function functor) {
        Objects.requireNonNull(result);
        Objects.requireNonNull(functor);
        
        for (final S sourceElement : source) {
            result.add(functor.apply(sourceElement));
        }
        
        return result;
    }
    
    /**
     * Maps from source collection to a target array.
     * 
     * @param source source collection
     * @param functor mapping functor
     * @return target array
     * 
     * @param  source type
     * @param  target type
     * 
     */
    @SuppressWarnings("unchecked")
    public static  T[] mapToArray(final Collection source, final Function functor) {
        Objects.requireNonNull(functor);
        
        final List resultList = new ArrayList<>(source.size());
        
        for (final S sourceElement : source) {
            final T executeResult = functor.apply(sourceElement);
            
            if (nonNull(executeResult)) {
                // add the non-null results to the list only
                resultList.add(executeResult);
            }
        }
        
        return (T[]) resultList.toArray();
    }
    
    /**
     * Filters a source collection and add result to a target collection.
     * 
     * @param  source/target type
     * @param source source collection
     * @param result target collection
     * @param predicate filtering predicate
     * @return target array
     */
    public static  Collection filter(final Collection source, final Collection result, final Predicate predicate) {
        Objects.requireNonNull(result);
        Objects.requireNonNull(predicate);
        
        for (final T sourceElement : source) {
            if (predicate.test(sourceElement)) {
                result.add(sourceElement);
            }
        }
        return result;
    }
    
    /**
     * Checks whether a collection is {@code null} or empty.
     * 
     * @param  type
     * @param collection collection
     * @return true, if {@code null} or empty
     */
    public static  boolean isEmpty(final Collection collection) {
        return NullableUtils.check(collection, NullableUtils.isEmpty(), true);
    }
    
    /**
     * Returns the first element from the given collection that evaluates the given predicate to
     * {@code true}.
     * 
     * @param  Parameterized type.
     * 
     * @param collection a collection.
     * @param predicate The predicate.
     * @return the first element from the given collection that evaluates the given predicate to
     *         {@code true}; {@code null} when there is none.
     */
    public static  T selectFirst(final Collection collection, final Predicate predicate) {
        Objects.requireNonNull(predicate);
        
        for (final T sourceElement : collection) {
            if (predicate.test(sourceElement)) {
                return sourceElement;
            }
        }
        
        return null;
    }
    
    /**
     * Determines whether an element in the given collection evaluates the given predicate to
     * {@code true}.
     * 
     * @param  Parameterized type.
     * 
     * @param collection a collection.
     * @param predicate a predicate.
     * 
     * @return {@code true} when there is an element in the collection fulfilling the predicate.
     */
    public static  boolean exists(final Collection collection, final Predicate predicate) {
        Objects.requireNonNull(predicate);
        
        for (final T sourceElement : collection) {
            if (predicate.test(sourceElement)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Compares two collections for equality. The elements in both collections should be unique,
     * otherwise the result might not be very accurate.
     * 
     * @param  Parameterized type.
     * 
     * @param col1 source collection.
     * @param col2 target collection.
     * 
     * @return {@code true} both collections have the same size and contain the same elements.
     */
    public static  boolean isEqual(final Collection col1, final Collection col2) {
        if (col1 == col2) {
            return true;
        } else if ((isNull(col1) && nonNull(col2)) || (nonNull(col1) && isNull(col2))) {
            return false;
        } else if (col1.size() != col2.size()) {
            return false;
        } else {
            final List remainingContents = new ArrayList(col1);
            
            for (final T col2Element : col2) {
                if (!remainingContents.remove(col2Element)) {
                    return false;
                }
            }
            
            return remainingContents.isEmpty();
            
        }
    }
    
    /**
     * Converts a collection to a map. The function is used to produce the map keys.
     * 
     * @param  key type
     * @param  entry type
     * 
     * @param source source collection
     * @param keyFunction Function to produce the map keys.
     * @return The map.
     */
    public static  Map asMap(final Collection source, final Function keyFunction) {
        return asMap(source, keyFunction, Function.identity());
    }
    
    /**
     * Converts a collection to a map.
     * 
     * @param  key type
     * @param  map value type
     * @param  collection element type
     * 
     * @param source source collection
     * @param keyFunction Function to produce the map keys.
     * @param valueFunction Function to produce the map values.
     * @return The collection as map.
     */
    public static  Map asMap(final Collection source, 
                                            final Function keyFunction,
                                            final Function valueFunction) {
        final Map map = new HashMap<>();
        
        for (final E sourceElement : source) {
            map.put(keyFunction.apply(sourceElement), valueFunction.apply(sourceElement));
        }
        
        return map;
    }
    
}