KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > TimeFrame


1 /**
2  * com.mckoi.util.TimeFrame 08 Jan 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.math.BigDecimal JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.text.StringCharacterIterator JavaDoc;
30
31 /**
32  * An immutable object that represents a frame of time down to the
33  * accuracy of a millisecond.
34  * <p>
35  * This object wraps around a BigDecimal that represents the number of
36  * milliseconds it takes to pass through the period.
37  *
38  * @author Tobias Downer
39  */

40
41 public class TimeFrame {
42
43   private static final BigDecimal JavaDoc BD_ZERO = new BigDecimal JavaDoc(0);
44
45   /**
46    * Formatting enum.
47    */

48   public static int WEEKS = 1,
49                     DAYS = 2,
50                     HOURS = 3,
51                     MINUTES = 4;
52
53   /**
54    * A BigDecimal that represents the number of milliseconds the time frame
55    * represents.
56    */

57   private BigDecimal JavaDoc period;
58
59   /**
60    * Constructs the TimeFrame for the given time.
61    */

62   public TimeFrame(BigDecimal JavaDoc period) {
63     this.period = period;
64   }
65
66   /**
67    * Returns the number of milliseconds for the period of this time frame.
68    */

69   public BigDecimal JavaDoc getPeriod() {
70     return period;
71   }
72
73   /**
74    * Returns true if this time frame represents no time.
75    */

76   public boolean isNoTime() {
77     return period.equals(BD_ZERO);
78   }
79
80   /**
81    * Returns a Date that is the addition of this period of time to the given
82    * date.
83    */

84   public Date JavaDoc addToDate(Date JavaDoc date) {
85     return new Date JavaDoc(date.getTime() + period.longValue());
86   }
87
88   /**
89    * Returns a string that represents this time frame formatted as a string.
90    * The period is formatted as short hand.
91    *
92    * @param format_type either WEEKS, HOURS, MINUTES
93    */

94   public String JavaDoc format(int format_type) {
95     return format(format_type, true);
96   }
97
98   /**
99    * Returns a string that represents this time frame formatted as a string.
100    *
101    * @param format_type either WEEKS, HOURS, MINUTES
102    * @param shorthand if false then timeframe is formatted in long hand.
103    * 'ms' -> 'milliseconds'
104    */

105   public String JavaDoc format(int format_type, boolean shorthand) {
106     if (period == null) {
107       return "";
108     }
109     StringBuffer JavaDoc str = new StringBuffer JavaDoc();
110     double val = period.longValue();
111     if (format_type == WEEKS) {
112       GeneralFormatter.appendWeekType(str, val, shorthand);
113     }
114     else if (format_type == DAYS) {
115       GeneralFormatter.appendDayType(str, val, shorthand);
116     }
117     else if (format_type == HOURS) {
118       GeneralFormatter.appendHourType(str, val, shorthand);
119     }
120     else if (format_type == MINUTES) {
121       GeneralFormatter.appendMinuteType(str, val, shorthand);
122     }
123     return str.toString();
124   }
125
126   /**
127    * Parses the given String and returns a TimeFrame object that represents
128    * the date. This excepts strings such as:
129    * <p><pre>
130    * "3 wks 12 days", "5.4 days", "9d", "12 minutes", "24 mins", etc.
131    * </pre>
132    * <p>
133    * See 'GeneralParser' for more details.
134    */

135   public static TimeFrame parse(String JavaDoc str) throws java.text.ParseException JavaDoc {
136     // The 'null' case.
137
if (str == null || str.equals("")) {
138       return null;
139     }
140
141     BigDecimal JavaDoc period =
142              GeneralParser.parseTimeMeasure(new StringCharacterIterator JavaDoc(str));
143     return new TimeFrame(period);
144   }
145
146   /**
147    * Returns true if the TimeFrame is equal to another.
148    */

149   public boolean equals(Object JavaDoc ob) {
150     TimeFrame tf = (TimeFrame) ob;
151     if (tf == null) {
152       return false;
153     }
154     return (this == tf || period.equals(tf.period));
155   }
156
157   /**
158    * For Debugging.
159    */

160   public String JavaDoc toString() {
161     return format(WEEKS);
162   }
163
164 }
165
Popular Tags