EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.softlayer.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ProductItemsToHardware.java]

nameclass, %method, %block, %line, %
ProductItemsToHardware.java100% (3/3)100% (8/8)99%  (192/193)100% (25/25)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ProductItemsToHardware100% (1/1)100% (4/4)99%  (112/113)100% (16/16)
apply (Iterable): Hardware 100% (1/1)99%  (86/87)100% (9/9)
ProductItemsToHardware (): void 100% (1/1)100% (7/7)100% (2/2)
ProductItemsToHardware (Pattern, Pattern): void 100% (1/1)100% (15/15)100% (4/4)
hardwareId (): Function 100% (1/1)100% (4/4)100% (1/1)
     
class ProductItemsToHardware$1100% (1/1)100% (2/2)100% (42/42)100% (3/3)
ProductItemsToHardware$1 (ProductItemsToHardware): void 100% (1/1)100% (6/6)100% (1/1)
apply (ProductItem): Volume 100% (1/1)100% (36/36)100% (2/2)
     
class ProductItemsToHardware$2100% (1/1)100% (2/2)100% (38/38)100% (7/7)
ProductItemsToHardware$2 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (List): String 100% (1/1)100% (35/35)100% (6/6)

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.softlayer.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.collect.Iterables.filter;
23import static com.google.common.collect.Iterables.get;
24import static com.google.common.collect.Iterables.getOnlyElement;
25import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCode;
26import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCodeMatches;
27import static org.jclouds.softlayer.predicates.ProductItemPredicates.matches;
28 
29import java.util.List;
30import java.util.regex.Matcher;
31import java.util.regex.Pattern;
32 
33import javax.inject.Inject;
34import javax.inject.Singleton;
35 
36import org.jclouds.compute.domain.Hardware;
37import org.jclouds.compute.domain.HardwareBuilder;
38import org.jclouds.compute.domain.Processor;
39import org.jclouds.compute.domain.Volume;
40import org.jclouds.compute.domain.internal.VolumeImpl;
41import org.jclouds.softlayer.domain.ProductItem;
42import org.jclouds.softlayer.domain.ProductItemPrice;
43 
44import com.google.common.base.Function;
45import com.google.common.collect.ImmutableList;
46import com.google.common.collect.Iterables;
47 
48/**
49 * Converts a set of ProductItems to Hardware. All cores have a speed of 2.0Ghz The Hardware Id will
50 * be a comma separated list containing the price ids: cpus,ram,volume
51 * 
52 * @author Jason King
53 */
54@Singleton
55public class ProductItemsToHardware implements Function<Iterable<ProductItem>, Hardware> {
56 
57   private static final String GUEST_DISK_CATEGORY_REGEX =  "guest_disk[0-9]";
58   private static final String FIRST_GUEST_DISK = "guest_disk0";
59   private static final String STORAGE_AREA_NETWORK = "SAN";
60 
61   private static final String RAM_CATEGORY = "ram";
62 
63   private static final String CPU_DESCRIPTION_REGEX = "(Private )?[0-9]+ x ([.0-9]+) GHz Core[s]?";
64   private static final double DEFAULT_CORE_SPEED = 2.0;
65 
66   private final Pattern cpuDescriptionRegex;
67   private final Pattern diskCategoryRegex;
68 
69   @Inject
70   public ProductItemsToHardware() {
71      this(Pattern.compile(CPU_DESCRIPTION_REGEX), Pattern.compile(GUEST_DISK_CATEGORY_REGEX));
72   }
73 
74   public ProductItemsToHardware(Pattern cpuDescriptionRegex, Pattern diskCategoryRegex) {
75      this.cpuDescriptionRegex = checkNotNull(cpuDescriptionRegex, "cpuDescriptionRegex");
76      this.diskCategoryRegex = checkNotNull(diskCategoryRegex, "diskCategoryRegex");
77   }
78 
79   @Override
80   public Hardware apply(Iterable<ProductItem> items) {
81 
82      ProductItem coresItem = getOnlyElement(filter(items, matches(cpuDescriptionRegex)));
83      ProductItem ramItem = getOnlyElement(filter(items, categoryCode(RAM_CATEGORY)));
84      ProductItem volumeItem = get(filter(items, categoryCode(FIRST_GUEST_DISK)), 0);
85 
86      String hardwareId = hardwareId().apply(ImmutableList.of(coresItem, ramItem, volumeItem));
87      double cores = ProductItems.capacity().apply(coresItem).doubleValue();
88      Matcher cpuMatcher = cpuDescriptionRegex.matcher(coresItem.getDescription());
89      double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : DEFAULT_CORE_SPEED;
90      int ram = ProductItems.capacity().apply(ramItem).intValue();
91 
92      return new HardwareBuilder().ids(hardwareId).processors(ImmutableList.of(new Processor(cores, coreSpeed))).ram(
93               ram).volumes(
94                  Iterables.transform(filter(items, categoryCodeMatches(diskCategoryRegex)),
95                        new Function<ProductItem, Volume>() {
96                           @Override
97                           public Volume apply(ProductItem item) {
98                              float volumeSize = ProductItems.capacity().apply(item);
99                              return new VolumeImpl(
100                                       item.getId() + "",
101                                       item.getDescription().indexOf(STORAGE_AREA_NETWORK) != -1 ? Volume.Type.SAN : Volume.Type.LOCAL,
102                                       volumeSize, null, categoryCode(FIRST_GUEST_DISK).apply(item), false);
103                           }
104                        })).build();
105   }
106 
107   /**
108    * Generates a hardwareId based on the priceId's of the items in the list
109    *
110    * @return comma separated list of priceid's
111    */
112   public static Function<List<ProductItem>, String> hardwareId() {
113      return new Function<List<ProductItem>, String>() {
114         @Override
115         public String apply(List<ProductItem> productItems) {
116            StringBuilder builder = new StringBuilder();
117            for (ProductItem item : productItems) {
118               ProductItemPrice price = ProductItems.price().apply(item);
119               builder.append(price.getId()).append(",");
120            }
121            return builder.toString().substring(0, builder.lastIndexOf(","));
122         }
123      };
124   }
125}

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