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: 4.29.2
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);
            }
        }
    }

    /**
     * Returns passed currentValue if it is not null and creates a new ArrayList if it is null.
     * 

* Example: values = createIfNull(values) */ public static List createIfNull(List currentValue) { if (currentValue == null) { return new ArrayList(); } else { return currentValue; } } /** * Returns a new empty array if the passed array is null. */ public static T[] createIfNull(T[] arguments) { if (arguments == null) { return (T[]) new Object[0]; } else { return arguments; } } /** * Returns a new empty set if the passed array is null. */ public static Set createIfNull(Set currentValue) { if (currentValue == null) { return new HashSet<>(); } else { return currentValue; } } /** * Converts a set of nested maps (like from yaml/json) into a flat map with dot-separated properties */ public static Map flatten(Map map) { if (map == null) { return null; } return flatten(null, map); } private static Map flatten(String prefix, Map map) { Map outMap = new HashMap<>(); for (Map.Entry entry : map.entrySet()) { String propertyName = entry.getKey(); if (prefix != null) { propertyName = prefix + "." + propertyName; } if (entry.getValue() instanceof Map) { outMap.putAll(flatten(propertyName, (Map) entry.getValue())); } else { outMap.put(propertyName, entry.getValue()); } } return outMap; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy