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

COVERAGE SUMMARY FOR SOURCE FILE [ProductItemToImage.java]

nameclass, %method, %block, %line, %
ProductItemToImage.java100% (5/5)100% (18/18)100% (269/269)100% (41/41)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ProductItemToImage100% (1/1)100% (10/10)100% (91/91)100% (14/14)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ProductItemToImage (): void 100% (1/1)100% (6/6)100% (2/2)
access$000 (String, String): String 100% (1/1)100% (4/4)100% (1/1)
access$100 (): Pattern 100% (1/1)100% (2/2)100% (1/1)
apply (ProductItem): Image 100% (1/1)100% (47/47)100% (5/5)
imageId (): Function 100% (1/1)100% (4/4)100% (1/1)
osBits (): Function 100% (1/1)100% (4/4)100% (1/1)
osFamily (): Function 100% (1/1)100% (4/4)100% (1/1)
osVersion (): Function 100% (1/1)100% (4/4)100% (1/1)
parseVersion (String, String): String 100% (1/1)100% (12/12)100% (2/2)
     
class ProductItemToImage$1100% (1/1)100% (2/2)100% (48/48)100% (10/10)
ProductItemToImage$1 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (ProductItem): OsFamily 100% (1/1)100% (45/45)100% (9/9)
     
class ProductItemToImage$2100% (1/1)100% (2/2)100% (75/75)100% (11/11)
ProductItemToImage$2 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (ProductItem): String 100% (1/1)100% (72/72)100% (10/10)
     
class ProductItemToImage$3100% (1/1)100% (2/2)100% (33/33)100% (6/6)
ProductItemToImage$3 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (ProductItem): Integer 100% (1/1)100% (30/30)100% (5/5)
     
