uk.org.retep.util.unit.TemperatureUnit Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.util.unit;
/**
* A unit of temperature, with the ability to convert into other standard units
* of temperature.
* @author peter
* @see http://en.wikipedia.org/wiki/Temperature_conversion_formulas
* @since 7.1
*/
public enum TemperatureUnit
{
/**
* Celsius is, or relates to, the Celsius temperature scale
* (previously known as the centigrade scale). The degree Celsius (symbol: C)
* can refer to a specific temperature on the Celsius scale as well as serve
* as unit increment to indicate a temperature interval
* (a difference between two temperatures or an uncertainty).
*
* Celsius is named after the Swedish astronomer Anders Celsius (1701 - 1744),
* who developed a similar temperature scale two years before his death.
* @since 7.1
* @see Wikipedia Article
*/
CELSIUS()
{
public String getName()
{
return "Celsius";
}
public double convert( double t, TemperatureUnit u )
{
return u.toCelsius( t );
}
public double toCelsius( double t )
{
return t;
}
public double toFahrenheit( double t )
{
return ( t * 1.8 ) + 32.0;
}
public double toKelvin( double t )
{
return t + 273.15;
}
public double toNewton( double t )
{
return t * 0.33;
}
public double toRankine( double t )
{
return ( t * 9.0 / 5.0 ) + 491.67;
}
public double toReaumur( double t )
{
return t * 4.0 / 5.0;
}
public double toRomer( double t )
{
return ( t * 21.0 / 40.0 ) + 7.5;
}
public double toDelisle( double t )
{
return ( 100.0 - t ) * 3.0 / 2.0;
}
public String unitString()
{
return "\u00B0C";
}
public char unitChar()
{
return '\u2103';
}
public boolean isStandard()
{
return true;
}
},
/**
* The Delisle scale is a temperature scale invented in 1732 by the French
* astronomer Joseph-Nicolas Delisle (1688-1768).
* It is similar to that of Reaumur.
* @since 7.1
* @see Wikipedia Article
*/
DELISLE()
{
public String getName()
{
return "Delisle";
}
public double convert( double t, TemperatureUnit u )
{
return u.toDelisle( t );
}
public double toCelsius( double t )
{
return 100 - ( t * 2.0 / 3.0 );
}
public double toDelisle( double t )
{
return t;
}
public String unitString()
{
return "\u00B0De";
}
public boolean isStandard()
{
return false;
}
},
/**
* Fahrenheit is a temperature scale named after the Polish/German physicist
* Daniel Gabriel Fahrenheit (1686-1736), who proposed it in 1724.
* @since 7.1
* @see Wikipedia Article
*/
FAHRENHEIT()
{
public String getName()
{
return "Fahrenheit";
}
public double convert( double t, TemperatureUnit u )
{
return u.toFahrenheit( t );
}
public double toCelsius( double t )
{
return ( t - 32.0 ) / 1.8;
}
public double toFahrenheit( double t )
{
return t;
}
public String unitString()
{
return "\u00B0F";
}
public char unitChar()
{
return '\u2109';
}
public boolean isStandard()
{
return true;
}
},
/**
* The Kelvin scale is a thermodynamic (absolute) temperature scale where
* absolute zero-the lowest possible temperature where nothing could be colder
* and no heat energy remains in a substance is defined as zero kelvin (0 K).
*
* The unit increment of the Kelvin scale is the kelvin (symbol: K), which is the
* SI unit of temperature and is one of the seven SI base units.
* @since 7.1
* @see Wikipedia Article
*/
KELVIN()
{
public String getName()
{
return "Kelvin";
}
public double convert( double t, TemperatureUnit u )
{
return u.toKelvin( t );
}
public double toCelsius( double t )
{
return t - 273.15;
}
public double toKelvin( double t )
{
return t;
}
public String unitString()
{
return "K";
}
public char unitChar()
{
return '\u212A';
}
public boolean isStandard()
{
return true;
}
},
/**
* The Newton scale is a temperature scale devised by Isaac Newton around 1700.
* @since 7.1
* @see Wikipedia Article
*/
NEWTON()
{
public String getName()
{
return "Newton";
}
public double convert( double t, TemperatureUnit u )
{
return u.toNewton( t );
}
public double toCelsius( double t )
{
return t*100.0/33.0;
}
public double toNewton( double t )
{
return t;
}
public String unitString()
{
return "\u00B0N";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isStandard()
{
return false;
}
},
/**
* Rankine is a thermodynamic (absolute) temperature scale named after the
* Scottish engineer and physicist William John Macquorn Rankine,
* who proposed it in 1859.
* @since 7.1
* @see Wikipedia Article
*/
RANKINE()
{
public String getName()
{
return "Rankine";
}
public double convert( double t, TemperatureUnit u )
{
return u.toRankine( t );
}
public double toCelsius( double t )
{
return KELVIN.toCelsius( toKelvin( t ) );
}
public double toFahrenheit( double t )
{
return t - 459.67;
}
public double toKelvin( double t )
{
return t / 1.8;
}
public double toRankine( double t )
{
return t;
}
public String unitString()
{
return "\u00B0Ra";
}
public boolean isStandard()
{
return false;
}
},
/**
* The Reaumur scale is a temperature scale named after Ren Antoine Ferchault de
* Reaumur, who first proposed it in 1731. The freezing point of water is 0 degrees
* Reaumur, the boiling point 80 degrees Reaumur.
* Hence, a Reaumur degree is 1.25 Celsius degrees or kelvins.
* The Reaumur temperature scale is also known as the octogesimal division
* (division octogesimale in French).
* @since 7.1
* @see Wikipedia Article
*/
REAUMUR()
{
public String getName()
{
return "R\u00E9aumur";
}
public double convert( double t, TemperatureUnit u )
{
return u.toReaumur( t );
}
public double toCelsius( double t )
{
return t*5.0/4.0;
}
public double toReaumur( double t )
{
return t;
}
public String unitString()
{
return "\u00B0R\u00E9";
}
public boolean isStandard()
{
return false;
}
},
/**
* Romer is a disused temperature scale named after the Danish astronomer
* Ole Christensen Romer, who proposed it in 1701.
* @since 7.1
* @see Wikipedia Article
*/
ROMER()
{
public String getName()
{
return "R\u00F8mer";
}
public double convert( double t, TemperatureUnit u )
{
return u.toRomer( t );
}
public double toCelsius( double t )
{
return ( t - 7.5 ) * 40.0 / 21.0;
}
public double toRomer( double t )
{
return t;
}
public String unitString()
{
return "\u00B0R\u00F8";
}
public boolean isStandard()
{
return false;
}
};
/**
* char returned by unitChar() when there is no unicode character defined for a
* TemperatureUnit
*/
public static final char UNICODE_UNSUPPORTED = '?';
/**
* Convert the given temperature in the given unit to this
* unit.
*
* For example, to convert 10 Kelvin to Celsius, use:
* TemperatureUnit.CELSIUS.convert( 10.0, TimeUnit.KELVIN )
*
* @param sourceTemperature the temperature in the given sourceUnit
* @param sourceUnit the unit of the sourceTemperature argument
* @return the converted temperature in this unit.
*/
public double convert( double sourceTemperature, TemperatureUnit sourceUnit )
{
throw new AbstractMethodError();
}
/**
* Equivalent to CELSIUS.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toCelsius( double t )
{
throw new AbstractMethodError();
}
/**
* Equivalent to FAHRENHEIT.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toFahrenheit( double t )
{
return CELSIUS.toFahrenheit( toCelsius( t ) );
}
/**
* Equivalent to KELVIN.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toKelvin( double t )
{
return CELSIUS.toKelvin( toCelsius( t ) );
}
/**
* Equivalent to RANKINE.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toRankine( double t )
{
return CELSIUS.toRankine( toCelsius( t ) );
}
/**
* Equivalent to NEWTON.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toNewton( double t )
{
return CELSIUS.toNewton( toCelsius( t ) );
}
/**
* Equivalent to REAUMUR.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toReaumur( double t )
{
return CELSIUS.toReaumur( toCelsius( t ) );
}
/**
* Equivalent to ROMER.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toRomer( double t )
{
return CELSIUS.toRomer( toCelsius( t ) );
}
/**
* Equivalent to DELISLE.convert(duration, this).
* @param t the temperature
* @return the converted temperature
*/
public double toDelisle( double t )
{
return CELSIUS.toDelisle( toCelsius( t ) );
}
/**
* The string representation of this unit as a unicode string.
* @return string representation of this unit as a unicode string
*/
public String unitString()
{
throw new AbstractMethodError();
}
/**
* The unicode character representing this unit. If there is no character defined
* in the unicode standard, then UNICODE_UNSUPPORTED is returned.
* @return character representing this unit
*/
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
/**
* Convert the given temperature into a String including it's unit
* @param t temperature
* @return The temperature with it's unit
*/
public final String toString( double t )
{
return toString( t, this );
}
/**
* Convert the given temperature into a String including it's unit
* @param t temperature
* @param unit The TemperatureUnit to format
* @return The temperature with it's unit
*/
public final static String toString( double t, TemperatureUnit unit )
{
return String.format( "%.2f%s", t, unit.unitString() );
}
/**
* Convert the given temperature into a String including it's unit.
* Unlike the toString() varients, this will use the unicode character
* to represent the unit if the TemperatureUnit has a unicode character.
* If the TemperatureUnit does not support a unicode character, then this
* is the same as toString( t ).
* @param t temperature
* @return The temperature with it's unit
*/
public final String toUnicodeString( double t )
{
return toUnicodeString( t, this );
}
/**
* Convert the given temperature into a String including it's unit
* Unlike the toString() varients, this will use the unicode character
* to represent the unit if the TemperatureUnit has a unicode character.
* If the TemperatureUnit does not support a unicode character, then this
* is the same as toString( t, unit ).
* @param t temperature
* @param unit The TemperatureUnit to format
* @return The temperature with it's unit
*/
public final static String toUnicodeString( double t, TemperatureUnit unit )
{
if( unit.unitChar() == UNICODE_UNSUPPORTED )
{
return toString( t, unit );
}
else
{
return String.format( "%.2f%c", t, unit.unitChar() );
}
}
/**
* Is this TemperatureUnit a recognised standard
* @return true if the TemperatureUnit is a current recognised standard unit.
*/
public boolean isStandard()
{
throw new AbstractMethodError();
}
/**
* Is this TemperatureUnit now disused?
*
* Equivalent to !isStandard()
* @return true if this unit is now disused.
*/
public final boolean isDisused()
{
return !isStandard();
}
/**
* The proper name of this TemperatureUnit, including the correct punctuation.
* @return The proper name of this TemperatureUnit
*/
public String getName()
{
throw new AbstractMethodError();
}
}