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

ru.greatbit.utils.collection.CollectionUtils Maven / Gradle / Ivy

package ru.greatbit.utils.collection;

import ru.greatbit.utils.serialize.JsonSerializer;
import ru.greatbit.utils.string.StringUtils;

import java.util.*;

/**
 * Created by azee on 4/29/14.
 */
public class CollectionUtils {

    /**
     * Merge lists
     * @param first - List
     * @param second - List
     * @return - List
     */
    public static  List mergeLists(List first, List second){
        Map dataMap = listToMap(first);

        for (T object : second){
            dataMap.put(object, object);
        }

        List resultObject = new LinkedList();
        for (T object : dataMap.keySet()){
            resultObject.add(object);
        }
        return resultObject;
    }

    /**
     * Merge lists of object
     * Objects are compared by serialisation value
     * @param first
     * @param second
     * @param 
     * @return
     * @throws Exception
     */
    public static  List mergeListsByValue(List first, List second) throws Exception {
        Map dataMap = listToMD5Map(first);

        for (T object : second){
            dataMap.put(StringUtils.getMd5String(JsonSerializer.marshal(object)), object);
        }

        List resultObject = new LinkedList();
        for (String key : dataMap.keySet()){
            resultObject.add(dataMap.get(key));
        }
        return resultObject;
    }



    /**
     * Get differences of lists
     * Objects should override hashCode and equals so they could be
     * compared in HashMap to find differences
     * @param first - List
     * @param second - List
     * @param  - Object class
     * @return Difference object
     */
    public static Difference getDiff(List first, List second){
        return getDiff(listToMap(first), listToMap(second));
    }

    /**
     * Get differences of lists
     * Uses serialised objects md5 - it will work slower
     * but can process objects if hashCode and equals can't be overridden
     * @param first
     * @param second
     * @param 
     * @return
     * @throws Exception
     */
    public static Difference getDiffAnyObject(List first, List second) throws Exception {
        return getDiff(listToMD5Map(first), listToMD5Map(second));
    }

    /**
     * Return a difference from 2 maps
     * @param firstMap
     * @param secondMap
     * @param 
     * @return
     */
    private static Difference getDiff(Map firstMap, Map secondMap){
        Difference difference = new Difference();

        for (K object : secondMap.keySet()){
            V value = firstMap.get(object);
            if (value == null){
                difference.getAdded().add(secondMap.get(object));
            } else {
                difference.getEqual().add(value);
            }
        }

        for (K object : firstMap.keySet()){
            V value = secondMap.get(object);
            if (value == null){
                difference.getRemoved().add(firstMap.get(object));
            }
        }
        return difference;
    }

    /**
     * Load a list to a map
     * @param input - List
     * @param  - Class of objects
     * @return - Map
     */
    public static  Map listToMap(List input){
        Map dataMap = new HashMap();
        for (V object : input){
            dataMap.put(object, object);
        }
        return dataMap;
    }

    /**
     * Load a list to a map, use serialised objects md5 - it will work slower
     * but can process objects if hashCode and equals can't be overridden
     * @param input
     * @param 
     * @return
     * @throws Exception
     */
    public static  Map listToMD5Map(List input) throws Exception {
        Map dataMap = new HashMap();
        for (T object : input){
            dataMap.put(StringUtils.getMd5String(JsonSerializer.marshal(object)), object);
        }
        return dataMap;
    }

    /**
     * Used to remove values from lists that don't support remove method
     * @param input
     * @param index
     * @param 
     * @return
     */
    public static  List removeByIndex(List input, int index) {
        List result = new LinkedList(input);
        result.remove(index);

        //Doing that to keep original input class
        if (input.getClass().getName().equals("java.util.Arrays$ArrayList")){
            input = (List) Arrays.asList(result.toArray());
        } else {
            input.clear();
            input.addAll(result);
        }
        return input;
    }

    /**
     * Remove duplicate values of list from map
     * @param values
     * @param 
     * @param 
     * @return
     */
    public static  Map> removeDuplicateValues(Map> values) {
        if (values == null){
            return values;
        }
        Map> newValues = new LinkedHashMap>();
        for (K key : values.keySet()) {
            newValues.put(key, removeDuplicateValues(values.get(key)));
        }
        return newValues;
    }

    /**
     * Remove duplicate values of list from list
     * @param values
     * @param 
     * @return
     */
    public static  List removeDuplicateValues(List values) {
        if (values == null){
            return values;
        }
        return new ArrayList(new HashSet(values));
    }
}