com.edsdev.jconvert.domain
Class Conversion

java.lang.Object
  extended bycom.edsdev.jconvert.domain.Conversion
All Implemented Interfaces:
java.lang.Comparable
Direct Known Subclasses:
DecimalConversion, FractionalConversion

public abstract class Conversion
extends java.lang.Object
implements java.lang.Comparable

This is the abstract class that represents a conversion itself.

Author:
Ed Sarrazin Created on Jul 14, 2007 10:10:14 AM

Constructor Summary
protected Conversion()
           
protected Conversion(java.lang.String fromUnit, java.lang.String fromUnitAbbr, java.lang.String toUnit, java.lang.String toUnitAbbr, java.lang.String fromToFactor, double fromToOffset)
           
 
Method Summary
 int compareTo(java.lang.Object obj)
           
 java.lang.String convertFraction(java.lang.String value, java.lang.String pFromUnit, java.lang.String pToUnit)
          Converts a fraction.
abstract  java.lang.String convertValue(java.math.BigInteger numerator, java.math.BigInteger denominator, java.lang.String pFromUnit)
          Converts a value represented by a numerator and denominator, returning a String representation of the result
abstract  double convertValue(double value, java.lang.String pFromUnit)
          Converts a value for you are returns the answer.
 java.lang.Double convertValue(double value, java.lang.String pFromUnit, java.lang.String pTtoUnit)
          Converts from one unit to another.
 java.lang.Double convertValue(java.lang.String value, java.lang.String pFromUnit, java.lang.String pTtoUnit)
          Converts from one unit to another.
static Conversion createEmptyInstance(java.lang.String fromUnit, java.lang.String fromUnitAbbr, java.lang.String toUnit, java.lang.String toUnitAbbr)
          Just a helper method to get a conversion that is not functional, but can be used to compare This is primarily to speed up generating conversions to determine if one already exists.
static Conversion createInstance(java.lang.String fromUnit, java.lang.String fromUnitAbbr, java.lang.String toUnit, java.lang.String toUnitAbbr, java.lang.String fromToFactor, double fromToOffset)
          This static mehtod acts as the factory for Conversion objects.
abstract  java.lang.String divide(Conversion byConversion)
          Divide this conversion by the passed in conversion
 boolean equals(java.lang.Object obj)
           
protected  java.math.BigInteger getBigInteger(java.lang.String value)
          Simply extracts BigInteger out of string, but also handles it when there is decimals - should use isWhole number to make sure, because this will throw an exception if not perfect
 java.lang.String getConversionPartner(java.lang.String unit)
          Returns the "Partner" conversion unit.
 java.lang.String getConversionPartnerAbbrev(java.lang.String unit)
          Returns the "Partner" conversion unit abbreviation.
abstract  java.math.BigInteger getFromToBottomFactor()
           
abstract  double getFromToFactor()
           
abstract  java.lang.String getFromToFactorString()
           
 double getFromToOffset()
           
abstract  java.math.BigInteger getFromToTopFactor()
           
abstract  java.math.BigInteger getFromToWholeNumber()
           
 java.lang.String getFromUnit()
           
 java.lang.String getFromUnitAbbr()
           
 int getGenerationAge()
          This represents the general age for a conversion.
protected  double getRoundedResult(double result)
           
 java.lang.String getToUnit()
           
 java.lang.String getToUnitAbbr()
           
 int hashCode()
           
 boolean isFraction(java.lang.String value)
           
protected static boolean isWholeNumber(java.lang.String value)
          This method really tells you if the number can be represented as a long.
abstract  java.lang.String multiply(Conversion byConversion)
          Multiply this conversion by the passed in conversion
abstract  void setFromToBottomFactor(java.math.BigInteger fromToBottomFactor)
           
abstract  void setFromToFactor(double fromToFactor)
           
abstract  void setFromToFactorString(java.lang.String factor)
           
 void setFromToOffset(double fromToOffset)
           
abstract  void setFromToTopFactor(java.math.BigInteger fromToTopFactor)
           
abstract  void setFromToWholeNumber(java.math.BigInteger fromToWholeNumber)
           
 void setFromUnit(java.lang.String fromUnit)
           
 void setFromUnitAbbr(java.lang.String fromUnitAbbr)
           
 void setGenerationAge(int generationAge)
           
 void setToUnit(java.lang.String toUnit)
           
 void setToUnitAbbr(java.lang.String toUnitAbbr)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Conversion

