All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.units.TimeSpan Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.units;

//------------------------------------------------------------------------------

import com.hfg.util.StringUtil;

/**
 Quantity of time.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class TimeSpan extends Quantity { //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- /** Convenience constructor that will call TimeUnit.valueOf() on the specified unit string. * @param inValue the numeric amount as a float * @param inUnit the time unit of the specified amount */ public TimeSpan(Float inValue, String inUnit) { this(inValue, TimeUnit.valueOf(inUnit)); if (null == getUnit()) { throw new UnitParseException("The string " + StringUtil.singleQuote(inUnit) + " couldn't be parsed into a TimeUnit!"); } } //--------------------------------------------------------------------------- /** Convenience constructor that will call TimeUnit.valueOf() on the specified unit string. * @param inValue the numeric amount as an integer * @param inUnit the time unit of the specified amount */ public TimeSpan(Integer inValue, String inUnit) { this(inValue, TimeUnit.valueOf(inUnit)); if (null == getUnit()) { throw new UnitParseException("The string " + StringUtil.singleQuote(inUnit) + " couldn't be parsed into a TimeUnit!"); } } //--------------------------------------------------------------------------- /** Convenience constructor that will call TimeUnit.valueOf() on the specified unit string. * @param inValue the numeric amount as a long * @param inUnit the time unit of the specified amount */ public TimeSpan(Long inValue, String inUnit) { this(inValue, TimeUnit.valueOf(inUnit)); if (null == getUnit()) { throw new UnitParseException("The string " + StringUtil.singleQuote(inUnit) + " couldn't be parsed into a TimeUnit!"); } } //--------------------------------------------------------------------------- public TimeSpan(Double inValue, TimeUnit inUnit) { super(inValue, inUnit); } //--------------------------------------------------------------------------- public TimeSpan(Float inValue, TimeUnit inUnit) { super(inValue, inUnit); } //--------------------------------------------------------------------------- public TimeSpan(Integer inValue, TimeUnit inUnit) { super(inValue, inUnit); } //--------------------------------------------------------------------------- public TimeSpan(Long inValue, TimeUnit inUnit) { super(inValue, inUnit); } //--------------------------------------------------------------------------- /** Convenience constructor. * @param inValue the string value */ public TimeSpan(String inValue) { super(inValue); if (! getUnit().getQuantityType().equals(QuantityType.TIME)) { throw new UnitParseException("The units in the string " + StringUtil.singleQuote(inValue) + " are not time units!"); } } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public TimeUnit getUnit() { return (TimeUnit) super.getUnit(); } //--------------------------------------------------------------------------- /** Returns the time span's length in milliseconds. Note that the values for MONTH and YEAR are approximations. @return the length of time in milliseconds that corresponds to this span of time. */ public long getMilliseconds() { return (intValue() * getUnit().getMilliseconds()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy