1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. 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, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
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 | import java.util.Set; |
30 | |
31 | import com.google.common.base.Function; |
32 | import com.google.common.base.Supplier; |
33 | import com.google.common.collect.ImmutableMap; |
34 | import com.google.common.collect.ImmutableMap.Builder; |
35 | import com.google.common.collect.Maps; |
36 | import com.google.common.collect.Multimap; |
37 | |
38 | /** |
39 | * General utilities used in jclouds code for {@link Map Maps}. |
40 | * |
41 | * @author Adrian Cole |
42 | */ |
43 | public class Maps2 { |
44 | |
45 | public static <K, V> Map<K, V> convertUnsafe(Multimap<K, V> in) { |
46 | LinkedHashMap<K, V> out = Maps.newLinkedHashMap(); |
47 | for (Entry<K, V> entry : in.entries()) { |
48 | out.put(entry.getKey(), entry.getValue()); |
49 | } |
50 | return ImmutableMap.copyOf(out); |
51 | } |
52 | |
53 | /** |
54 | * If the supplied map contains the key {@code k1}, its value will be assigned to the key {@code |
55 | * k2}. Note that this doesn't modify the input map. |
56 | * |
57 | * @param <V> |
58 | * type of value the map holds |
59 | * @param in |
60 | * the map you wish to make a copy of |
61 | * @param k1 |
62 | * old key |
63 | * @param k2 |
64 | * new key |
65 | * @return copy of the map with the value of the key re-routed, or the original, if it {@code k1} |
66 | * wasn't present. |
67 | */ |
68 | public static <V> Map<String, V> renameKey(Map<String, V> in, String k1, String k2) { |
69 | if (checkNotNull(in, "input map").containsKey(checkNotNull(k1, "old key"))) { |
70 | Builder<String, V> builder = ImmutableMap.builder(); |
71 | builder.putAll(filterKeys(in, not(equalTo(k1)))); |
72 | V tags = in.get(k1); |
73 | builder.put(checkNotNull(k2, "new key"), tags); |
74 | in = builder.build(); |
75 | } |
76 | return in; |
77 | } |
78 | |
79 | /** |
80 | * change the keys but keep the values in-tact. |
81 | * |
82 | * @param <K1> |
83 | * input key type |
84 | * @param <K2> |
85 | * output key type |
86 | * @param <V> |
87 | * value type |
88 | * @param in |
89 | * input map to transform |
90 | * @param fn |
91 | * how to transform the values |
92 | * @return immutableMap with the new keys. |
93 | */ |
94 | public static <K1, K2, V> Map<K2, V> transformKeys(Map<K1, V> in, Function<K1, K2> fn) { |
95 | checkNotNull(in, "input map"); |
96 | checkNotNull(fn, "function"); |
97 | Builder<K2, V> returnVal = ImmutableMap.builder(); |
98 | for (Entry<K1, V> entry : in.entrySet()) |
99 | returnVal.put(fn.apply(entry.getKey()), entry.getValue()); |
100 | return returnVal.build(); |
101 | } |
102 | |
103 | public static <K, V> Supplier<Map<K, V>> composeMapSupplier(Iterable<Supplier<Map<K, V>>> suppliers) { |
104 | return new ListMapSupplier<K, V>(suppliers); |
105 | } |
106 | |
107 | static class ListMapSupplier<K, V> implements Supplier<Map<K, V>> { |
108 | |
109 | private final Iterable<Supplier<Map<K, V>>> suppliers; |
110 | |
111 | ListMapSupplier(Iterable<Supplier<Map<K, V>>> suppliers) { |
112 | this.suppliers = checkNotNull(suppliers, "suppliers"); |
113 | } |
114 | |
115 | @Override |
116 | public Map<K, V> get() { |
117 | Map<K, V> toReturn = Maps.newLinkedHashMap(); |
118 | for (Supplier<Map<K, V>> supplier : suppliers) { |
119 | toReturn.putAll(supplier.get()); |
120 | } |
121 | return toReturn; |
122 | } |
123 | } |
124 | |
125 | /** |
126 | * Constructs a map with the given keys where values are generated by the given function. |
127 | * Supports duplicate and {@code null} values, but {@code null} keys are <em>not</em> allowed. |
128 | * |
129 | * @param <K> the type of the keys |
130 | * @param <V> the type of the values |
131 | * @param keys the keys to be included in the map. Keys must be non-<code>null</code> |
132 | * @param valueFunction the function that produces values for the keys |
133 | * @return a map containing the keys from the given set with values which are generated from |
134 | * the keys |
135 | * @see Maps#uniqueIndex(Iterable, Function) |
136 | */ |
137 | public static <K, V> Map<K, V> fromKeys(Set<K> keys, Function<? super K, V> valueFunction) { |
138 | Map<K, V> result = Maps.newHashMapWithExpectedSize(keys.size()); |
139 | for (K key : keys) { |
140 | result.put(checkNotNull(key), valueFunction.apply(key)); |
141 | } |
142 | return result; |
143 | } |
144 | |
145 | } |