EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.trmk.vcloud_0_8.suppliers]

COVERAGE SUMMARY FOR SOURCE FILE [OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault.java]

nameclass, %method, %block, %line, %
OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault.java100% (2/2)86%  (6/7)91%  (116/128)95%  (21/22)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault$ReferenceTypeNameMatchesPattern100% (1/1)67%  (2/3)56%  (15/27)80%  (4/5)
toString (): String 0%   (0/1)0%   (0/12)0%   (0/1)
OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault$ReferenceType... 100% (1/1)100% (9/9)100% (3/3)
apply (ReferenceType): boolean 100% (1/1)100% (6/6)100% (1/1)
     
class OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault100% (1/1)100% (4/4)100% (101/101)100% (17/17)
OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault (ValueOfConfi... 100% (1/1)100% (21/21)100% (5/5)
apply (Iterable): ReferenceType 100% (1/1)100% (44/44)100% (8/8)
defaultReferenceType (Iterable): ReferenceType 100% (1/1)100% (6/6)100% (1/1)
findReferenceTypeWithNameMatchingPattern (Iterable, String): ReferenceType 100% (1/1)100% (30/30)100% (3/3)

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 */
19package org.jclouds.trmk.vcloud_0_8.suppliers;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23 
24import java.util.NoSuchElementException;
25 
26import org.jclouds.config.ValueOfConfigurationKeyOrNull;
27import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType;
28 
29import com.google.common.base.Function;
30import com.google.common.base.Predicate;
31import com.google.common.collect.Iterables;
32 
33/**
34 * 
35 * @author Adrian Cole
36 */
37public class OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault implements
38      Function<Iterable<ReferenceType>, ReferenceType> {
39 
40   protected final ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull;
41   protected final String configurationKey;
42   protected final Predicate<ReferenceType> defaultSelector;
43 
44   public OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(
45         ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, String configurationKey,
46         Predicate<ReferenceType> defaultSelector) {
47      this.configurationKey = checkNotNull(configurationKey, "configurationKey");
48      this.valueOfConfigurationKeyOrNull = checkNotNull(valueOfConfigurationKeyOrNull, "valueOfConfigurationKeyOrNull");
49      this.defaultSelector = checkNotNull(defaultSelector, "defaultSelector");
50   }
51 
52   @Override
53   public ReferenceType apply(Iterable<ReferenceType> referenceTypes) {
54      checkNotNull(referenceTypes, "referenceTypes");
55      checkArgument(Iterables.size(referenceTypes) > 0,
56            "No referenceTypes corresponding to configuration key %s present", configurationKey);
57      if (Iterables.size(referenceTypes) == 1)
58         return Iterables.getLast(referenceTypes);
59      String namingPattern = valueOfConfigurationKeyOrNull.apply(configurationKey);
60      if (namingPattern != null) {
61         return findReferenceTypeWithNameMatchingPattern(referenceTypes, namingPattern);
62      } else {
63         return defaultReferenceType(referenceTypes);
64      }
65   }
66 
67   public ReferenceType defaultReferenceType(Iterable<ReferenceType> referenceTypes) {
68      return Iterables.find(referenceTypes, defaultSelector);
69   }
70 
71   public ReferenceType findReferenceTypeWithNameMatchingPattern(Iterable<ReferenceType> referenceTypes,
72         String namingPattern) {
73      try {
74         return Iterables.find(referenceTypes, new ReferenceTypeNameMatchesPattern(namingPattern));
75      } catch (NoSuchElementException e) {
76         throw new NoSuchElementException(String.format(
77               "referenceType matching pattern [%s], corresponding to configuration key %s, not in %s", namingPattern,
78               configurationKey, referenceTypes));
79      }
80   }
81 
82   static class ReferenceTypeNameMatchesPattern implements Predicate<ReferenceType> {
83 
84      private final String namingPattern;
85 
86      public ReferenceTypeNameMatchesPattern(String namingPattern) {
87         this.namingPattern = checkNotNull(namingPattern, "namingPattern");
88      }
89 
90      @Override
91      public boolean apply(ReferenceType arg0) {
92         return arg0.getName().matches(namingPattern);
93      }
94 
95      @Override
96      public String toString() {
97         return "nameMatchesPattern(" + namingPattern + ")";
98 
99      }
100   }
101}

[all classes][org.jclouds.trmk.vcloud_0_8.suppliers]
EMMA 2.0.5312 (C) Vladimir Roubtsov