
org.jclouds.util.Maps2 Maven / Gradle / Ivy
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.util;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Maps.filterKeys;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
/**
* General utilities used in jclouds code for {@link Map Maps}.
*
* @author Adrian Cole
*/
public class Maps2 {
public static Map convertUnsafe(Multimap in) {
LinkedHashMap out = Maps.newLinkedHashMap();
for (Entry entry : in.entries()) {
out.put(entry.getKey(), entry.getValue());
}
return ImmutableMap.copyOf(out);
}
/**
* If the supplied map contains the key {@code k1}, its value will be assigned to the key {@code
* k2}. Note that this doesn't modify the input map.
*
* @param
* type of value the map holds
* @param in
* the map you wish to make a copy of
* @param k1
* old key
* @param k2
* new key
* @return copy of the map with the value of the key re-routed, or the original, if it {@code k1}
* wasn't present.
*/
public static Map renameKey(Map in, String k1, String k2) {
if (checkNotNull(in, "input map").containsKey(checkNotNull(k1, "old key"))) {
Builder builder = ImmutableMap.builder();
builder.putAll(filterKeys(in, not(equalTo(k1))));
V tags = in.get(k1);
builder.put(checkNotNull(k2, "new key"), tags);
in = builder.build();
}
return in;
}
/**
* change the keys but keep the values in-tact.
*
* @param
* input key type
* @param
* output key type
* @param
* value type
* @param in
* input map to transform
* @param fn
* how to transform the values
* @return immutableMap with the new keys.
*/
public static Map transformKeys(Map in, Function fn) {
checkNotNull(in, "input map");
checkNotNull(fn, "function");
Builder returnVal = ImmutableMap.builder();
for (Entry entry : in.entrySet())
returnVal.put(fn.apply(entry.getKey()), entry.getValue());
return returnVal.build();
}
public static Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy