| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.util; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.equalTo; |
| 23 | import static com.google.common.base.Predicates.not; |
| 24 | import static com.google.common.collect.Maps.filterKeys; |
| 25 | |
| 26 | import java.util.LinkedHashMap; |
| 27 | import java.util.Map; |
| 28 | import java.util.Map.Entry; |
| 29 | |
| 30 | import com.google.common.base.Function; |
| 31 | import com.google.common.base.Supplier; |
| 32 | import com.google.common.collect.ImmutableMap; |
| 33 | import com.google.common.collect.Maps; |
| 34 | import com.google.common.collect.Multimap; |
| 35 | import com.google.common.collect.ImmutableMap.Builder; |
| 36 | |
| 37 | /** |
| 38 | * General utilities used in jclouds code for {@link Map}s. |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class Maps2 { |
| 43 | |
| 44 | public static <K, V> Map<K, V> convertUnsafe(Multimap<K, V> in) { |
| 45 | LinkedHashMap<K, V> out = Maps.newLinkedHashMap(); |
| 46 | for (Entry<K, V> entry : in.entries()) { |
| 47 | out.put(entry.getKey(), entry.getValue()); |
| 48 | } |
| 49 | return ImmutableMap.copyOf(out); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * If the supplied map contains the key {@code k1}, its value will be assigned to the key {@code |
| 54 | * k2}. Note that this doesn't modify the input map. |
| 55 | * |
| 56 | * @param <V> |
| 57 | * type of value the map holds |
| 58 | * @param in |
| 59 | * the map you wish to make a copy of |
| 60 | * @param k1 |
| 61 | * old key |
| 62 | * @param k2 |
| 63 | * new key |
| 64 | * @return copy of the map with the value of the key re-routed, or the original, if it {@code k1} |
| 65 | * wasn't present. |
| 66 | */ |
| 67 | public static <V> Map<String, V> renameKey(Map<String, V> in, String k1, String k2) { |
| 68 | if (checkNotNull(in, "input map").containsKey(checkNotNull(k1, "old key"))) { |
| 69 | Builder<String, V> builder = ImmutableMap.builder(); |
| 70 | builder.putAll(filterKeys(in, not(equalTo(k1)))); |
| 71 | V tags = in.get(k1); |
| 72 | builder.put(checkNotNull(k2, "new key"), tags); |
| 73 | in = builder.build(); |
| 74 | } |
| 75 | return in; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * change the keys but keep the values in-tact. |
| 80 | * |
| 81 | * @param <K1> |
| 82 | * input key type |
| 83 | * @param <K2> |
| 84 | * output key type |
| 85 | * @param <V> |
| 86 | * value type |
| 87 | * @param in |
| 88 | * input map to transform |
| 89 | * @param fn |
| 90 | * how to transform the values |
| 91 | * @return immutableMap with the new keys. |
| 92 | */ |
| 93 | public static <K1, K2, V> Map<K2, V> transformKeys(Map<K1, V> in, Function<K1, K2> fn) { |
| 94 | checkNotNull(in, "input map"); |
| 95 | checkNotNull(fn, "function"); |
| 96 | Builder<K2, V> returnVal = ImmutableMap.builder(); |
| 97 | for (Entry<K1, V> entry : in.entrySet()) |
| 98 | returnVal.put(fn.apply(entry.getKey()), entry.getValue()); |
| 99 | return returnVal.build(); |
| 100 | } |
| 101 | |
| 102 | public static <K, V> Supplier<Map<K, V>> composeMapSupplier(Iterable<Supplier<Map<K, V>>> suppliers) { |
| 103 | return new ListMapSupplier<K, V>(suppliers); |
| 104 | } |
| 105 | |
| 106 | static class ListMapSupplier<K, V> implements Supplier<Map<K, V>> { |
| 107 | |
| 108 | private final Iterable<Supplier<Map<K, V>>> suppliers; |
| 109 | |
| 110 | ListMapSupplier(Iterable<Supplier<Map<K, V>>> suppliers) { |
| 111 | this.suppliers = checkNotNull(suppliers, "suppliers"); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Map<K, V> get() { |
| 116 | Map<K, V> toReturn = Maps.newLinkedHashMap(); |
| 117 | for (Supplier<Map<K, V>> supplier : suppliers) { |
| 118 | toReturn.putAll(supplier.get()); |
| 119 | } |
| 120 | return toReturn; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } |