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

com.commercetools.sync.commons.utils.CollectionUtils Maven / Gradle / Ivy

package com.commercetools.sync.commons.utils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;

public final class CollectionUtils {

    /**
     * Create a new collection which contains only elements which satisfy {@code includeCondition} predicate.
     *
     * @param collection       {@link Collection} to be filtered
     * @param includeCondition condition which verifies whether the value should be included to the final result.
     * @param               type of the collection items.
     * @return new filtered stream with items which satisfy {@code includeCondition} predicate. If {@code collection}
     *     is null or empty - empty stream is returned.
     */
    @Nonnull
    public static  Stream filterCollection(@Nullable final Collection collection,
                                                 @Nonnull final Predicate includeCondition) {

        return collection == null ? Stream.empty()
                : collection.stream()
                .filter(includeCondition);
    }

    /**
     * Convert a {@code collection} to a set of values using {@code keyMapper} mapping.
     *
     * 

If the collection has duplicate keys - only one will be stored, which one is not defined though. * * @param collection {@link Collection} to convert * @param keyMapper function which converts the collection entry to a key. * @param type of collection entries * @param type of Set key * @return new {@link Set} which consists of items, converted from <T> entries to keys using * {@code entryToKey} function. */ @Nonnull public static Set collectionToSet(@Nullable final Collection collection, @Nonnull final Function keyMapper) { return collection == null ? Collections.emptySet() : collection.stream() .map(keyMapper) .collect(toSet()); } /** * Convert a {@code collection} to a map using {@code keyMapper} and {@code valueMapper} mappers. * If keys are duplicated - only one value is stored, which one - undefined. * * @param collection {@link Collection} to convert * @param keyMapper function which converts the collection entry to a key * @param valueMapper function which converts the collection entry to a value * @param type of collection entries * @param type of Map key * @param type of Map value * @return new {@link Map} which consists of key-value pairs, converted from <T> entries * @see #collectionToMap(Collection, Function) */ @Nonnull public static Map collectionToMap(@Nullable final Collection collection, @Nonnull final Function keyMapper, @Nonnull final Function valueMapper) { return collection == null ? Collections.emptyMap() : collection.stream() .collect(toMap(keyMapper, valueMapper, (k1, k2) -> k1)); // ignore duplicates } /** * Same as {@link #collectionToMap(Collection, Function, Function)}, but uses entries themselves as map values. * * @param collection {@link Collection} to convert * @param keyMapper function which converts the collection entry to a key * @param type of collection entries * @param type of Map key * @return new {@link Map} which consists of key-value pairs, converted from <T> entries * @see #collectionToMap(Collection, Function, Function) */ @Nonnull public static Map collectionToMap(@Nullable final Collection collection, @Nonnull final Function keyMapper) { return collectionToMap(collection, keyMapper, value -> value); } private CollectionUtils() { } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy