001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.xpdl.model.misc;
046:
047: import org.obe.util.AbstractEnum;
048: import org.obe.util.CommonConfig;
049:
050: import java.util.Calendar;
051: import java.util.HashMap;
052: import java.util.List;
053: import java.util.Map;
054:
055: /**
056: * Class which defines all duration units available. This class defines
057: * the following duration units:
058: * <p/>
059: * <ul>
060: * <li><b>Y</b> - Year
061: * <li><b>M</b> - Month
062: * <li><b>D</b> - Day
063: * <li><b>h</b> - Hour
064: * <li><b>m</b> - Minute
065: * <li><b>s</b> - Second
066: * </ul>
067: *
068: * @author Adrian Price
069: */
070: public final class DurationUnit extends AbstractEnum {
071: private static final long serialVersionUID = 3955674746845399636L;
072:
073: public static final int SECOND_INT = 0;
074: public static final int MINUTE_INT = 1;
075: public static final int HOUR_INT = 2;
076: public static final int DAY_INT = 3;
077: public static final int MONTH_INT = 4;
078: public static final int YEAR_INT = 5;
079: public static final DurationUnit SECOND = new DurationUnit("s",
080: SECOND_INT);
081: public static final DurationUnit MINUTE = new DurationUnit("m",
082: MINUTE_INT);
083: public static final DurationUnit HOUR = new DurationUnit("h",
084: HOUR_INT);
085: public static final DurationUnit DAY = new DurationUnit("D",
086: DAY_INT);
087: public static final DurationUnit MONTH = new DurationUnit("M",
088: MONTH_INT);
089: public static final DurationUnit YEAR = new DurationUnit("Y",
090: YEAR_INT);
091: public static final DurationUnit DEFAULT;
092: private static final long MS_PER_S = 1000L;
093: private static final int S_PER_M = 60;
094: private static final int M_PER_H = 60;
095: private static final int H_PER_D = 24;
096: private static final int D_PER_M = 30;
097: private static final int D_PER_Y = 365;
098:
099: private static final DurationUnit[] _values = { SECOND, MINUTE,
100: HOUR, DAY, MONTH, YEAR };
101: private static final int[] _jdkCalendarFields = { Calendar.SECOND,
102: Calendar.MINUTE, Calendar.HOUR, Calendar.DAY_OF_MONTH,
103: Calendar.MONTH, Calendar.YEAR };
104: private static final Map _tagMap = new HashMap();
105: private static final Map unitToMillisecondsMap = new HashMap();
106: public static final List VALUES = clinit(_values, _tagMap);
107:
108: static {
109: unitToMillisecondsMap.put(SECOND, new Long(MS_PER_S));
110: unitToMillisecondsMap.put(MINUTE, new Long(MS_PER_S * S_PER_M));
111: unitToMillisecondsMap.put(HOUR, new Long(MS_PER_S * S_PER_M
112: * M_PER_H));
113: unitToMillisecondsMap.put(DAY, new Long(MS_PER_S * S_PER_M
114: * M_PER_H * H_PER_D));
115: unitToMillisecondsMap.put(MONTH, new Long(MS_PER_S * S_PER_M
116: * M_PER_H * H_PER_D * D_PER_M));
117: unitToMillisecondsMap.put(YEAR, new Long(MS_PER_S * S_PER_M
118: * M_PER_H * H_PER_D * D_PER_Y));
119: DEFAULT = valueOf(CommonConfig.getDefaultDurationUnit());
120: }
121:
122: /**
123: * Converts the specified type String to a DurationUnit object. If the
124: * String cannot be converted to a DurationUnit then the value null
125: * is returned.
126: *
127: * @param tag The duration unit String
128: * @return A DurationUnit or null
129: */
130: public static DurationUnit valueOf(String tag) {
131: DurationUnit durationUnit = (DurationUnit) _tagMap.get(tag);
132: if (durationUnit == null && tag != null)
133: throw new IllegalArgumentException(tag);
134: return durationUnit;
135: }
136:
137: /**
138: * Constructs a new duration unit.
139: *
140: * @param name
141: * @param ordinal An int value
142: */
143: private DurationUnit(String name, int ordinal) {
144: super (name, ordinal);
145: }
146:
147: /**
148: * Returns the integer code for the corresponding JDK Calendar field. This
149: * value can be passed to the <code>java.util.Calendar.add(int, int)</code>
150: * method.
151: *
152: * @return One of the constants declared by the
153: * <code>java.util.Calendar</code> class.
154: */
155: public int getJDKCalendarField() {
156: return _jdkCalendarFields[ordinal];
157: }
158:
159: /**
160: * Converts the duration unit to milliseconds.<br/>
161: * <br/>
162: * <em>N.B. For duration units {@link #MONTH} and {@link #YEAR} this method
163: * returns only a rough approximation based on 30 days per month and 365
164: * days per year respectively. Be wary of using the resulting milliseconds
165: * value for serious temporal computations.</em>
166: *
167: * @return Duration unit value expressed milliseconds.
168: */
169: public long toMilliseconds() {
170: return ((Long) unitToMillisecondsMap.get(this )).longValue();
171: }
172:
173: public List family() {
174: return VALUES;
175: }
176: }
|