Coverage Report - com.timjohnstondev.unitconverter.model.Property
 
Classes in this File Line Coverage Branch Coverage Complexity
Property
98%
51/52
81%
18/22
1.786
 
 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  517
 public class Property implements Comparable <Property>
 24  
 {
 25  
   private int order;
 26  
   private String name;
 27  247
   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  247
   {
 40  247
     name = newName;
 41  247
     hasFormula = isFormulaBased;
 42  247
     usesMoles = canUseMoles;
 43  247
   }
 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  517
     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  209
     order = newOrder;
 63  209
   }
 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  343
     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  6
     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  1971
     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  1989
     conversions.add(conversion);
 105  1989
   }
 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  18
     final List <String> symbols = new ArrayList <String>();
 115  18
     for (Conversion conversion : conversions)
 116  
     {
 117  66
       if (!symbols.contains(conversion.getFormattedUnitSymbol()))
 118  
       {
 119  60
         symbols.add(conversion.getFormattedUnitSymbol());
 120  
       }
 121  
     }
 122  18
     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  5
     String result = "";
 136  5
     if (hasFormula)
 137  
     {
 138  2
       result = getFormula(fromSymbol, toSymbol);
 139  
     }
 140  
     else
 141  
     {
 142  3
       final BigDecimal fromFactor = new BigDecimal(getConversion(fromSymbol));
 143  3
       final BigDecimal toFactor = new BigDecimal(getConversion(toSymbol));
 144  3
       final BigDecimal quotient = toFactor.divide(fromFactor, 30, RoundingMode.HALF_UP);
 145  3
       result = quotient.stripTrailingZeros().toPlainString();
 146  
     }
 147  5
     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  4
     String unitName = "";
 159  4
     for (Conversion conversion : conversions)
 160  
     {
 161  13
       if (unitSymbol.equals(conversion.getFormattedUnitSymbol()))
 162  
       {
 163  4
         unitName = conversion.getUnitName();
 164  
       }
 165  
     }
 166  4
     return unitName;
 167  
   }
 168  
 
 169  
   private String getFormula(final String fromSymbol, final String toSymbol)
 170  
   {
 171  2
     String formula = "";
 172  2
     for (Conversion conversion : conversions)
 173  
     {
 174  2
       final String fromUnitSymbol = conversion.getUnitSymbol();
 175  2
       final String toUnitSymbol = ((ConversionFormula) conversion).getToUnitSymbol();
 176  2
       if (fromSymbol.equals(fromUnitSymbol) && toSymbol.equals(toUnitSymbol))
 177  
       {
 178  2
         formula = conversion.getConversion();
 179  2
         break;
 180  
       }
 181  0
     }
 182  2
     return formula;
 183  
   }
 184  
 
 185  
   private String getConversion(final String symbol)
 186  
   {
 187  6
     String value = "";
 188  6
     for (Conversion conversion : conversions)
 189  
     {
 190  18
       if (symbol.equals(conversion.getFormattedUnitSymbol()))
 191  
       {
 192  6
         value = conversion.getConversion();
 193  6
         break;
 194  
       }
 195  
     }
 196  6
     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  21
     for (Conversion conversion : conversions)
 207  
     {
 208  181
       conversion.setUnitSeparator(symbol);
 209  
     }
 210  21
   }
 211  
 
 212  
   @Override
 213  
   public final int compareTo(final Property prop)
 214  
   {
 215  517
     return order - prop.getOrder();
 216  
   }
 217  
 }