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

COVERAGE SUMMARY FOR SOURCE FILE [TemplateBuilderImpl.java]

nameclass, %method, %block, %line, %
TemplateBuilderImpl.java100% (24/24)61%  (60/99)60%  (1101/1845)58%  (162.9/283)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TemplateBuilderImpl$11100% (1/1)33%  (1/3)10%  (6/58)12%  (1/8)
apply (Image): boolean 0%   (0/1)0%   (0/39)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$11 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$10100% (1/1)33%  (1/3)12%  (6/51)12%  (1/8)
apply (Image): boolean 0%   (0/1)0%   (0/32)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$10 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$4100% (1/1)33%  (1/3)12%  (6/51)12%  (1/8)
apply (OperatingSystem): boolean 0%   (0/1)0%   (0/32)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$4 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$5100% (1/1)33%  (1/3)12%  (6/51)12%  (1/8)
apply (OperatingSystem): boolean 0%   (0/1)0%   (0/32)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$5 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$6100% (1/1)33%  (1/3)12%  (6/51)12%  (1/8)
apply (OperatingSystem): boolean 0%   (0/1)0%   (0/32)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$6 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$9100% (1/1)33%  (1/3)12%  (6/51)12%  (1/8)
apply (Image): boolean 0%   (0/1)0%   (0/32)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$9 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$15100% (1/1)50%  (1/2)14%  (3/21)50%  (1/2)
compare (Hardware, Hardware): int 0%   (0/1)0%   (0/18)0%   (0/1)
TemplateBuilderImpl$15 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class TemplateBuilderImpl$7100% (1/1)33%  (1/3)14%  (6/42)12%  (1/8)
apply (OperatingSystem): boolean 0%   (0/1)0%   (0/23)0%   (0/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$7 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$3100% (1/1)33%  (1/3)18%  (6/34)17%  (1/6)
apply (OperatingSystem): boolean 0%   (0/1)0%   (0/15)0%   (0/4)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$3 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$16100% (1/1)50%  (1/2)33%  (3/9)50%  (1/2)
compare (Hardware, Hardware): int 0%   (0/1)0%   (0/6)0%   (0/1)
TemplateBuilderImpl$16 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class TemplateBuilderImpl$12100% (1/1)67%  (2/3)34%  (14/41)50%  (4/8)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
apply (Hardware): boolean 100% (1/1)36%  (8/22)50%  (3/6)
TemplateBuilderImpl$12 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$1100% (1/1)67%  (2/3)39%  (27/69)71%  (4.2/6)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
apply (ComputeMetadata): boolean 100% (1/1)42%  (21/50)81%  (3.2/4)
TemplateBuilderImpl$1 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$21100% (1/1)67%  (2/3)48%  (12/25)67%  (2/3)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
TemplateBuilderImpl$21 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
apply (Image): boolean 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$14100% (1/1)67%  (2/3)52%  (15/29)63%  (1.9/3)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
apply (Hardware): boolean 100% (1/1)90%  (9/10)90%  (0.9/1)
TemplateBuilderImpl$14 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$18100% (1/1)67%  (2/3)56%  (15/27)67%  (2/3)
toString (): String 0%   (0/1)0%   (0/12)0%   (0/1)
TemplateBuilderImpl$18 (TemplateBuilderImpl, String): void 100% (1/1)100% (9/9)100% (1/1)
apply (Location): boolean 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$19$1100% (1/1)67%  (2/3)56%  (15/27)67%  (2/3)
toString (): String 0%   (0/1)0%   (0/12)0%   (0/1)
TemplateBuilderImpl$19$1 (TemplateBuilderImpl$19, Hardware): void 100% (1/1)100% (9/9)100% (1/1)
apply (Image): boolean 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$20100% (1/1)67%  (2/3)56%  (15/27)67%  (2/3)
toString (): String 0%   (0/1)0%   (0/12)0%   (0/1)
TemplateBuilderImpl$20 (TemplateBuilderImpl, Hardware): void 100% (1/1)100% (9/9)100% (1/1)
apply (Image): boolean 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$13100% (1/1)67%  (2/3)56%  (18/32)73%  (2.9/4)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
apply (Hardware): boolean 100% (1/1)92%  (12/13)96%  (1.9/2)
TemplateBuilderImpl$13 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$8100% (1/1)67%  (2/3)69%  (35/51)75%  (6/8)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
apply (OperatingSystem): boolean 100% (1/1)91%  (29/32)83%  (5/6)
TemplateBuilderImpl$8 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class TemplateBuilderImpl$22100% (1/1)67%  (2/3)76%  (16/21)67%  (2/3)
toString (): String 0%   (0/1)0%   (0/5)0%   (0/1)
TemplateBuilderImpl$22 (TemplateBuilderImpl, List): void 100% (1/1)100% (9/9)100% (1/1)
apply (Image): boolean 100% (1/1)100% (7/7)100% (1/1)
     
class TemplateBuilderImpl100% (1/1)68%  (23/34)78%  (752/964)73%  (133.9/183)
any (): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/1)
biggest (): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
fastest (): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
fromHardware (Hardware): TemplateBuilder 0%   (0/1)0%   (0/19)0%   (0/5)
fromTemplate (Template): TemplateBuilder 0%   (0/1)0%   (0/21)0%   (0/5)
hardwareId (String): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
minCores (double): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
minRam (int): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
os64Bit (boolean): TemplateBuilder 0%   (0/1)0%   (0/6)0%   (0/2)
osNameMatches (String): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
osVersionMatches (String): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/2)
resolveImage (Hardware, Iterable): Image 100% (1/1)44%  (30/68)60%  (6/10)
fromImage (Image): TemplateBuilder 100% (1/1)51%  (42/83)55%  (11/20)
buildImagePredicate (): Predicate 100% (1/1)66%  (77/117)70%  (19/27)
hardwareSorter (): Ordering 100% (1/1)72%  (13/18)83%  (5/6)
currentLocationWiderThan (Location): boolean 100% (1/1)88%  (14/16)87%  (0.9/1)
<static initializer> 100% (1/1)100% (13/13)100% (3/3)
TemplateBuilderImpl (Supplier, Supplier, Supplier, Supplier, Provider, Provid... 100% (1/1)100% (133/133)100% (24/24)
access$000 (TemplateBuilderImpl): Predicate 100% (1/1)100% (3/3)100% (1/1)
build (): Template 100% (1/1)100% (108/108)100% (19/19)
getImages (): Set 100% (1/1)100% (5/5)100% (1/1)
imageDescriptionMatches (String): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
imageId (String): TemplateBuilder 100% (1/1)100% (32/32)100% (11/11)
imageNameMatches (String): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
imageVersionMatches (String): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
locationId (String): TemplateBuilder 100% (1/1)100% (35/35)100% (6/6)
nothingChangedExceptOptions (): boolean 100% (1/1)100% (54/54)100% (1/1)
options (TemplateOptions): TemplateBuilder 100% (1/1)100% (15/15)100% (3/3)
osArchMatches (String): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
osDescriptionMatches (String): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
osFamily (OsFamily): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
resolveSize (Ordering, Iterable): Hardware 100% (1/1)100% (56/56)100% (8/8)
smallest (): TemplateBuilder 100% (1/1)100% (5/5)100% (2/2)
toString (): String 100% (1/1)100% (87/87)100% (1/1)
     
class TemplateBuilderImpl$17100% (1/1)100% (2/2)100% (54/54)100% (2/2)
TemplateBuilderImpl$17 (): void 100% (1/1)100% (3/3)100% (1/1)
compare (Image, Image): int 100% (1/1)100% (51/51)100% (1/1)
     
class TemplateBuilderImpl$19100% (1/1)100% (2/2)100% (18/18)100% (2/2)
TemplateBuilderImpl$19 (TemplateBuilderImpl, Iterable): void 100% (1/1)100% (9/9)100% (1/1)
apply (Hardware): boolean 100% (1/1)100% (9/9)100% (1/1)
     
class TemplateBuilderImpl$2100% (1/1)100% (3/3)100% (41/41)100% (8/8)
TemplateBuilderImpl$2 (TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
apply (Image): boolean 100% (1/1)100% (22/22)100% (6/6)
toString (): String 100% (1/1)100% (13/13)100% (1/1)

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.compute.domain.internal;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.base.Predicates.and;
23import static com.google.common.collect.Iterables.filter;
24import static com.google.common.collect.Lists.newArrayList;
25import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
26import static org.jclouds.compute.util.ComputeServiceUtils.getCoresAndSpeed;
27import static org.jclouds.compute.util.ComputeServiceUtils.getSpace;
28 
29import java.util.List;
30import java.util.NoSuchElementException;
31import java.util.Set;
32 
33import javax.annotation.Resource;
34import javax.inject.Inject;
35import javax.inject.Named;
36import javax.inject.Provider;
37 
38import org.jclouds.collect.Memoized;
39import org.jclouds.compute.domain.ComputeMetadata;
40import org.jclouds.compute.domain.Hardware;
41import org.jclouds.compute.domain.Image;
42import org.jclouds.compute.domain.OperatingSystem;
43import org.jclouds.compute.domain.OsFamily;
44import org.jclouds.compute.domain.Template;
45import org.jclouds.compute.domain.TemplateBuilder;
46import org.jclouds.compute.options.TemplateOptions;
47import org.jclouds.compute.reference.ComputeServiceConstants;
48import org.jclouds.domain.Location;
49import org.jclouds.logging.Logger;
50import org.jclouds.util.Lists2;
51 
52import com.google.common.annotations.VisibleForTesting;
53import com.google.common.base.Predicate;
54import com.google.common.base.Predicates;
55import com.google.common.base.Supplier;
56import com.google.common.collect.ComparisonChain;
57import com.google.common.collect.ImmutableList;
58import com.google.common.collect.Iterables;
59import com.google.common.collect.Ordering;
60import com.google.common.primitives.Doubles;
61 
62/**
63 * 
64 * @author Adrian Cole
65 */
66@SuppressWarnings("unchecked")
67public class TemplateBuilderImpl implements TemplateBuilder {
68 
69   @Resource
70   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
71   protected Logger logger = Logger.NULL;
72 
73   protected final Supplier<Set<? extends Image>> images;
74   protected final Supplier<Set<? extends Hardware>> hardwares;
75   protected final Supplier<Set<? extends Location>> locations;
76   protected final Supplier<Location> defaultLocation;
77   protected final Provider<TemplateOptions> optionsProvider;
78   protected final Provider<TemplateBuilder> defaultTemplateProvider;
79 
80   @VisibleForTesting
81   protected Location location;
82   @VisibleForTesting
83   protected String imageId;
84   @VisibleForTesting
85   protected String hardwareId;
86   @VisibleForTesting
87   protected String imageVersion;
88   @VisibleForTesting
89   protected OsFamily osFamily;
90   @VisibleForTesting
91   protected String osVersion;
92   @VisibleForTesting
93   protected Boolean os64Bit;
94   @VisibleForTesting
95   protected String osName;
96   @VisibleForTesting
97   protected String osDescription;
98   @VisibleForTesting
99   protected String osArch;
100   @VisibleForTesting
101   protected String imageName;
102   @VisibleForTesting
103   protected String imageDescription;
104   @VisibleForTesting
105   protected double minCores;
106   @VisibleForTesting
107   protected int minRam;
108   @VisibleForTesting
109   protected boolean biggest;
110   @VisibleForTesting
111   protected boolean fastest;
112   @VisibleForTesting
113   protected TemplateOptions options;
114 
115   @Inject
116   protected TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
117         @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> hardwares,
118         Supplier<Location> defaultLocation2, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
119         @Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider) {
120      this.locations = locations;
121      this.images = images;
122      this.hardwares = hardwares;
123      this.defaultLocation = defaultLocation2;
124      this.optionsProvider = optionsProvider;
125      this.defaultTemplateProvider = defaultTemplateProvider;
126   }
127 
128   /**
129    * If the current location id is null, then we don't care where to launch a node.
130    * 
131    * If the input location is null, then the data isn't location sensitive
132    * 
133    * If the input location is a parent of the specified location, then we are ok.
134    */
135   private final Predicate<ComputeMetadata> locationPredicate = new Predicate<ComputeMetadata>() {
136      @Override
137      public boolean apply(ComputeMetadata input) {
138         boolean returnVal = true;
139         if (location != null && input.getLocation() != null)
140            returnVal = location.equals(input.getLocation()) || location.getParent() != null
141                  && location.getParent().equals(input.getLocation()) || location.getParent().getParent() != null
142                  && location.getParent().getParent().equals(input.getLocation());
143         return returnVal;
144      }
145 
146      @Override
147      public String toString() {
148         return "location(" + location + ")";
149      }
150   };
151 
152   private final Predicate<Image> idPredicate = new Predicate<Image>() {
153      @Override
154      public boolean apply(Image input) {
155         boolean returnVal = true;
156         if (imageId != null) {
157            returnVal = imageId.equals(input.getId());
158            // match our input params so that the later predicates pass.
159            if (returnVal) {
160               fromImage(input);
161            }
162         }
163         return returnVal;
164      }
165 
166      @Override
167      public String toString() {
168         return "imageId(" + imageId + ")";
169      }
170   };
171 
172   private final Predicate<OperatingSystem> osFamilyPredicate = new Predicate<OperatingSystem>() {
173 
174      @Override
175      public boolean apply(OperatingSystem input) {
176         boolean returnVal = true;
177         if (osFamily != null)
178            returnVal = osFamily.equals(input.getFamily());
179         return returnVal;
180      }
181 
182      @Override
183      public String toString() {
184         return "osFamily(" + osFamily + ")";
185      }
186   };
187 
188   private final Predicate<OperatingSystem> osNamePredicate = new Predicate<OperatingSystem>() {
189      @Override
190      public boolean apply(OperatingSystem input) {
191         boolean returnVal = true;
192         if (osName != null) {
193            if (input.getName() == null)
194               returnVal = false;
195            else
196               returnVal = input.getName().contains(osName) || input.getName().matches(osName);
197         }
198         return returnVal;
199      }
200 
201      @Override
202      public String toString() {
203         return "osName(" + osName + ")";
204      }
205   };
206 
207   private final Predicate<OperatingSystem> osDescriptionPredicate = new Predicate<OperatingSystem>() {
208      @Override
209      public boolean apply(OperatingSystem input) {
210         boolean returnVal = true;
211         if (osDescription != null) {
212            if (input.getDescription() == null)
213               returnVal = false;
214            else
215               returnVal = input.getDescription().contains(osDescription)
216                     || input.getDescription().matches(osDescription);
217         }
218         return returnVal;
219      }
220 
221      @Override
222      public String toString() {
223         return "osDescription(" + osDescription + ")";
224      }
225   };
226 
227   private final Predicate<OperatingSystem> osVersionPredicate = new Predicate<OperatingSystem>() {
228      @Override
229      public boolean apply(OperatingSystem input) {
230         boolean returnVal = true;
231         if (osVersion != null) {
232            if (input.getVersion() == null)
233               returnVal = false;
234            else
235               returnVal = input.getVersion().contains(osVersion) || input.getVersion().matches(osVersion);
236         }
237         return returnVal;
238      }
239 
240      @Override
241      public String toString() {
242         return "osVersion(" + osVersion + ")";
243      }
244   };
245 
246   private final Predicate<OperatingSystem> os64BitPredicate = new Predicate<OperatingSystem>() {
247      @Override
248      public boolean apply(OperatingSystem input) {
249         boolean returnVal = true;
250         if (os64Bit != null) {
251            if (os64Bit)
252               return input.is64Bit();
253            else
254               return !input.is64Bit();
255         }
256         return returnVal;
257      }
258 
259      @Override
260      public String toString() {
261         return "os64Bit(" + os64Bit + ")";
262      }
263   };
264 
265   private final Predicate<OperatingSystem> osArchPredicate = new Predicate<OperatingSystem>() {
266      @Override
267      public boolean apply(OperatingSystem input) {
268         boolean returnVal = true;
269         if (osArch != null) {
270            if (input.getArch() == null)
271               returnVal = false;
272            else
273               returnVal = input.getArch().contains(osArch) || input.getArch().matches(osArch);
274         }
275         return returnVal;
276      }
277 
278      @Override
279      public String toString() {
280         return "osArch(" + osArch + ")";
281      }
282   };
283 
284   private final Predicate<Image> imageVersionPredicate = new Predicate<Image>() {
285      @Override
286      public boolean apply(Image input) {
287         boolean returnVal = true;
288         if (imageVersion != null) {
289            if (input.getVersion() == null)
290               returnVal = false;
291            else
292               returnVal = input.getVersion().contains(imageVersion) || input.getVersion().matches(imageVersion);
293         }
294         return returnVal;
295      }
296 
297      @Override
298      public String toString() {
299         return "imageVersion(" + imageVersion + ")";
300      }
301   };
302 
303   private final Predicate<Image> imageNamePredicate = new Predicate<Image>() {
304      @Override
305      public boolean apply(Image input) {
306         boolean returnVal = true;
307         if (imageName != null) {
308            if (input.getName() == null)
309               returnVal = false;
310            else
311               returnVal = input.getName().contains(imageName) || input.getName().matches(imageName);
312         }
313         return returnVal;
314      }
315 
316      @Override
317      public String toString() {
318         return "imageName(" + imageName + ")";
319      }
320   };
321   private final Predicate<Image> imageDescriptionPredicate = new Predicate<Image>() {
322      @Override
323      public boolean apply(Image input) {
324         boolean returnVal = true;
325         if (imageDescription != null) {
326            if (input.getDescription() == null)
327               returnVal = false;
328            else
329               returnVal = input.getDescription().equals(imageDescription)
330                     || input.getDescription().contains(imageDescription)
331                     || input.getDescription().matches(imageDescription);
332         }
333         return returnVal;
334      }
335 
336      @Override
337      public String toString() {
338         return "imageDescription(" + imageDescription + ")";
339      }
340   };
341   private final Predicate<Hardware> hardwareIdPredicate = new Predicate<Hardware>() {
342      @Override
343      public boolean apply(Hardware input) {
344         boolean returnVal = true;
345         if (hardwareId != null) {
346            returnVal = hardwareId.equals(input.getId());
347            // match our input params so that the later predicates pass.
348            if (returnVal) {
349               fromHardware(input);
350            }
351         }
352         return returnVal;
353      }
354 
355      @Override
356      public String toString() {
357         return "hardwareId(" + hardwareId + ")";
358      }
359   };
360 
361   private final Predicate<Hardware> hardwareCoresPredicate = new Predicate<Hardware>() {
362      @Override
363      public boolean apply(Hardware input) {
364         double cores = getCores(input);
365         return cores >= TemplateBuilderImpl.this.minCores;
366      }
367 
368      @Override
369      public String toString() {
370         return "minCores(" + minCores + ")";
371      }
372   };
373 
374   private final Predicate<Hardware> hardwareRamPredicate = new Predicate<Hardware>() {
375      @Override
376      public boolean apply(Hardware input) {
377         return input.getRam() >= TemplateBuilderImpl.this.minRam;
378      }
379 
380      @Override
381      public String toString() {
382         return "minRam(" + minRam + ")";
383      }
384   };
385   private final Predicate<Hardware> hardwarePredicate = and(hardwareIdPredicate, locationPredicate,
386         hardwareCoresPredicate, hardwareRamPredicate);
387 
388   static final Ordering<Hardware> DEFAULT_SIZE_ORDERING = new Ordering<Hardware>() {
389      public int compare(Hardware left, Hardware right) {
390         return ComparisonChain.start().compare(getCores(left), getCores(right)).compare(left.getRam(), right.getRam())
391               .compare(getSpace(left), getSpace(right)).result();
392      }
393   };
394   static final Ordering<Hardware> BY_CORES_ORDERING = new Ordering<Hardware>() {
395      public int compare(Hardware left, Hardware right) {
396         return Doubles.compare(getCoresAndSpeed(left), getCoresAndSpeed(right));
397      }
398   };
399   static final Ordering<Image> DEFAULT_IMAGE_ORDERING = new Ordering<Image>() {
400      public int compare(Image left, Image right) {
401         return ComparisonChain.start()
402               .compare(left.getName(), right.getName(), Ordering.<String> natural().nullsLast())
403               .compare(left.getVersion(), right.getVersion(), Ordering.<String> natural().nullsLast())
404               .compare(left.getOperatingSystem().getName(), right.getOperatingSystem().getName(),//
405                     Ordering.<String> natural().nullsLast())
406               .compare(left.getOperatingSystem().getVersion(), right.getOperatingSystem().getVersion(),//
407                     Ordering.<String> natural().nullsLast())
408               .compare(left.getOperatingSystem().getDescription(), right.getOperatingSystem().getDescription(),//
409                     Ordering.<String> natural().nullsLast())
410               .compare(left.getOperatingSystem().getArch(), right.getOperatingSystem().getArch()).result();
411      }
412   };
413 
414   /**
415    * {@inheritDoc}
416    */
417   @Override
418   public TemplateBuilder fromTemplate(Template template) {
419      location = template.getLocation();
420      fromHardware(template.getHardware());
421      fromImage(template.getImage());
422      options(template.getOptions());
423      return this;
424   }
425 
426   /**
427    * {@inheritDoc}
428    */
429   @Override
430   public TemplateBuilder fromHardware(Hardware hardware) {
431      if (currentLocationWiderThan(hardware.getLocation()))
432         this.location = hardware.getLocation();
433      this.minCores = getCores(hardware);
434      this.minRam = hardware.getRam();
435      return this;
436   }
437 
438   private boolean currentLocationWiderThan(Location location) {
439      return this.location == null || (location != null && this.location.getScope().compareTo(location.getScope()) < 0);
440   }
441 
442   /**
443    * {@inheritDoc}
444    */
445   @Override
446   public TemplateBuilder fromImage(Image image) {
447      if (currentLocationWiderThan(image.getLocation()))
448         this.location = image.getLocation();
449      if (image.getOperatingSystem().getFamily() != null)
450         this.osFamily = image.getOperatingSystem().getFamily();
451      if (image.getName() != null)
452         this.imageName = image.getName();
453      if (image.getDescription() != null)
454         this.imageDescription = image.getDescription();
455      if (image.getOperatingSystem().getName() != null)
456         this.osName = image.getOperatingSystem().getName();
457      if (image.getOperatingSystem().getDescription() != null)
458         this.osDescription = image.getOperatingSystem().getDescription();
459      if (image.getVersion() != null)
460         this.imageVersion = image.getVersion();
461      if (image.getOperatingSystem().getVersion() != null)
462         this.osVersion = image.getOperatingSystem().getVersion();
463      this.os64Bit = image.getOperatingSystem().is64Bit();
464      if (image.getOperatingSystem().getArch() != null)
465         this.osArch = image.getOperatingSystem().getArch();
466      return this;
467   }
468 
469   /**
470    * {@inheritDoc}
471    */
472   @Override
473   public TemplateBuilder smallest() {
474      this.biggest = false;
475      return this;
476   }
477 
478   /**
479    * {@inheritDoc}
480    */
481   @Override
482   public TemplateBuilder biggest() {
483      this.biggest = true;
484      return this;
485   }
486 
487   /**
488    * {@inheritDoc}
489    */
490   @Override
491   public TemplateBuilder fastest() {
492      this.fastest = true;
493      return this;
494   }
495 
496   /**
497    * {@inheritDoc}
498    */
499   @Override
500   public TemplateBuilder locationId(final String locationId) {
501      Set<? extends Location> locations = this.locations.get();
502      try {
503         this.location = Iterables.find(locations, new Predicate<Location>() {
504 
505            @Override
506            public boolean apply(Location input) {
507               return input.getId().equals(locationId);
508            }
509 
510            @Override
511            public String toString() {
512               return "locationId(" + locationId + ")";
513            }
514 
515         });
516      } catch (NoSuchElementException e) {
517         throw new NoSuchElementException(String.format("location id %s not found in: %s", locationId, locations));
518      }
519      return this;
520   }
521 
522   /**
523    * {@inheritDoc}
524    */
525   @Override
526   public TemplateBuilder osFamily(OsFamily os) {
527      this.osFamily = os;
528      return this;
529   }
530 
531   /**
532    * {@inheritDoc}
533    */
534   @Override
535   public Template build() {
536      if (nothingChangedExceptOptions()) {
537         TemplateBuilder defaultTemplate = defaultTemplateProvider.get();
538         if (options != null)
539            defaultTemplate.options(options);
540         return defaultTemplate.build();
541      }
542      if (location == null)
543         location = defaultLocation.get();
544      if (options == null)
545         options = optionsProvider.get();
546      logger.debug(">> searching params(%s)", this);
547      Set<? extends Image> images = getImages();
548      Predicate<Image> imagePredicate = buildImagePredicate();
549      Iterable<? extends Image> supportedImages = filter(images, buildImagePredicate());
550      if (Iterables.size(supportedImages) == 0)
551         throw new NoSuchElementException(String.format(
552               "no image matched predicate %s images that didn't match below:\n%s", imagePredicate, images));
553      Hardware hardware = resolveSize(hardwareSorter(), supportedImages);
554      Image image = resolveImage(hardware, supportedImages);
555      logger.debug("<<   matched image(%s)", image);
556 
557      return new TemplateImpl(image, hardware, location, options);
558   }
559 
560   protected Hardware resolveSize(Ordering<Hardware> hardwareOrdering, final Iterable<? extends Image> images) {
561      Set<? extends Hardware> hardwarel = hardwares.get();
562      Hardware hardware;
563      try {
564         Iterable<? extends Hardware> hardwaresThatAreCompatibleWithOurImages = filter(hardwarel,
565               new Predicate<Hardware>() {
566                  @Override
567                  public boolean apply(final Hardware hardware) {
568                     return Iterables.any(images, new Predicate<Image>() {
569 
570                        @Override
571                        public boolean apply(Image input) {
572                           return hardware.supportsImage().apply(input);
573                        }
574 
575                        @Override
576                        public String toString() {
577                           return "hardware(" + hardware + ").supportsImage()";
578                        }
579 
580                     });
581 
582                  }
583               });
584         hardware = hardwareOrdering.max(filter(hardwaresThatAreCompatibleWithOurImages, hardwarePredicate));
585      } catch (NoSuchElementException exception) {
586         throw new NoSuchElementException("hardware don't support any images: " + toString() + "\n" + hardwarel
587               + "\n" + images);
588      }
589      logger.debug("<<   matched hardware(%s)", hardware);
590      return hardware;
591   }
592 
593   protected Ordering<Hardware> hardwareSorter() {
594      Ordering<Hardware> hardwareOrdering = DEFAULT_SIZE_ORDERING;
595      if (!biggest)
596         hardwareOrdering = hardwareOrdering.reverse();
597      if (fastest)
598         hardwareOrdering = Ordering.compound(ImmutableList.of(BY_CORES_ORDERING, hardwareOrdering));
599      return hardwareOrdering;
600   }
601 
602   /**
603    * 
604    * @param hardware
605    * @param supportedImages
606    * @throws NoSuchElementException
607    *            if there's no image that matches the predicate
608    */
609   protected Image resolveImage(final Hardware hardware, Iterable<? extends Image> supportedImages) {
610      Predicate<Image> imagePredicate = new Predicate<Image>() {
611 
612         @Override
613         public boolean apply(Image arg0) {
614            return hardware.supportsImage().apply(arg0);
615         }
616 
617         @Override
618         public String toString() {
619            return "hardware(" + hardware + ").supportsImage()";
620         }
621      };
622      try {
623         Iterable<? extends Image> matchingImages = filter(supportedImages, imagePredicate);
624         if (logger.isTraceEnabled())
625            logger.trace("<<   matched images(%s)", matchingImages);
626         List<? extends Image> maxImages = Lists2.multiMax(DEFAULT_IMAGE_ORDERING, matchingImages);
627         if (logger.isTraceEnabled())
628            logger.trace("<<   best images(%s)", maxImages);
629         return maxImages.get(maxImages.size() - 1);
630      } catch (NoSuchElementException exception) {
631         throw new NoSuchElementException("image didn't match: " + toString() + "\n" + supportedImages);
632      }
633   }
634 
635   protected Set<? extends Image> getImages() {
636      return images.get();
637   }
638 
639   private Predicate<Image> buildImagePredicate() {
640      List<Predicate<Image>> predicates = newArrayList();
641      if (imageId != null) {
642         predicates.add(idPredicate);
643      } else {
644         if (location != null)
645            predicates.add(new Predicate<Image>() {
646 
647               @Override
648               public boolean apply(Image input) {
649                  return locationPredicate.apply(input);
650               }
651 
652               @Override
653               public String toString() {
654                  return "location(" + location + ")";
655               }
656            });
657 
658         final List<Predicate<OperatingSystem>> osPredicates = newArrayList();
659         if (osFamily != null)
660            osPredicates.add(osFamilyPredicate);
661         if (osName != null)
662            osPredicates.add(osNamePredicate);
663         if (osDescription != null)
664            osPredicates.add(osDescriptionPredicate);
665         if (osVersion != null)
666            osPredicates.add(osVersionPredicate);
667         if (os64Bit != null)
668            osPredicates.add(os64BitPredicate);
669         if (osArch != null)
670            osPredicates.add(osArchPredicate);
671         predicates.add(new Predicate<Image>() {
672 
673            @Override
674            public boolean apply(Image input) {
675               return Predicates.and(osPredicates).apply(input.getOperatingSystem());
676            }
677 
678            @Override
679            public String toString() {
680               return Predicates.and(osPredicates).toString();
681            }
682 
683         });
684         if (imageVersion != null)
685            predicates.add(imageVersionPredicate);
686         if (imageName != null)
687            predicates.add(imageNamePredicate);
688         if (imageDescription != null)
689            predicates.add(imageDescriptionPredicate);
690      }
691 
692      // looks verbose, but explicit <Image> type needed for this to compile
693      // properly
694      Predicate<Image> imagePredicate = predicates.size() == 1 ? Iterables.<Predicate<Image>> get(predicates, 0)
695            : Predicates.<Image> and(predicates);
696      return imagePredicate;
697   }
698 
699   /**
700    * {@inheritDoc}
701    */
702   @Override
703   public TemplateBuilder imageId(String imageId) {
704      this.imageId = imageId;
705      this.imageName = null;
706      this.imageDescription = null;
707      this.imageVersion = null;
708      this.osFamily = null;
709      this.osName = null;
710      this.osDescription = null;
711      this.osVersion = null;
712      this.os64Bit = null;
713      this.osArch = null;
714      return this;
715   }
716 
717   /**
718    * {@inheritDoc}
719    */
720   @Override
721   public TemplateBuilder imageNameMatches(String nameRegex) {
722      this.imageName = nameRegex;
723      return this;
724   }
725 
726   /**
727    * {@inheritDoc}
728    */
729   @Override
730   public TemplateBuilder imageDescriptionMatches(String descriptionRegex) {
731      this.imageDescription = descriptionRegex;
732      return this;
733   }
734 
735   /**
736    * {@inheritDoc}
737    */
738   @Override
739   public TemplateBuilder imageVersionMatches(String imageVersionRegex) {
740      this.imageVersion = imageVersionRegex;
741      return this;
742   }
743 
744   /**
745    * {@inheritDoc}
746    */
747   @Override
748   public TemplateBuilder osVersionMatches(String osVersionRegex) {
749      this.osVersion = osVersionRegex;
750      return this;
751   }
752 
753   /**
754    * {@inheritDoc}
755    */
756   @Override
757   public TemplateBuilder osArchMatches(String osArchitectureRegex) {
758      this.osArch = osArchitectureRegex;
759      return this;
760   }
761 
762   /**
763    * {@inheritDoc}
764    */
765   @Override
766   public TemplateBuilder minCores(double minCores) {
767      this.minCores = minCores;
768      return this;
769   }
770 
771   /**
772    * {@inheritDoc}
773    */
774   @Override
775   public TemplateBuilder minRam(int megabytes) {
776      this.minRam = megabytes;
777      return this;
778   }
779 
780   /**
781    * {@inheritDoc}
782    */
783   @Override
784   public TemplateBuilder osNameMatches(String osNameRegex) {
785      this.osName = osNameRegex;
786      return this;
787   }
788 
789   /**
790    * {@inheritDoc}
791    */
792   @Override
793   public TemplateBuilder osDescriptionMatches(String osDescriptionRegex) {
794      this.osDescription = osDescriptionRegex;
795      return this;
796   }
797 
798   /**
799    * {@inheritDoc}
800    */
801   @Override
802   public TemplateBuilder hardwareId(String hardwareId) {
803      this.hardwareId = hardwareId;
804      return this;
805   }
806 
807   /**
808    * {@inheritDoc}
809    */
810   @Override
811   public TemplateBuilder options(TemplateOptions options) {
812      this.options = optionsProvider.get();
813      checkNotNull(options, "options").copyTo(this.options);
814      return this;
815   }
816 
817   @VisibleForTesting
818   boolean nothingChangedExceptOptions() {
819      return osFamily == null && location == null && imageId == null && hardwareId == null && osName == null
820            && osDescription == null && imageVersion == null && osVersion == null && osArch == null && os64Bit == null
821            && imageName == null && imageDescription == null && minCores == 0 && minRam == 0 && !biggest && !fastest;
822   }
823 
824   /**
825    * {@inheritDoc}
826    */
827   @Override
828   public TemplateBuilder any() {
829      return defaultTemplateProvider.get();
830   }
831 
832   @Override
833   public String toString() {
834      return "[biggest=" + biggest + ", fastest=" + fastest + ", imageName=" + imageName + ", imageDescription="
835            + imageDescription + ", imageId=" + imageId + ", imageVersion=" + imageVersion + ", location=" + location
836            + ", minCores=" + minCores + ", minRam=" + minRam + ", osFamily=" + osFamily + ", osName=" + osName
837            + ", osDescription=" + osDescription + ", osVersion=" + osVersion + ", osArch=" + osArch + ", os64Bit="
838            + os64Bit + ", hardwareId=" + hardwareId + "]";
839   }
840 
841   @Override
842   public TemplateBuilder os64Bit(boolean is64Bit) {
843      this.os64Bit = is64Bit;
844      return this;
845   }
846 
847}

[all classes][org.jclouds.compute.domain.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov