View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.temperatureconverter.ejb;
18  
19  import java.text.DecimalFormat;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  /**
24   * A domain object that can store a temperature and scale. Additionally, it can parse a string to a temperature and scale.
25   * 
26   * @author Pete Muir
27   * @author Bruce Wolfe
28   * 
29   */
30  public class Temperature {
31  
32      public static final double ABSOLUTE_ZERO_C = -273.150;
33      public static final double ABSOLUTE_ZERO_F = -459.670;
34  
35      /*
36       * Create a regular expression to extract the temperature and scale (if passed).
37       */
38      private static Pattern PATTERN = Pattern.compile("^([-+]?\\d*\\.?\\d+)\\s*([cCfF]?)");
39  
40      /**
41       * Parse a string, with an optional scale suffix. If no scale suffix is on the string, the defaultScale will be used.
42       * 
43       * @param temperature the temperature to parse
44       * @param defaultScale the default scale to use
45       */
46      public static Temperature parse(String temperature, Scale defaultScale) {
47          double t;
48          Scale s;
49  
50          /**
51           * Extract temperature and scale
52           */
53          Matcher matcher = PATTERN.matcher(temperature);
54  
55          if (matcher.find()) {
56              // Extract the temperature
57              t = Double.parseDouble(matcher.group(1));
58  
59              // Use the scale included with the sourceTemperature OR the defaultScale provided.
60              if (!matcher.group(2).isEmpty()) {
61                  try {
62                      s = Scale.valueOfAbbreviation(matcher.group(2));
63                  } catch (IllegalArgumentException e) {
64                      throw new IllegalArgumentException("You must provide a valid temperature scale- 'C|F'");
65                  }
66              } else {
67                  s = defaultScale;
68              }
69          } else {
70              throw new IllegalArgumentException("You must provide a valid temperature to convert- 'XX.XXX'");
71          }
72  
73          return new Temperature(t, s);
74      }
75  
76      private final double temperature;
77      private final Scale scale;
78  
79      public Temperature(double temperature, Scale scale) {
80          this.temperature = temperature;
81          this.scale = scale;
82      }
83  
84      public Scale getScale() {
85          return scale;
86      }
87  
88      public double getTemperature() {
89          return temperature;
90      }
91  
92      @Override
93      public String toString() {
94          return new DecimalFormat("###.###").format(temperature) + " " + scale.getSymbol();
95      }
96  
97  }