EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.http.functions]

COVERAGE SUMMARY FOR SOURCE FILE [UnwrapOnlyNestedJsonValue.java]

nameclass, %method, %block, %line, %
UnwrapOnlyNestedJsonValue.java100% (1/1)100% (2/2)68%  (42/62)65%  (11/17)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class UnwrapOnlyNestedJsonValue100% (1/1)100% (2/2)68%  (42/62)65%  (11/17)
apply (HttpResponse): Object 100% (1/1)62%  (33/53)54%  (7/13)
UnwrapOnlyNestedJsonValue (ParseJson, TypeLiteral): void 100% (1/1)100% (9/9)100% (4/4)

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.http.functions;
20 
21import java.util.List;
22import java.util.Map;
23import java.util.Set;
24 
25import javax.inject.Inject;
26import javax.inject.Singleton;
27 
28import org.jclouds.http.HttpResponse;
29 
30import com.google.common.base.Function;
31import com.google.common.collect.ImmutableList;
32import com.google.common.collect.ImmutableMap;
33import com.google.common.collect.ImmutableSet;
34import com.google.common.collect.Iterables;
35import com.google.inject.TypeLiteral;
36 
37/**
38 * @author Adrian Cole
39 */
40@Singleton
41public class UnwrapOnlyNestedJsonValue<T> implements Function<HttpResponse, T> {
42 
43   private final ParseJson<Map<String, Map<String, T>>> json;
44   private final TypeLiteral<T> type;
45 
46   @Inject
47   UnwrapOnlyNestedJsonValue(ParseJson<Map<String, Map<String, T>>> json, TypeLiteral<T> type) {
48      this.json = json;
49      this.type = type;
50   }
51 
52   @SuppressWarnings("unchecked")
53   @Override
54   public T apply(HttpResponse arg0) {
55      Map<String, Map<String, T>> map = json.apply(arg0);
56      if (map == null || map.size() == 0)
57         return null;
58      Map<String, T> map1 = Iterables.getOnlyElement(map.values());
59      if (map1 == null || map1.size() == 0) {
60         if (type.getRawType().isAssignableFrom(Set.class))
61            return (T) ImmutableSet.of();
62         else if (type.getRawType().isAssignableFrom(List.class))
63            return (T) ImmutableList.of();
64         else if (type.getRawType().isAssignableFrom(Map.class))
65            return (T) ImmutableMap.of();
66         return null;
67      }
68      return Iterables.getOnlyElement(map1.values());
69   }
70}

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