class ProductItemToImage$4100% (1/1)100% (2/2)100% (22/22)100% (4/4)
ProductItemToImage$4 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (ProductItem): String 100% (1/1)100% (19/19)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.softlayer.compute.functions;
20 
21import com.google.common.base.Function;
22import org.jclouds.compute.domain.Image;
23import org.jclouds.compute.domain.ImageBuilder;
24import org.jclouds.compute.domain.OperatingSystem;
25import org.jclouds.compute.domain.OsFamily;
26import org.jclouds.compute.reference.ComputeServiceConstants;
27import org.jclouds.logging.Logger;
28import org.jclouds.softlayer.domain.ProductItem;
29import org.jclouds.softlayer.domain.ProductItemPrice;
30 
31import javax.annotation.Resource;
32import javax.inject.Named;
33import javax.inject.Singleton;
34import java.util.NoSuchElementException;
35import java.util.regex.Matcher;
36import java.util.regex.Pattern;
37 
38import static com.google.common.base.Preconditions.checkNotNull;
39 
40/**
41 * @author Jason King
42 */
43@Singleton
44public class ProductItemToImage implements Function<ProductItem, Image> {
45 
46   /**  Pattern to capture the number of bits e.g. "a (32 bit) os" */
47   private static final Pattern OS_BITS_PATTERN = Pattern.compile(".*\\((\\d+) ?bit\\).*");
48 
49   private static final String CENTOS  = "CentOS";
50   private static final String DEBIAN  = "Debian GNU/Linux";
51   private static final String FEDORA  = "Fedora Release";
52   private static final String RHEL    = "Red Hat Enterprise Linux";
53   private static final String UBUNTU  = "Ubuntu Linux";
54   private static final String WINDOWS = "Windows Server";
55 
56   @Resource
57   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
58   protected Logger logger = Logger.NULL;
59 
60   @Override
61   public Image apply(ProductItem productItem) {
62      checkNotNull(productItem,"productItem");
63 
64      OsFamily family = osFamily().apply(productItem);
65      Integer bits = osBits().apply(productItem);
66      OperatingSystem os = OperatingSystem.builder()
67                              .description(productItem.getDescription())
68                              .family(family)
69                              .version(osVersion().apply(productItem))
70                              .is64Bit(bits.equals(64))
71                              .build();
72 
73      return new ImageBuilder()
74            .ids(imageId().apply(productItem))
75            .description(productItem.getDescription())
76            .operatingSystem(os)
77            .build();
78   }
79 
80   /**
81    * Parses the item description to determine the OSFamily
82    * @return the @see OsFamily or OsFamily.UNRECOGNIZED
83    */
84    public static Function<ProductItem,OsFamily> osFamily() {
85       return new Function<ProductItem,OsFamily>() {
86            @Override
87            public OsFamily apply(ProductItem productItem) {
88               checkNotNull(productItem,"productItem");
89 
90               final String description = productItem.getDescription();
91               if(description.startsWith(CENTOS)) return OsFamily.CENTOS;
92               else if(description.startsWith(DEBIAN)) return OsFamily.DEBIAN;
93               else if(description.startsWith(FEDORA)) return OsFamily.FEDORA;
94               else if(description.startsWith(RHEL)) return OsFamily.RHEL;
95               else if(description.startsWith(UBUNTU)) return OsFamily.UBUNTU;
96               else if(description.startsWith(WINDOWS)) return OsFamily.WINDOWS;
97               return OsFamily.UNRECOGNIZED;
98            }
99        };
100    }
101 
102   /**
103    * Parses the item description to determine the os version
104    * @return the version
105    * @throws java.util.NoSuchElementException if the version cannot be determined
106    */
107    public static Function<ProductItem,String> osVersion() {
108       return new Function<ProductItem,String>() {
109            @Override
110            public String apply(ProductItem productItem) {
111               checkNotNull(productItem,"productItem");
112 
113               final String description = productItem.getDescription();
114               OsFamily family = osFamily().apply(productItem);
115               if (family.equals(OsFamily.CENTOS)) return parseVersion(description, CENTOS);
116               else if(family.equals(OsFamily.DEBIAN)) return parseVersion(description, DEBIAN);
117               else if(family.equals(OsFamily.FEDORA)) return parseVersion(description, FEDORA);
118               else if(family.equals(OsFamily.RHEL)) return parseVersion(description, RHEL);
119               else if(family.equals(OsFamily.UBUNTU)) return parseVersion(description, UBUNTU);
120               else if(family.equals(OsFamily.WINDOWS)) return parseVersion(description, WINDOWS);
121               else throw new NoSuchElementException("No os version for item:"+productItem);
122            }
123        };
124    }
125 
126    private static String parseVersion(String description, String os) {
127       String noOsName = description.replaceFirst(os,"").trim();
128       return noOsName.split(" ")[0];
129    }
130 
131   /**
132    * Parses the item description to determine the number of OS bits
133    * Expects the number to be in parenthesis and to contain the word "bit".
134    * The following return 64: "A (64 bit) OS", "A (64bit) OS"
135    * @return the number of bits
136    * @throws java.util.NoSuchElementException if the number of bits cannot be determined
137    */
138    public static Function<ProductItem,Integer> osBits() {
139       return new Function<ProductItem,Integer>() {
140            @Override
141            public Integer apply(ProductItem productItem) {
142               checkNotNull(productItem,"productItem");
143 
144               Matcher m = OS_BITS_PATTERN.matcher(productItem.getDescription());
145               if (m.matches()) {
146                  return Integer.parseInt(m.group(1));
147               } else {
148                  throw new NoSuchElementException("Cannot determine os-bits for item:"+productItem);
149               }
150            }
151       };
152    }
153 
154  /**
155   * Generates an id for an Image.
156   * @return the generated id
157   */
158   public static Function<ProductItem,String> imageId() {
159      return new Function<ProductItem,String>() {
160         @Override
161         public String apply(ProductItem productItem) {
162            checkNotNull(productItem,"productItem");
163            ProductItemPrice price = ProductItems.price().apply(productItem);
164            return ""+price.getId();
165         }
166      };
167   }
168 
169}

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