protected Conversion()

Conversion

protected Conversion(java.lang.String fromUnit,
                     java.lang.String fromUnitAbbr,
                     java.lang.String toUnit,
                     java.lang.String toUnitAbbr,
                     java.lang.String fromToFactor,
                     double fromToOffset)
Method Detail

createInstance

public static Conversion createInstance(java.lang.String fromUnit,
                                        java.lang.String fromUnitAbbr,
                                        java.lang.String toUnit,
                                        java.lang.String toUnitAbbr,
                                        java.lang.String fromToFactor,
                                        double fromToOffset)
This static mehtod acts as the factory for Conversion objects. From this method you will receive a class that implements the Conversion interface in its own way. The parameters that are passed in are the basic properties that are needed to construct this class. They are one for one with the information that is stored in the dat files.

Parameters:
fromUnit - String:Unit that will be converted from.
fromUnitAbbr - String:Abbreviation of the unit that will be converted from.
toUnit - String:Unit that will be converted to.
toUnitAbbr - String:Abbreviation of the unit that will be converted to.
fromToFactor - String:Factor used to convert from the designated unit to the designated unit. This can either be a decimal or fractional representation - assumes that the fraction uses "/" symbol i.e.1/3 not 1\3.
fromToOffset - double value representing the offset to be applied in the conversion. Note that the factor will be applied first and the offset later.
Returns:
Conversion implementation representing the information that was passed in.

createEmptyInstance

public static Conversion createEmptyInstance(java.lang.String fromUnit,
                                             java.lang.String fromUnitAbbr,
                                             java.lang.String toUnit,
                                             java.lang.String toUnitAbbr)
Just a helper method to get a conversion that is not functional, but can be used to compare This is primarily to speed up generating conversions to determine if one already exists. Will return a conversion item with the appropriate from and to units, but the conversion factor and offset are bogus.

Parameters:
fromUnit -
fromUnitAbbr -
toUnit -
toUnitAbbr -
Returns:
Conversion instance that does not have valid conversion information.

isWholeNumber

protected static boolean isWholeNumber(java.lang.String value)
This method really tells you if the number can be represented as a long. The criteria are that it cannot be so large that a long cannot handle it AND it cannot have decimals unless there are only zeros after the decimal.

Parameters:
value - String value representation of the number
Returns:
true if is whole number(long) or false otherwise.

getBigInteger

protected java.math.BigInteger getBigInteger(java.lang.String value)
Simply extracts BigInteger out of string, but also handles it when there is decimals - should use isWhole number to make sure, because this will throw an exception if not perfect

Parameters:
value - String representation of the long
Returns:
BigInteger representation of the value passed in.

hashCode

public int hashCode()

toString

public java.lang.String toString()

equals

public boolean equals(java.lang.Object obj)

convertValue

public abstract double convertValue(double value,
                                    java.lang.String pFromUnit)
Converts a value for you are returns the answer.

Parameters:
value - double value you want to convert
pFromUnit - Unit that you want to convert from
Returns:
answer If the fromUnit does not match the classes from unit, then it is assumed that you are converting the other way ex. System.out.println(conversion.convertValue(17, conversion.getFromUnit()));

convertValue

public abstract java.lang.String convertValue(java.math.BigInteger numerator,
                                              java.math.BigInteger denominator,
                                              java.lang.String pFromUnit)
Converts a value represented by a numerator and denominator, returning a String representation of the result

Parameters:
numerator -
denominator -
pFromUnit -
Returns:

getRoundedResult

protected double getRoundedResult(double result)

convertValue

public java.lang.Double convertValue(java.lang.String value,
                                     java.lang.String pFromUnit,
                                     java.lang.String pTtoUnit)
Converts from one unit to another.

Parameters:
value - String value that you want to convert
pFromUnit - Unit you want to convert from
pTtoUnit - Unit you want to convert to
Returns:
Double value as a result. If it does not have the information to convert these units, then null is returned.

convertValue

public java.lang.Double convertValue(double value,
                                     java.lang.String pFromUnit,
                                     java.lang.String pTtoUnit)
Converts from one unit to another.

Parameters:
value - double value that you want to convert
pFromUnit - Unit you want to convert from
pTtoUnit - Unit you want to convert to
Returns:
Double value as a result. If it does not have the information to convert these units, then null is returned.

convertFraction

public java.lang.String convertFraction(java.lang.String value,
                                        java.lang.String pFromUnit,
                                        java.lang.String pToUnit)
