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

brooklyn.util.collections.MutableMap Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.util.collections;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.ImmutableMap;

/** Map impl, exposing simple builder operations (add) in a fluent-style API,
 * where the final map is mutable.  You can also toImmutable. */
public class MutableMap extends LinkedHashMap {
    private static final long serialVersionUID = -2463168443382874384L;

    public static  MutableMap of() {
        return new MutableMap();
    }
    
    public static  MutableMap of(K k1, V v1) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        return result;
    }
    
    public static  MutableMap of(K k1, V v1, K k2, V v2) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        result.put(k2, v2);
        return result;
    }
    
    public static  MutableMap of(K k1, V v1, K k2, V v2, K k3, V v3) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        result.put(k2, v2);
        result.put(k3, v3);
        return result;
    }

    public static  MutableMap of(K k1, V v1, K k2, V v2, K k3, V v3,K k4, V v4) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        result.put(k2, v2);
        result.put(k3, v3);
        result.put(k4, v4);
        return result;
    }

    public static  MutableMap of(K k1, V v1, K k2, V v2, K k3, V v3,K k4, V v4,K k5, V v5) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        result.put(k2, v2);
        result.put(k3, v3);
        result.put(k4, v4);
        result.put(k5, v5);
        return result;
    }

    public static  MutableMap of(K k1, V v1, K k2, V v2, K k3, V v3,K k4, V v4,K k5, V v5,K k6,V v6) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        result.put(k2, v2);
        result.put(k3, v3);
        result.put(k4, v4);
        result.put(k5, v5);
        result.put(k6, v6);
        return result;
    }

    public static  MutableMap of(K k1, V v1, K k2, V v2, K k3, V v3,K k4, V v4,K k5, V v5,K k6,V v6,K k7,V v7) {
        MutableMap result = new MutableMap();
        result.put(k1, v1);
        result.put(k2, v2);
        result.put(k3, v3);
        result.put(k4, v4);
        result.put(k5, v5);
        result.put(k6, v6);
        result.put(k7, v7);
        return result;
     }

    public static  MutableMap copyOf(Map orig) {
        MutableMap result = new MutableMap();
        result.putAll(orig);
        return result;
    }
    
    public MutableMap() {}
    @SuppressWarnings("unchecked")
    public MutableMap(@SuppressWarnings("rawtypes") Map source) { super(source); }

    /** as {@link #put(Object, Object)} but fluent style */
    public MutableMap add(K key, V value) {
        put(key, value);
        return this;
    }

    /** as {@link #putAll(Map)} but fluent style */
    public MutableMap add(Map m) {
        putAll(m);
        return this;
    }

    /** as {@link #put(Object, Object)} but excluding null values, and fluent style */
    public MutableMap addIfNotNull(K key, V value) {
        if (value!=null) add(key, value);
        return this;
    }

    public ImmutableMap toImmutable() {
        return ImmutableMap.copyOf(this);
    }
    
    public static  Builder builder() {
        return new Builder();
    }

    /**
     * @see guava's ImmutableMap.Builder
     */
    public static class Builder {
        final MutableMap result = new MutableMap();

        public Builder() {}

        public Builder put(K key, V value) {
            result.put(key, value);
            return this;
        }

        public Builder putIfNotNull(K key, V value) {
            if (value!=null) result.put(key, value);
            return this;
        }

        public Builder putIfAbsent(K key, V value) {
            if (!result.containsKey(key)) result.put(key, value);
            return this;
        }

        public Builder put(Entry entry) {
            result.put(entry.getKey(), entry.getValue());
            return this;
        }

        public Builder putAll(Map map) {
            result.putAll(map);
            return this;
        }

        public Builder remove(K key) {
            result.remove(key);
            return this;
        }
        
        public Builder removeAll(K... keys) {
            for (K key : keys) {
                result.remove(key);
            }
            return this;
        }

        public Builder removeAll(Iterable keys) {
        	for (K key : keys) {
        		result.remove(key);
        	}
            return this;
        }

        /** moves the value stored under oldKey to newKey, if there was such a value */
        public Builder renameKey(K oldKey, K newKey) {
            if (result.containsKey(oldKey)) {
                V oldValue = result.remove(oldKey);
                result.put(newKey, oldValue);
            }
            return this;
        }
        
        public MutableMap build() {
          return new MutableMap(result);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy