com.hfg.units.TimeUnit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.units;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Enumeration of standard time units.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding 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 TimeUnit extends Unit
{
//**************************************************************************
// PRIVATE FIELDS
//**************************************************************************
private int mCalendarNum;
private long mMilliseconds;
private static Map sUniqueMap = new HashMap<>();
private static Map sLookupMap = new HashMap<>();
//**************************************************************************
// PUBLIC FIELDS
//**************************************************************************
public static final TimeUnit second = new TimeUnit( "second", "s", Calendar.SECOND).addAlternateName("sec");
public static final TimeUnit minute = new TimeUnit( "minute", "min", Calendar.MINUTE, new BaseSIUnitConverter(60));
public static final TimeUnit hour = new TimeUnit( "hour", "hr", Calendar.HOUR_OF_DAY, new BaseSIUnitConverter(60 * 60));
public static final TimeUnit day = new TimeUnit( "day", "day", Calendar.DAY_OF_MONTH, new BaseSIUnitConverter(24 * 60 * 60));
public static final TimeUnit week = new TimeUnit( "week", "wk", Calendar.WEEK_OF_YEAR, new BaseSIUnitConverter(7 * 24 * 60 * 60));
public static final TimeUnit month = new TimeUnit( "month", "mon", Calendar.MONTH, new BaseSIUnitConverter(365.25 * 24 * 60 * 60 / 12));
public static final TimeUnit year = new TimeUnit( "year", "yr", Calendar.YEAR, new BaseSIUnitConverter(365.25 * 24 * 60 * 60));
//**************************************************************************
// CONSTRUCTORS
//**************************************************************************
//---------------------------------------------------------------------------
private TimeUnit(String inName, String inAbbrev, int inCalendarNum)
{
this(inName, inAbbrev, inCalendarNum, null);
}
//---------------------------------------------------------------------------
private TimeUnit(String inName, String inAbbrev, int inCalendarNum, BaseSIUnitConverter inBaseUnitConverter)
{
super(MeasurementSystem.Metric, QuantityType.TIME, inName, inAbbrev, null, inBaseUnitConverter);
mCalendarNum = inCalendarNum;
mMilliseconds = 1000 * (int) this.computeBaseSIValue(1);
sUniqueMap.put(name(), this);
sLookupMap.put(name(), this);
sLookupMap.put(getAbbrev(), this);
}
//**************************************************************************
// PUBLIC METHODS
//**************************************************************************
//---------------------------------------------------------------------------
public static Collection values()
{
return sUniqueMap.values();
}
//---------------------------------------------------------------------------
/**
Returns the TimeUnit object that corresponds to the specified name or abbreviation string.
@param inValue the string name or abbreviation value to be converted into a TimeUnit object
@return the TimeUnit constant corresponding to the specified name or abbreviation
*/
public static TimeUnit valueOf(String inValue)
{
TimeUnit result = null;
if (StringUtil.isSet(inValue))
{
String stringValue = inValue.trim();
result = sLookupMap.get(stringValue);
// Plural?
if (null == result
&& stringValue.endsWith("s"))
{
result = sLookupMap.get(stringValue.substring(0, stringValue.length() - 1));
}
}
return result;
}
//---------------------------------------------------------------------------
/**
Returns the associated int value to use with a Calendar object's get() method.
@return the Calendar object int value associated with this time unit.
*/
public int getCalendarNum()
{
return mCalendarNum;
}
//---------------------------------------------------------------------------
/**
Returns the time unit'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 unit of time.
*/
public long getMilliseconds()
{
return mMilliseconds;
}
//---------------------------------------------------------------------------
@Override
public TimeUnit addAlternateName(String inValue)
{
super.addAlternateName(inValue);
sLookupMap.put(inValue, this);
return this;
}
}