View Javadoc

1   /**
2    * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
3    * file except in compliance with the License. You may obtain a copy of the License at
4    * 
5    * http://www.apache.org/licenses/LICENSE-2.0
6    * 
7    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9    * specific language governing permissions and limitations under the License.
10   */
11  
12  package com.timjohnstondev.unitconverter.model;
13  
14  import java.math.BigDecimal;
15  import java.math.RoundingMode;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  /**
20   * A {@code Property}, such as temperature, pressure or length, has units and conversion factors or formulas to convert
21   * between the different units.
22   */
23  public class Property implements Comparable <Property>
24  {
25    private int order;
26    private String name;
27    private List <Conversion> conversions = new ArrayList <Conversion>();
28    private boolean hasFormula;
29    private boolean usesMoles;
30  
31    /**
32     * Constructs a {@code Property} with the given name and specifies if it uses formulas or factors for unit conversion.
33     * 
34     * @param newName the descriptive name of this {@code Property}
35     * @param isFormulaBased determines if this {@code Property} will use formulas or factors for unit conversion
36     * @param canUseMoles determines if this {@code Property} can do mole to mass and mass to mole unit conversions
37     */
38    public Property(final String newName, final boolean isFormulaBased, final boolean canUseMoles)
39    {
40      name = newName;
41      hasFormula = isFormulaBased;
42      usesMoles = canUseMoles;
43    }
44  
45    /**
46     * Returns this {@code Property}'s sort order
47     * 
48     * @return the sort order of this property
49     */
50    public final int getOrder()
51    {
52      return order;
53    }
54  
55    /**
56     * Sets this {@code Property}'s sort order
57     * 
58     * @param newOrder the new sort order of this property
59     */
60    public final void setOrder(final int newOrder)
61    {
62      order = newOrder;
63    }
64  
65    /**
66     * Returns this {@code Property}'s name
67     * 
68     * @return the name of this property
69     */
70    public final String getName()
71    {
72      return name;
73    }
74  
75    /**
76     * Returns {@code true} if this {@code Property} can do mole to mass and mass to mole unit conversions, else {@code
77     * false}
78     * 
79     * @return {@code true} if this {@code Property} can do mole to mass and mass to mole unit conversions, else {@code
80     *         false}
81     */
82    public final boolean usesMoles()
83    {
84      return usesMoles;
85    }
86  
87    /**
88     * Returns {@code true} if this {@code Property} uses formulas for unit conversion, else {@code false}
89     * 
90     * @return {@code true} if this {@code Property} uses formulas for unit conversion, else {@code false}
91     */
92    public final boolean hasFormula()
93    {
94      return hasFormula;
95    }
96  
97    /**
98     * Adds a {@link Conversion} which is a formula or factor that is used to perform unit conversions.
99     * 
100    * @param conversion factor or formula to perform the conversion
101    */
102   public final void addConversion(final Conversion conversion)
103   {
104     conversions.add(conversion);
105   }
106 
107   /**
108    * Returns a list of the symbols for this {@code Property}.
109    * 
110    * @return a list of the symbols
111    */
112   final List <String> getSymbols()
113   {
114     final List <String> symbols = new ArrayList <String>();
115     for (Conversion conversion : conversions)
116     {
117       if (!symbols.contains(conversion.getFormattedUnitSymbol()))
118       {
119         symbols.add(conversion.getFormattedUnitSymbol());
120       }
121     }
122     return symbols;
123   }
124 
125   /**
126    * Returns the {@link Conversion} factor or formula for this {@code Property} for going from one set of units to
127    * another.
128    * 
129    * @param fromSymbol the original units
130    * @param toSymbol the desired units
131    * @return the {@code Conversion} factor or formula to perform the conversion
132    */
133   final String getConversion(final String fromSymbol, final String toSymbol)
134   {
135     String result = "";
136     if (hasFormula)
137     {
138       result = getFormula(fromSymbol, toSymbol);
139     }
140     else
141     {
142       final BigDecimal fromFactor = new BigDecimal(getConversion(fromSymbol));
143       final BigDecimal toFactor = new BigDecimal(getConversion(toSymbol));
144       final BigDecimal quotient = toFactor.divide(fromFactor, 30, RoundingMode.HALF_UP);
145       result = quotient.stripTrailingZeros().toPlainString();
146     }
147     return result;
148   }
149 
150   /**
151    * Returns this symbol's name in unabbreviated words for this {@code Property}.
152    * 
153    * @param unitSymbol the symbol that represents the returned name
154    * @return the name for this symbol
155    */
156   final String getUnitName(final String unitSymbol)
157   {
158     String unitName = "";
159     for (Conversion conversion : conversions)
160     {
161       if (unitSymbol.equals(conversion.getFormattedUnitSymbol()))
162       {
163         unitName = conversion.getUnitName();
164       }
165     }
166     return unitName;
167   }
168 
169   private String getFormula(final String fromSymbol, final String toSymbol)
170   {
171     String formula = "";
172     for (Conversion conversion : conversions)
173     {
174       final String fromUnitSymbol = conversion.getUnitSymbol();
175       final String toUnitSymbol = ((ConversionFormula) conversion).getToUnitSymbol();
176       if (fromSymbol.equals(fromUnitSymbol) && toSymbol.equals(toUnitSymbol))
177       {
178         formula = conversion.getConversion();
179         break;
180       }
181     }
182     return formula;
183   }
184 
185   private String getConversion(final String symbol)
186   {
187     String value = "";
188     for (Conversion conversion : conversions)
189     {
190       if (symbol.equals(conversion.getFormattedUnitSymbol()))
191       {
192         value = conversion.getConversion();
193         break;
194       }
195     }
196     return value;
197   }
198 
199   /**
200    * Updates the unit separator symbol
201    * 
202    * @param symbol the new unit separator symbol
203    */
204   public final void setUnitSeparator(final String symbol)
205   {
206     for (Conversion conversion : conversions)
207     {
208       conversion.setUnitSeparator(symbol);
209     }
210   }
211 
212   @Override
213   public final int compareTo(final Property prop)
214   {
215     return order - prop.getOrder();
216   }
217 }