uk.org.retep.util.unit.LengthUnit 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 measuring length, with the ability to convert into other standard
* units of measuring lengths.
*
*
* Wikipedia Article: Conversion_of_units
*
*
* @author peter
* @since 7.1
*/
public enum LengthUnit
{
/**
*
* An inch (plural: inches; symbol or abbreviation: in or, sometimes, " - a double
* prime) is the name of a unit of length in English / Imperial units.
* There are 36 inches in a yard and 12 inches in a foot.
* A corresponding unit of area is the square inch and a corresponding unit of
* volume is the cubic inch.
*
*
* The inch is very commonly used in the United Kingdom. In the UK, personal
* heights are expressed in feet and inches by people of all ages.
*
* Wikipedia Article: Inch
*
* @since 7.1
*/
INCH()
{
public double convert( double a, LengthUnit u )
{
return u.toInch( a );
}
public String getName()
{
return "Inch";
}
public String getSymbol()
{
return "in";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isSI()
{
return false;
}
public double toMetre( double l )
{
return l * 0.0254;
}
public double toInch( double l )
{
return l;
}
public double toFoot( double l )
{
return l * 12.0;
}
public double toMile( double l )
{
return l / 63360.0;
}
public double toYard( double l )
{
return l * 36.0;
}
},
/**
*
* A foot (plural: feet; symbol or abbreviation: ft or, sometimes, ' - a prime)
* is a unit of length in English / Imperial units.
*
*
* The most commonly used foot today is the international foot represented by
* this LengthUnit.
*
*
* There are 3 feet in a yard and 12 inches in a foot.
*
* Wikipedia Article: Foot_(unit_of_length)
* @since 7.1
*/
FOOT()
{
public double convert( double a, LengthUnit u )
{
return u.toFoot( a );
}
public String getName()
{
return "Foot";
}
public String getSymbol()
{
return "ft";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isSI()
{
return false;
}
public double toMetre( double l )
{
return l * 0.3048;
}
public double toInch( double l )
{
return l * 12.0;
}
public double toFoot( double l )
{
return l;
}
public double toMile( double l )
{
return l / 5280.0;
}
public double toYard( double l )
{
return l / 3.0;
}
},
/**
*
* A mile is a unit of length, usually used to measure distance.
*
* Wikipedia Article: Mile
* @since 7.1
*/
MILE()
{
public double convert( double a, LengthUnit u )
{
return u.toMile( a );
}
public String getName()
{
return "Mile";
}
public String getSymbol()
{
return "mil";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isSI()
{
return false;
}
public double toMetre( double l )
{
return l * 1609.344;
}
public double toInch( double l )
{
return l * 63360.0;
}
public double toFoot( double l )
{
return l * 5280.0;
}
public double toMile( double l )
{
return l;
}
public double toYard( double l )
{
return l * 1760.0;
}
},
/**
*
* A yard (abbreviation: yd) is the name of a unit of length in English /
* Imperial units. A yard is three feet or 36 inches.
* The yard is often used to express distances.
* A corresponding unit of area is the square yard.
*
*
* The most commonly used yard today is the international yard represented by
* this LengthUnit, which by definition is equal to 0.9144 metre.
*
* Wikipedia Article: METRE
* @since 7.1
*/
YARD()
{
public double convert( double a, LengthUnit u )
{
return u.toYard( a );
}
public String getName()
{
return "Yard";
}
public String getSymbol()
{
return "yd";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isSI()
{
return false;
}
public double toMetre( double l )
{
return l * 0.9144;
}
public double toInch( double l )
{
return l / 36.0;
}
public double toFoot( double l )
{
return l * 3.0;
}
public double toMile( double l )
{
return l / 1760.0;
}
public double toYard( double l )
{
return l;
}
},
/**
* The metre, or meter (U.S.), is a measure of length. It is the basic unit
* of length in the metric system and in the International System of Units
* (SI), used around the world for general and scientific purposes.
* @since 7.1
* Wikipedia Article: METRE
* @since 7.1
*/
METRE()
{
public double convert( double a, LengthUnit u )
{
return u.toMetre( a );
}
public String getName()
{
return "METRE";
}
public String getDefinition()
{
return "";
}
public String getSymbol()
{
return "m";
}
public char unitChar()
{
return 'm';
}
public boolean isSI()
{
return true;
}
public double toMetre( double l )
{
return l;
}
public double toInch( double l )
{
return l / 0.0254;
}
public double toFoot( double l )
{
return l / .3048;
}
public double toMile( double l )
{
return l / 1609.344;
}
public double toYard( double l )
{
return l / 0.9144;
}
},
/**
*
* A Metric Cubit used in some countries, is 1/2 m or 500.00 mm.
*
*
* Cubit is the name for any one of many units of measure used by various
* ancient peoples and is among the first recorded units of length.
* The Cubit is possibly based on the forearm length of an average person,
* and the Egyptian hieroglyph for the unit shows this symbol.
*
*
* It was employed consistently for measuring products like timber, stone,
* cords and textiles through Antiquity, the Middle-Ages up to the Early Modern
* Times.
*
* Wikipedia Article: Cubit
* @since 7.1
*/
METRIC_CUBIT()
{
public double convert( double a, LengthUnit u )
{
return u.toMetricCubit( a );
}
public String getName()
{
return "Metric Cubit";
}
public String getSymbol()
{
return "cubit";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isSI()
{
return false;
}
public double toMetre( double l )
{
return l / 2.0;
}
public double toInch( double l )
{
return METRE.toInch( toMetre( l ) );
}
public double toMetricCubit( double l )
{
return l;
}
public double toFoot( double l )
{
return METRE.toFoot( toMetre( l ) );
}
public double toMile( double l )
{
return METRE.toMile( toMetre( l ) );
}
public double toYard( double l )
{
return METRE.toYard( toMetre( l ) );
}
},
/**
*
* The Roman cubitus is a six-palms-cubit of about 444.5 mm or 17.5 inches.
*
*
* The Cubit is the name for any one of many units of measure used by various
* ancient peoples and is among the first recorded units of length.
* The Cubit is possibly based on the forearm length of an average person,
* and the Egyptian hieroglyph for the unit shows this symbol.
*
*
* It was employed consistently for measuring products like timber, stone,
* cords and textiles through Antiquity, the Middle-Ages up to the Early Modern
* Times.
*
* Wikipedia Article: Cubit
* @since 7.1
*/
ROMAN_CUBIT()
{
public double convert( double a, LengthUnit u )
{
return u.toRomanCubit( a );
}
public String getName()
{
return "Roman Cubit";
}
public String getSymbol()
{
return "cubit";
}
public char unitChar()
{
return UNICODE_UNSUPPORTED;
}
public boolean isSI()
{
return false;
}
public double toMetre( double l )
{
return l * 444.5;
}
public double toInch( double l )
{
return METRE.toInch( toMetre( l ) );
}
public double toRomanCubit( double l )
{
return l;
}
public double toFoot( double l )
{
return METRE.toFoot( toMetre( l ) );
}
public double toMile( double l )
{
return METRE.toMile( toMetre( l ) );
}
public double toYard( double l )
{
return METRE.toYard( toMetre( l ) );
}
};
/**
* char returned by unitChar() when there is no unicode character defined for a
* AngleUnit.
*/
public static final char UNICODE_UNSUPPORTED = '?';
/**
* Convert the given angle in the given unit to this
* unit.
*
* For example, to convert 1 metres into yards, use:
* LengthUnit.YARDS.convert( 180.0, LengthUnit.METRE )
*
* @param sourceLength the length in the given sourceUnit
* @param sourceUnit the unit of the sourceLength argument
* @return the converted length in this unit.
*/
public double convert( double sourceLength, AngleUnit sourceUnit )
{
throw new AbstractMethodError();
}
/**
* The proper name of this LengthUnit, including the correct punctuation.
* @return The proper name of this LengthUnit
*/
public String getName()
{
throw new AbstractMethodError();
}
/**
* The string representation of this unit's symbol
* @return string representation of this unit's symbol
*/
public String getSymbol()
{
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;
}
/**
* Is this unit an SI unit?
* @return true if this unit is an SI unit
*/
public boolean isSI()
{
return true;
}
/**
* Equivalent to METRE.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toMetre( double l )
{
throw new AbstractMethodError();
}
/**
* Equivalent to METRIC_CUBIT.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toMetricCubit( double l )
{
return toMetre( l ) * 2.0;
}
/**
* Equivalent to ROMAN_CUBIT.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toRomanCubit( double l )
{
return toMetre( l ) / .4445;
}
/**
* Equivalent to INCH.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toInch( double l )
{
throw new AbstractMethodError();
}
/**
* Equivalent to FOOT.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toFoot( double l )
{
throw new AbstractMethodError();
}
/**
* Equivalent to MILE.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toMile( double l )
{
throw new AbstractMethodError();
}
/**
* Equivalent to YARD.convert(duration, this).
* @param l the length
* @return the converted length
*/
public double toYard( double l )
{
throw new AbstractMethodError();
}
}