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

liquibase.util.CollectionUtil Maven / Gradle / Ivy

The 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) * @deprecated use {@link org.apache.commons.collections4.CollectionUtils} instead */ @Deprecated public static List createIfNull(List currentValue) { return ObjectUtil.defaultIfNull(currentValue, new ArrayList<>()); } /** * Returns a new empty array if the passed array is null. * @deprecated use {@link org.apache.commons.collections4.CollectionUtils} instead */ @Deprecated public static T[] createIfNull(T[] arguments) { return ObjectUtil.defaultIfNull(arguments, (T[]) new Object[0]); } /** * Returns a new empty set if the passed set is null. * @deprecated use {@link org.apache.commons.collections4.CollectionUtils} instead */ @Deprecated public static Set createIfNull(Set currentValue) { return ObjectUtil.defaultIfNull(currentValue, new HashSet<>()); } /** * Returns a new empty map if the passed map is null. * @deprecated use {@link org.apache.commons.collections4.CollectionUtils} instead */ @Deprecated public static Map createIfNull(Map currentValue) { return ObjectUtil.defaultIfNull(currentValue, new HashMap<>()); } /** * 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; } /** * Find the actual key in a map, by searching the keys in the map and checking them ignoring case. * @param key the key to search for, in any case * @param map the map in which to search * @return the properly cased key, if found, or null if not found */ public static String findKeyInMapIgnoreCase(String key, Map map) { for (Map.Entry mapEntry : map.entrySet()) { String actualKey = mapEntry.getKey(); if (actualKey.equalsIgnoreCase(key)) { return actualKey; } } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy