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

COVERAGE SUMMARY FOR SOURCE FILE [HardwarePropertyHandler.java]

nameclass, %method, %block, %line, %
HardwarePropertyHandler.java100% (2/2)100% (7/7)83%  (290/348)91%  (35.3/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class HardwarePropertyHandler100% (1/1)100% (6/6)83%  (267/322)91%  (35.4/39)
getResult (): HardwareProperty 100% (1/1)69%  (125/180)73%  (9.4/13)
<static initializer> 100% (1/1)100% (7/7)100% (2/2)
HardwarePropertyHandler (): void 100% (1/1)100% (6/6)100% (2/2)
parseNumberOrNull (String): Number 100% (1/1)100% (22/22)100% (5/5)
startElement (String, String, String, Attributes): void 100% (1/1)100% (98/98)100% (15/15)
stringOrNumber (String): Object 100% (1/1)100% (9/9)100% (2/2)
     
class HardwarePropertyHandler$1100% (1/1)100% (1/1)88%  (23/26)88%  (0.9/1)
<static initializer> 100% (1/1)88%  (23/26)88%  (0.9/1)

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.deltacloud.xml;
20 
21import java.net.URI;
22import java.util.Map;
23import java.util.Set;
24import java.util.regex.Pattern;
25 
26import org.jclouds.javax.annotation.Nullable;
27 
28import org.jclouds.deltacloud.domain.EnumHardwareProperty;
29import org.jclouds.deltacloud.domain.FixedHardwareProperty;
30import org.jclouds.deltacloud.domain.HardwareParameter;
31import org.jclouds.deltacloud.domain.HardwareProperty;
32import org.jclouds.deltacloud.domain.RangeHardwareProperty;
33import org.jclouds.deltacloud.domain.HardwareProperty.Kind;
34import org.jclouds.http.functions.ParseSax;
35import org.jclouds.util.SaxUtils;
36import org.xml.sax.Attributes;
37import org.xml.sax.SAXException;
38 
39import com.google.common.collect.Sets;
40 
41/**
42 * @author Adrian Cole
43 */
44public class HardwarePropertyHandler extends ParseSax.HandlerWithResult<HardwareProperty> {
45   private Kind kind;
46   private String name;
47   private String unit;
48   private Object value;
49   private HardwareParameter param;
50   private Set<Object> availableValues = Sets.newLinkedHashSet();
51   private Number first;
52   private Number last;
53 
54   /**
55    * resets state of the handler when called.
56    * 
57    * @return property or null
58    */
59   public HardwareProperty getResult() {
60      try {
61         switch (kind) {
62         case FIXED:
63            return new FixedHardwareProperty(name, unit, value);
64         case ENUM:
65            return new EnumHardwareProperty(name, unit, value, param, availableValues);
66         case RANGE:
67            return new RangeHardwareProperty(name, unit, (Number) value, param, first, last);
68         default:
69            return null;
70         }
71      } finally {
72         this.kind = null;
73         this.name = null;
74         this.unit = null;
75         this.value = null;
76         this.param = null;
77         this.availableValues = Sets.newLinkedHashSet();
78         this.first = null;
79         this.last = null;
80      }
81   }
82 
83   private static final Pattern LONG = Pattern.compile("^[0-9]+$");
84   private static final Pattern DOUBLE = Pattern.compile("^[0-9]+\\.[0-9]+$");
85 
86   public static @Nullable
87   Number parseNumberOrNull(String in) {
88      if (DOUBLE.matcher(in).matches())
89         return new Double(in);
90      else if (LONG.matcher(in).matches())
91         return new Long(in);
92      return null;
93   }
94 
95   @Override
96   public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
97      Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs);
98      if (qName.equals("property")) {
99         this.kind = Kind.fromValue(attributes.get("kind"));
100         this.name = attributes.get("name");
101         this.unit = attributes.get("unit");
102         if (attributes.containsKey("value")) {
103            this.value = stringOrNumber(attributes.get("value"));
104         }
105      } else if (qName.equals("param")) {
106         this.param = new HardwareParameter(URI.create(attributes.get("href")), attributes.get("method"),
107               attributes.get("name"), attributes.get("operation"));
108      } else if (qName.equals("range")) {
109         this.first = parseNumberOrNull(attributes.get("first"));
110         this.last = parseNumberOrNull(attributes.get("last"));
111      } else if (qName.equals("entry")) {
112         this.availableValues.add(stringOrNumber(attributes.get("value")));
113      }
114   }
115 
116   public static Object stringOrNumber(String in) {
117      Number number = parseNumberOrNull(in);
118      return number != null ? number : in;
119   }
120}

[all classes][org.jclouds.deltacloud.xml]
EMMA 2.0.5312 (C) Vladimir Roubtsov