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

com.box.sdk.internal.utils.CollectionUtils Maven / Gradle / Ivy

The newest version!
package com.box.sdk.internal.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * {@link Collection} related utlities.
 */
public class CollectionUtils {

    /**
     * Only static members.
     */
    protected CollectionUtils() {
    }

    /**
     * Re-maps a provided collection.
     *
     * @param  type of result
     * @param  type of source
     * @param source     for mapping
     * @param mapper     element mapper
     * @return mapped source
     */
    public static  List map(Collection source,
                                                          Mapper mapper) {
        List result = new LinkedList();
        for (T_Source element : source) {
            result.add(mapper.map(element));
        }
        return result;
    }

    /**
     * Creates list from iterable.
     *
     * @param iterable Iterable that will be used to create list.
     * @param       Collection element
     * @return List with all items from iterable. Elements are in the same order as iterable is returning them.
     */
    public static  List createListFrom(Iterable iterable) {
        List result = new ArrayList();
        for (T element : iterable) {
            result.add(element);
        }
        return result;
    }

    public static String mapToString(Map map) {
        return map.keySet().stream()
            .map(k -> k + "=" + map.get(k))
            .collect(Collectors.joining(",\n", "{\n", "\n}"));
    }

    /**
     * Contract for {@link Collection}-s mapping.
     *
     * @param  type of result
     * @param  type of source
     */
    public interface Mapper {

        /**
         * Maps a provided value.
         *
         * @param input for mapping
         * @return mapped value
         */
        T_Result map(T_Source input);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy