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