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.List;
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 final Map sUniqueMap = new HashMap<>(8);
private static final Map sLookupMap = new HashMap<>(25);
//**************************************************************************
// PUBLIC FIELDS
//**************************************************************************
// The base SI unit of time is the second
public static final TimeUnit second = new TimeUnit(MeasurementSystem.SI, "second", "s", Calendar.SECOND).addAlternateName("sec").setPlural("seconds");
public static final TimeUnit millisecond = new TimeUnit(MeasurementSystem.SI, "millisecond", "ms", Calendar.MILLISECOND, new BaseSIUnitConverter(.001)).setPlural("milliseconds");
public static final TimeUnit minute = new TimeUnit(MeasurementSystem.SI, "minute", "min", Calendar.MINUTE, new BaseSIUnitConverter(60)).setPlural("minutes");
public static final TimeUnit hour = new TimeUnit(MeasurementSystem.SI, "hour", "hr", Calendar.HOUR_OF_DAY, new BaseSIUnitConverter(60 * 60)).setPlural("hours").setPluralAbbrev("hrs");
public static final TimeUnit day = new TimeUnit(MeasurementSystem.SI, "day", "day", Calendar.DAY_OF_MONTH, new BaseSIUnitConverter(24 * 60 * 60)).setPlural("days").setPluralAbbrev("days");
public static final TimeUnit week = new TimeUnit(MeasurementSystem.SI, "week", "wk", Calendar.WEEK_OF_YEAR, new BaseSIUnitConverter(7 * 24 * 60 * 60)).setPlural("weeks").setPluralAbbrev("wks");
public static final TimeUnit month = new TimeUnit(MeasurementSystem.SI, "month", "mon", Calendar.MONTH, new BaseSIUnitConverter(365.25 * 24 * 60 * 60 / 12)).setPlural("months");
public static final TimeUnit year = new TimeUnit(MeasurementSystem.SI, "year", "yr", Calendar.YEAR, new BaseSIUnitConverter(365.25 * 24 * 60 * 60)).setPlural("years").setPluralAbbrev("yrs");
//**************************************************************************
// CONSTRUCTORS
//**************************************************************************
//---------------------------------------------------------------------------
private TimeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, int inCalendarNum)
{
this(inSystem, inName, inAbbrev, inCalendarNum, null);
}
//---------------------------------------------------------------------------
private TimeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, int inCalendarNum, BaseSIUnitConverter inBaseUnitConverter)
{
super(inSystem, 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);
}
//---------------------------------------------------------------------------
protected TimeUnit(List inSubUnits)
{
super(inSubUnits);
}
//**************************************************************************
// 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));
}
// Capitalization?
if (null == result
&& Character.isUpperCase(stringValue.charAt(0)))
{
result = sLookupMap.get(stringValue.toLowerCase());
// Plural?
if (null == result
&& stringValue.toLowerCase().endsWith("s"))
{
result = sLookupMap.get(stringValue.substring(0, stringValue.length() - 1).toLowerCase());
}
}
if (null == result)
{
// Try a more intensive parse attempt
Unit unitResult = Unit.valueOf(stringValue);
if (unitResult instanceof TimeUnit)
{
result = (TimeUnit) unitResult;
}
}
}
return result;
}
//---------------------------------------------------------------------------
@Override
public TimeUnit setMeasurementSystem(MeasurementSystem inValue)
{
return (TimeUnit) super.setMeasurementSystem(inValue);
}
//---------------------------------------------------------------------------
@Override
public TimeUnit setPlural(String inValue)
{
return (TimeUnit) super.setPlural(inValue);
}
//---------------------------------------------------------------------------
@Override
public TimeUnit setPluralAbbrev(String inValue)
{
return (TimeUnit) super.setPluralAbbrev(inValue);
}
//---------------------------------------------------------------------------
/**
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;
}
}