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

liquibase.util.CollectionUtil Maven / Gradle / Ivy

There is a newer version: 3.6.2.5.inovus
Show newest version
package liquibase.util;

import java.util.*;

public class CollectionUtil {

    public static  Set> powerSet(Collection originalSet) {
        Set> sets = new HashSet<>();
        if (originalSet.isEmpty()) {
            sets.add(new HashSet());
            return sets;
        }
        List list = new ArrayList<>(originalSet);
        T head = list.get(0);
        Collection rest = list.subList(1, list.size());
        for (Set set : powerSet(rest)) {
            Set newSet = new HashSet<>();
            newSet.add(head);
            newSet.addAll(set);
            sets.add(newSet);
            sets.add(set);
        }
        return sets;
    }

    public static  List> permutations(Map> parameterValues) {
        List> list = new ArrayList<>();
        if ((parameterValues == null) || parameterValues.isEmpty()) {
            return list;
        }

        permute(new HashMap(), new ArrayList(parameterValues.keySet()), parameterValues, list);

        return list;


    }

    private static  void permute(Map basePermutation, List remainingKeys, Map> parameterValues, List> returnList) {

        String thisKey = remainingKeys.get(0);
        remainingKeys = remainingKeys.subList(1, remainingKeys.size());
        for (T value : parameterValues.get(thisKey)) {
            Map permutation = new HashMap<>(basePermutation);

            permutation.put(thisKey, value);

            if (remainingKeys.isEmpty()) {
                returnList.add(permutation);
            } else {
                permute(permutation, remainingKeys, parameterValues, returnList);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy