
org.apache.brooklyn.util.collections.MutableMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-utils-common Show documentation
Show all versions of brooklyn-utils-common Show documentation
Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.brooklyn.util.collections;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.guava.Maybe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Predicate;
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;
private static final Logger log = LoggerFactory.getLogger(MutableMap.class);
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(@Nullable Map extends K, ? extends V> orig) {
MutableMap result = new MutableMap();
if (orig!=null)
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 (and accepting null, ignoring it) */
public MutableMap add(@Nullable Map extends K,? extends V> m) {
if (m!=null) 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 Maybe getMaybe(K key) {
if (containsKey(key)) return Maybe.of(get(key));
return Maybe.absent("No entry for key '"+key+"' in this map");
}
/** @deprecated since 0.7.0, use {@link #asImmutableCopy()}, or {@link #asUnmodifiable()} / {@link #asUnmodifiableCopy()} */ @Deprecated
public ImmutableMap toImmutable() {
return ImmutableMap.copyOf(this);
}
/** as {@link MutableList#asImmutableCopy()} */
public Map asImmutableCopy() {
try {
return ImmutableMap.copyOf(this);
} catch (Exception e) {
Exceptions.propagateIfFatal(e);
log.warn("Error converting list to Immutable, using unmodifiable instead: "+e, e);
return asUnmodifiableCopy();
}
}
/** as {@link MutableList#asUnmodifiable()} */
public Map asUnmodifiable() {
return Collections.unmodifiableMap(this);
}
/** as {@link MutableList#asUnmodifiableCopy()} */
public Map asUnmodifiableCopy() {
return Collections.unmodifiableMap(MutableMap.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 extends K, ? extends V> entry) {
result.put(entry.getKey(), entry.getValue());
return this;
}
public Builder putAll(Map extends K, ? extends V> map) {
result.add(map);
return this;
}
public Builder remove(K key) {
result.remove(key);
return this;
}
public Builder removeAll(@SuppressWarnings("unchecked") K... keys) {
for (K key : keys) {
result.remove(key);
}
return this;
}
public Builder removeAll(Iterable extends K> 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);
}
public Builder filterValues(Predicate super V> filter) {
for (Iterator iter = result.values().iterator(); iter.hasNext();) {
V val = iter.next();
if (!filter.apply(val)) iter.remove();
}
return this;
}
public Builder filterKeys(Predicate super K> filter) {
for (Iterator iter = result.keySet().iterator(); iter.hasNext();) {
K key = iter.next();
if (!filter.apply(key)) iter.remove();
}
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy