1 |
|
---|
2 | package ise.antelope.tasks.typedefs;
|
---|
3 |
|
---|
4 | import java.util.*;
|
---|
5 | import org.apache.tools.ant.types.EnumeratedAttribute;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * The enumeration of units: millisecond, second, minute, hour, day, week,
|
---|
9 | * month, year.
|
---|
10 | *
|
---|
11 | * @version $Revision: 1.1 $
|
---|
12 | */
|
---|
13 | public class TimeUnit extends EnumeratedAttribute {
|
---|
14 |
|
---|
15 | public final static String MILLISECOND = "millisecond";
|
---|
16 | public final static String SECOND = "second";
|
---|
17 | public final static String MINUTE = "minute";
|
---|
18 | public final static String HOUR = "hour";
|
---|
19 | public final static String DAY = "day";
|
---|
20 | public final static String WEEK = "week";
|
---|
21 | public final static String MONTH = "month";
|
---|
22 | public final static String YEAR = "year";
|
---|
23 |
|
---|
24 | /** static unit objects, for use as sensible defaults */
|
---|
25 | public final static TimeUnit MILLISECOND_UNIT = new TimeUnit(MILLISECOND);
|
---|
26 | public final static TimeUnit SECOND_UNIT = new TimeUnit(SECOND);
|
---|
27 | public final static TimeUnit MINUTE_UNIT = new TimeUnit(MINUTE);
|
---|
28 | public final static TimeUnit HOUR_UNIT = new TimeUnit(HOUR);
|
---|
29 | public final static TimeUnit DAY_UNIT = new TimeUnit(DAY);
|
---|
30 | public final static TimeUnit WEEK_UNIT = new TimeUnit(WEEK);
|
---|
31 |
|
---|
32 | private final static String[] units = {MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR};
|
---|
33 |
|
---|
34 | private Hashtable timeTable = new Hashtable();
|
---|
35 |
|
---|
36 | /** Constructor for TimeUnit */
|
---|
37 | public TimeUnit() {
|
---|
38 | timeTable.put(MILLISECOND, new Long(1L));
|
---|
39 | timeTable.put(SECOND, new Long(1000L));
|
---|
40 | timeTable.put(MINUTE, new Long(1000L * 60L));
|
---|
41 | timeTable.put(HOUR, new Long(1000L * 60L * 60L));
|
---|
42 | timeTable.put(DAY, new Long(1000L * 60L * 60L * 24L));
|
---|
43 | timeTable.put(WEEK, new Long(1000L * 60L * 60L * 24L * 7L));
|
---|
44 |
|
---|
45 | // calculate month and year based on the current date
|
---|
46 | Calendar now = Calendar.getInstance();
|
---|
47 | long now_ms = now.getTimeInMillis();
|
---|
48 |
|
---|
49 | Calendar later = Calendar.getInstance();
|
---|
50 | later.add(Calendar.MONTH, 1);
|
---|
51 | timeTable.put(MONTH, new Long(later.getTimeInMillis() - now_ms));
|
---|
52 |
|
---|
53 | later.add(Calendar.MONTH, -1);
|
---|
54 | later.add(Calendar.YEAR, 1);
|
---|
55 | timeTable.put(YEAR, new Long(later.getTimeInMillis() - now_ms));
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * private constructor used for static construction of TimeUnit objects.
|
---|
60 | *
|
---|
61 | * @param value String representing the value.
|
---|
62 | */
|
---|
63 | private TimeUnit(String value) {
|
---|
64 | this();
|
---|
65 | setValueProgrammatically(value);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * set the inner value programmatically.
|
---|
70 | *
|
---|
71 | * @param value to set
|
---|
72 | */
|
---|
73 | protected void setValueProgrammatically(String value) {
|
---|
74 | this.value = value;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Gets the multiplier attribute of the TimeUnit object
|
---|
79 | *
|
---|
80 | * @return The multiplier value
|
---|
81 | */
|
---|
82 | public long getMultiplier() {
|
---|
83 | String key = getValue().toLowerCase();
|
---|
84 | Long l = (Long) timeTable.get(key);
|
---|
85 | return l.longValue();
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Gets the values attribute of the TimeUnit object
|
---|
90 | *
|
---|
91 | * @return The values value
|
---|
92 | */
|
---|
93 | public String[] getValues() {
|
---|
94 | return units;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * convert the time in the current unit, to millis
|
---|
99 | *
|
---|
100 | * @param numberOfUnits long expressed in the current objects units
|
---|
101 | * @return long representing the value in millis
|
---|
102 | */
|
---|
103 | public long toMillis(long numberOfUnits) {
|
---|
104 | return numberOfUnits * getMultiplier();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|