Converts a fraction.

Parameters:
value - String value representing the fraction. If the value is not a fraction, then null will be returned.
pFromUnit -
pToUnit -
Returns:
String representation of the result. Should also be a fraction.

isFraction

public boolean isFraction(java.lang.String value)

getConversionPartner

public java.lang.String getConversionPartner(java.lang.String unit)
Returns the "Partner" conversion unit. If this converstion converts from a to b, and you pass in b, then you will get a. Likewise if you pass in a, you will get b

Parameters:
unit - String unit whose partner you are looking for.
Returns:
String:partner

getConversionPartnerAbbrev

public java.lang.String getConversionPartnerAbbrev(java.lang.String unit)
Returns the "Partner" conversion unit abbreviation. If this converstion converts from a to b, and you pass in b, then you will get a. Likewise if you pass in a, you will get b

Parameters:
unit - String unit whose partner you are looking for. Do not pass in an abbreviation, this is still looking to start with a unit
Returns:
String:partner abbreviation

multiply

public abstract java.lang.String multiply(Conversion byConversion)
Multiply this conversion by the passed in conversion

Parameters:
byConversion - Conversion you are multiplying by
Returns:
String representation of the value. I know this is a bit hokey, but it allows me to pass this back into the create method and generate another conversion if need be. This String number can be a fraction, decimal or integer.

divide

public abstract java.lang.String divide(Conversion byConversion)
Divide this conversion by the passed in conversion

Parameters:
byConversion - Conversion you are dividing by
Returns:
String representation of the value. I know this is a bit hokey, but it allows me to pass this back into the create method and generate another conversion if need be. This String number can be a fraction, decimal or integer.

getFromToFactor

public abstract double getFromToFactor()
Returns:
double representation of the FromToFacor - may not be applicable to all implementing classes.

setFromToFactor

public abstract void setFromToFactor(double fromToFactor)
Parameters:
fromToFactor - sets the double value of the fromToFactor - may not be applicable to all implementing classes.

getFromToOffset

public double getFromToOffset()
Returns:
double value of the fromTo Offset for this conversion.

setFromToOffset

public void setFromToOffset(double fromToOffset)
Parameters:
fromToOffset - sets the double value for the FromTo offset.

getFromUnit

public java.lang.String getFromUnit()
Returns:
Stirng FromToUnit value

setFromUnit

public void setFromUnit(java.lang.String fromUnit)
Parameters:
fromUnit - String fromToUnit value

getFromUnitAbbr

public java.lang.String getFromUnitAbbr()
Returns:
fromTo unit abbreviation

setFromUnitAbbr

public void setFromUnitAbbr(java.lang.String fromUnitAbbr)
Parameters:
fromUnitAbbr - String fromTo unit abbreviation

getToUnit

public java.lang.String getToUnit()
Returns:
to unit

setToUnit

public void setToUnit(java.lang.String toUnit)
Parameters:
toUnit - string to unit

getToUnitAbbr

public java.lang.String getToUnitAbbr()
Returns:
to unit abbreviation

setToUnitAbbr

public void setToUnitAbbr(java.lang.String toUnitAbbr)
Parameters:
toUnitAbbr - String to unit abbreviation

getGenerationAge

public int getGenerationAge()
This represents the general age for a conversion. Targeted to be used to help with significant digits.

Returns:
int value representing the age of this conversion.

setGenerationAge

public void setGenerationAge(int generationAge)
Parameters:
generationAge - sets the generation age

compareTo

public int compareTo(java.lang.Object obj)
Specified by:
compareTo in interface java.lang.Comparable

getFromToWholeNumber

public abstract java.math.BigInteger getFromToWholeNumber()

setFromToWholeNumber

public abstract void setFromToWholeNumber(java.math.BigInteger fromToWholeNumber)

getFromToBottomFactor

public abstract java.math.BigInteger getFromToBottomFactor()

setFromToBottomFactor

public abstract void setFromToBottomFactor(java.math.BigInteger fromToBottomFactor)

getFromToTopFactor

public abstract java.math.BigInteger getFromToTopFactor()

setFromToTopFactor

public abstract void setFromToTopFactor(java.math.BigInteger fromToTopFactor)

setFromToFactorString

public abstract void setFromToFactorString(java.lang.String factor)

getFromToFactorString

public abstract java.lang.String getFromToFactorString()


JConvert - opensource project by Ed Sarrazin