001package com.hfg.units.length; 002 003 004import com.hfg.units.LengthUnit; 005import com.hfg.units.Quantity; 006import com.hfg.units.QuantityType; 007import com.hfg.units.UnitParseException; 008import com.hfg.util.StringUtil; 009 010//------------------------------------------------------------------------------ 011/** 012 Convenience extension of Quantity for length. 013 <div> 014 @author J. Alex Taylor, hairyfatguy.com 015 </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class Length extends Quantity 039{ 040 041 //########################################################################### 042 // CONSTRUCTORS 043 //########################################################################### 044 045 //--------------------------------------------------------------------------- 046 /** 047 Convenience constructor that will call LengthUnit.valueOf() on the specified unit string. 048 * @param inValue the numeric amount as a float 049 * @param inUnit the length unit of the specified amount 050 */ 051 public Length(Float inValue, String inUnit) 052 { 053 this(inValue, LengthUnit.valueOf(inUnit)); 054 055 if (null == getUnit()) 056 { 057 throw new UnitParseException("The string " + StringUtil.singleQuote(inUnit) + " couldn't be parsed into a LengthUnit!"); 058 } 059 } 060 061 //--------------------------------------------------------------------------- 062 /** 063 Convenience constructor that will call LengthUnit.valueOf() on the specified unit string. 064 * @param inValue the numeric amount as an integer 065 * @param inUnit the length unit of the specified amount 066 */ 067 public Length(Integer inValue, String inUnit) 068 { 069 this(inValue, LengthUnit.valueOf(inUnit)); 070 071 if (null == getUnit()) 072 { 073 throw new UnitParseException("The string " + StringUtil.singleQuote(inUnit) + " couldn't be parsed into a LengthUnit!"); 074 } 075 } 076 077 //--------------------------------------------------------------------------- 078 public Length(Double inValue, LengthUnit inUnit) 079 { 080 super(inValue, inUnit); 081 } 082 083 //--------------------------------------------------------------------------- 084 public Length(Float inValue, LengthUnit inUnit) 085 { 086 super(inValue, inUnit); 087 } 088 089 //--------------------------------------------------------------------------- 090 public Length(Integer inValue, LengthUnit inUnit) 091 { 092 super(inValue, inUnit); 093 } 094 095 //--------------------------------------------------------------------------- 096 /** 097 Convenience constructor. 098 * @param inValue the string value 099 */ 100 public Length(String inValue) 101 { 102 super(inValue); 103 104 if (! getUnit().getQuantityType().equals(QuantityType.LENGTH)) 105 { 106 throw new UnitParseException("The units in the string " + StringUtil.singleQuote(inValue) + " are not length units!"); 107 } 108 } 109 110 //########################################################################### 111 // PUBLIC METHODS 112 //########################################################################### 113 114 //--------------------------------------------------------------------------- 115 @Override 116 public LengthUnit getUnit() 117 { 118 return (LengthUnit) super.getUnit(); 119 } 120 121 //--------------------------------------------------------------------------- 122 /** 123 Returns the length in the specified units. 124 @return the length the units specified. 125 */ 126 public Length convertTo(LengthUnit inUnit) 127 { 128 return new Length(super.convertTo(inUnit).doubleValue(), inUnit); 129 } 130 131}