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

org.javers.common.collections.Maps Maven / Gradle / Ivy

package org.javers.common.collections;

import org.javers.core.metamodel.property.MissingProperty;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author bartosz walacik
 */
public class Maps {

    public static Map wrapNull(Object map){
        if (map == null || map == MissingProperty.INSTANCE || !(map instanceof Map)){
            return Collections.emptyMap();
        }
        return (Map)map;
    }

    /**
     * null args are allowed
     */
    public static  Set commonKeys(Map left, Map right) {
        if (left == null || right == null) {
            return Collections.emptySet();
        }

        return Sets.intersection(left.keySet(),right.keySet());
    }

    /**
     * null args are allowed
     */
    public static  Set keysDifference(Map left, Map right) {
        if (left == null){
            return Collections.emptySet();
        }

        if (right == null){
            return left.keySet();
        }

        return Sets.difference(left.keySet(), right.keySet());
    }

    public static Map of(Object key, Object val) {
        Map m = new HashMap();
        m.put(key, val);
        return Collections.unmodifiableMap(m);
    }

    public static   Map merge(Map a, Map b) {
        if (a == null || a.isEmpty()) {
            return b;
        }

        if (b == null || b.isEmpty()) {
            return a;
        }

        Map m = new HashMap(b);
        m.putAll(a);

        return Collections.unmodifiableMap(m);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy