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

se.kuseman.payloadbuilder.api.utils.MapUtils Maven / Gradle / Ivy

package se.kuseman.payloadbuilder.api.utils;

import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/** Map utilities */
public class MapUtils
{
    private MapUtils()
    {
    }

    /** Build map out of entries */
    @SafeVarargs
    public static  Map ofEntries(SimpleEntry... values)
    {
        return ofEntries(false, values);
    }

    /** Build a map with kept insertion order out of entries */
    @SafeVarargs
    public static  Map ofEntries(boolean keepInsertOrder, SimpleEntry... values)
    {
        Map result = keepInsertOrder ? new LinkedHashMap<>()
                : new HashMap<>();

        for (SimpleEntry e : values)
        {
            if (result.put(e.getKey(), e.getValue()) != null)
            {
                throw new IllegalStateException(String.format("Duplicate key %s", e.getKey()));
            }
        }
        return result;
    }

    /** Build entry out of provided key and value */
    public static  SimpleEntry entry(K key, V value)
    {
        return new SimpleEntry<>(key, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy