/* Copyright 2000 Charles G. Wright * This software may be distributed under the terms of the * GNU General Public License. * * $Id: UnitConverter.java,v 1.1 2000-06-15 10:15:08-05 chuckles Exp chuckles $ */ import java.io.*; import java.util.*; /**
This class implements unit conversions between Metric and IP. * The root unit system for all calculations is always metric. * Input and output values can be metric or I-P (inch-pound). * The class contains a state variable, set using the static setUnitSystem() * method, which sets the unit system that will be used for all conversions. *
Methods that do input or output should create an instance of this * class for each type of unit with which they deal. They should then * run input data through getMetricValue() to get the value that will * be used for all calculations. Output data should be run through the * getConvertedValue() method to get the value that will be displayed. * If this is done, unit coversion is otherwise transparent to other * elements of the program. *
Note that if the unit system is set for metric, the conversion * methods return input data unchanged. *
The class provides methods for retrieving a string for the * type of unit (e.g. "Power") and a label for the unit (e.g. "Watts"). * The label is appropriate for the unit system currently selected. *
The class extends the Observable class, giving a convenient means * for updating objects that utilize it for conversions, when the unit * system changes. */ public class UnitConverter extends Observable{ // Class Variables /** Of choices METRIC or IP, indicates that metric units are used. */ public static final int METRIC = 0; /** Of choices METRIC or IP, indicates that English units are used. */ public static final int IP = 1; /** Indicates that no conversion will be made. */ public static final int NOTHING = 0; /** Indicates that the type of unit is Temperature. */ public static final int TEMPERATURE = 1; /** Indicates that the type of unit is power per unit area. */ public static final int POWER_PER_AREA = 2; /** Indicates that the type of unit is power. */ public static final int POWER = 3; /** Indicates that the type of unit is energy. */ public static final int ENERGY = 4; /** Indicates that the type of unit is R value. */ public static final int RVALUE = 5; /** Indicates that the type of unit is U value. */ public static final int UVALUE = 6; /** Indicates that the type of unit is area. */ public static final int AREA = 7; /** Indicates that the type of unit is velocity. */ public static final int VELOCITY_MPH = 8; /** Indicates that the type of unit is velocity. */ public static final int VELOCITY_FT_SEC = 9; /** Indicates that the type of unit is length. */ public static final int LENGTH = 10; // List of unit names private static final String[] unit_names = {"", "Temperature", "Radiation", "Power", "Energy", "R Value", "U value", "Area", "Velocity", "Velocity", "Length" }; // List of the available labels for the different types of units (Metric) private static final String[] metric_unit_labels = {"", "C", "Watts/m**2", "Watts", "Joules", "m**2 C/Watt", "Watts/m**2 C", "m**2", "m/sec", "m/sec", "m" }; // List of the available labels for the different types of units (IP) private static final String[] ip_unit_labels = {"", "F", "BTUH/ft**2", "BTUH", "BTU", "ft**2 F/BTUH", "BTUH/ft**2 F", "ft**2", "mph", "ft/sec", "ft" }; private static String[] unit_labels = metric_unit_labels; //---------------------------------------------------- // Conversion factors for metric to english private static final double[] conversion_factors = {1.0, 1.8, 0.317211, 3.41443, 0.000948451, 5.674458, 0.176228, 10.76391 }; private static final double[] conversion_offsets = {0.0, 32.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; /** METRIC or IP, applying to all instances. */ private static int currentSystem = METRIC; private static Vector instances = new Vector(); //----------------- Instance variables ---------------------- private int unit_type; //---- constructor ------ /** Creates a UnitConverter object for conversion of unitType units. * for unitType, use one of the static class variables TEMPERATURE, * POWER, POWER_PER_AREA, etc.*/ public UnitConverter(int unitType){ unit_type = unitType; // add this element to the list of UnitConverters for later updates. instances.addElement(this); } //----class methods----- /** Sets the system of units to METRIC or IP. */ static public void setUnitSystem(int unitSystem){ UnitConverter uc; // Only do something if something changed. if (currentSystem != unitSystem){ currentSystem = unitSystem; if(unitSystem == METRIC){ unit_labels = metric_unit_labels; } else { unit_labels = ip_unit_labels; } // notify all of the observers of the unit change. // Not sure I like this... System.out.println("Notifying everyone of unit change"); for (int i = 0; i < instances.size(); i++){ uc = (UnitConverter)instances.elementAt(i); System.out.println("..."); uc.setChanged(); uc.notifyObservers(); } } } static public int getUnitSystem(){ return(currentSystem); } //---- instance methods /** Returns the type of unit for this converter. */ public int getUnitType(){ return(unit_type); } /** Returns the name of the units for which this object converts * (e.g. "Power").*/ public String getName(){ return(unit_names[unit_type]); } /** Returns the current label associated with these units (e.g. "Watts").*/ public String getLabel(){ return unit_labels[unit_type]; } public static double getConvertedValue(double metric_value, int unit_type){ if(currentSystem == METRIC){ return(metric_value); }else{ return((metric_value * conversion_factors[unit_type]) + conversion_offsets[unit_type]); } } /** Converts the passed value, assumed to be in metric, * into the current unit system. */ public double getConvertedValue(double metric_value){ return(getConvertedValue(metric_value, unit_type)); } /** Returns conversion factor (Metric to English). */ public double getConversionFactor(){ return(conversion_factors[unit_type]); } /** Converts the passed value, assumed to be in the * current unit system, to metric and returns the value. */ public double getMetricValue(double value){ if(currentSystem == METRIC){ return(value); }else{ return((value - conversion_offsets[unit_type]) / conversion_factors[unit_type]); } } }