EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.util]

COVERAGE SUMMARY FOR SOURCE FILE [Maps2.java]

nameclass, %method, %block, %line, %
Maps2.java50%  (1/2)43%  (3/7)71%  (93/131)63%  (17/27)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Maps2$ListMapSupplier0%   (0/1)0%   (0/2)0%   (0/30)0%   (0/7)
Maps2$ListMapSupplier (Iterable): void 0%   (0/1)0%   (0/9)0%   (0/3)
get (): Map 0%   (0/1)0%   (0/21)0%   (0/4)
     
class Maps2100% (1/1)60%  (3/5)92%  (93/101)85%  (17/20)
Maps2 (): void 0%   (0/1)0%   (0/3)0%   (0/2)
composeMapSupplier (Iterable): Supplier 0%   (0/1)0%   (0/5)0%   (0/1)
convertUnsafe (Multimap): Map 100% (1/1)100% (24/24)100% (4/4)
renameKey (Map, String, String): Map 100% (1/1)100% (35/35)100% (7/7)
transformKeys (Map, Function): Map 100% (1/1)100% (34/34)100% (6/6)

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 */
19package org.jclouds.util;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.base.Predicates.equalTo;
23import static com.google.common.base.Predicates.not;
24import static com.google.common.collect.Maps.filterKeys;
25 
26import java.util.LinkedHashMap;
27import java.util.Map;
28import java.util.Map.Entry;
29 
30import com.google.common.base.Function;
31import com.google.common.base.Supplier;
32import com.google.common.collect.ImmutableMap;
33import com.google.common.collect.Maps;
34import com.google.common.collect.Multimap;
35import 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 */
42public 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}

[all classes][org.jclouds.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov