KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > DurationFormat


1 /**
2  * $Id: DurationFormat.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.text.FieldPosition JavaDoc;
33 import java.text.Format JavaDoc;
34 import java.text.ParsePosition JavaDoc;
35 import java.util.Date JavaDoc;
36
37 /**
38  * Helper class that formats a duration (milliseconds since January 1, 1970) as a
39  * simple ASCII string: <code><i>X</i>hr <i>Y</i>min <i>Z</i>sec <i>00</i>ms</code>.
40  * Does not support parsing of input. Safe for concurrent use. Only generates
41  * output for non-zero fields.
42  *
43  * @since JWare/AntX 0.4 (copied from JWare/core 0.6)
44  * @author ssmc, &copy;1997-2002 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
45  * @version <span id="clsv">0.5</span>
46  * @.safety multiple
47  * @.group impl,helper
48  **/

49
50 public class DurationFormat extends Format JavaDoc
51 {
52     static final long MS_PER_HOUR = 60L /*min-hr*/ * 60L /*sec-min*/ * 1000L/*ms-sec*/;
53     static final long MS_PER_MIN = 60L /*sec-min*/ * 1000L/*ms-sec*/;
54     static final long MS_PER_SEC = 1000L/*ms-sec*/;
55
56     /**
57      * Used to mean zero (0) duration.
58      **/

59     public static final String JavaDoc NO_DURATION = "--";
60
61
62
63     /**
64      * Creates a standard Java Format adapter for duration formatting.
65      **/

66     public DurationFormat()
67     {
68         super();
69         SPC="";
70         hash_= getClass().getName().hashCode()+SPC.hashCode();
71     }
72
73
74
75     /**
76      * Creates a standard Java Format adapter for duration formatting.
77      **/

78     public DurationFormat(String JavaDoc spc)
79     {
80         super();
81         if (spc==null) { throw new IllegalArgumentException JavaDoc("non-null spacer"); }
82         SPC=spc;
83         hash_= getClass().getName().hashCode()+SPC.hashCode();
84     }
85
86
87
88     /**
89      * Formats a duration (milliseconds since January 1, 1970) as an ASCII
90      * string: <code><i>X</i>hr <i>Y</i>min <i>Z</i>sec <i>00</i>ms</code>.
91      * @param duration the duration to format
92      * @param inc_ms true if milliseconds should be included
93      * @param sb string buffer to be updated (non-null)
94      * @return the updated string buffer (never null)
95      **/

96     public StringBuffer JavaDoc format(long duration, boolean inc_ms, StringBuffer JavaDoc sb)
97     {
98         if (duration>0) {
99             int nf=0;
100             long field = duration / MS_PER_HOUR;
101             long ms_gone = field * MS_PER_HOUR;
102             if (field>0) {
103                 sb.append(field);
104                 sb.append("hr");
105                 nf++;
106             }
107             field = (duration - ms_gone) / MS_PER_MIN;
108             ms_gone += field * MS_PER_MIN;
109             if (field>0) {
110                 if (nf>0) {sb.append(SPC);}
111                 sb.append(field);
112                 sb.append("min");
113                 nf++;
114             }
115             field = (duration - ms_gone) / MS_PER_SEC;
116             ms_gone += field * MS_PER_SEC;
117             if (field>0) {
118                 if (nf>0) {sb.append(SPC);}
119                 sb.append(field);
120                 sb.append("sec");
121                 nf++;
122             }
123             field = duration - ms_gone;
124             if (inc_ms && field>0) {
125                 if (nf>0) {sb.append(SPC);}
126                 sb.append(field);
127                 sb.append("ms");
128                 nf++;
129             }
130         } else {
131             sb.append(NO_DURATION);
132         }
133         return sb;
134     }
135
136
137
138     /**
139      * Formats a duration (milliseconds since January 1, 1970) as an ASCII
140      * string: <code><i>X</i>hr <i>Y</i>min <i>Z</i>sec <i>00</i>ms</code>.
141      * @param duration the duration to format
142      * @param inc_ms true if milliseconds should be included
143      * @param w print writer used to output string (non-null)
144      **/

145     public void format(long duration, boolean inc_ms, PrintWriter JavaDoc w)
146     {
147         if (duration>0) {
148             int nf=0;
149             long field = duration / MS_PER_HOUR;
150             long ms_gone = field * MS_PER_HOUR;
151             if (field>0) {
152                 w.print(field);
153                 w.print("hr");
154                 nf++;
155             }
156             field = (duration - ms_gone) / MS_PER_MIN;
157             ms_gone += field * MS_PER_MIN;
158             if (field>0) {
159                 if (nf>0) {w.print(SPC);}
160                 w.print(field);
161                 w.print("min");
162                 nf++;
163             }
164             field = (duration - ms_gone) / MS_PER_SEC;
165             ms_gone += field * MS_PER_SEC;
166             if (field>0) {
167                 if (nf>0) {w.print(SPC);}
168                 w.print(field);
169                 w.print("sec");
170                 nf++;
171             }
172             field = duration - ms_gone;
173             if (inc_ms && field>0) {
174                 if (nf>0) {w.print(SPC);}
175                 w.print(field);
176                 w.print("ms");
177                 nf++;
178             }
179         } else {
180             w.print(NO_DURATION);
181         }
182     }
183
184
185
186     /**
187      * Convenience that allocates transient StringBuffer internally.
188      **/

189     public final String JavaDoc format(long duration, boolean inc_ms)
190     {
191         return format(duration,inc_ms,new StringBuffer JavaDoc(50)).toString();
192     }
193
194
195
196     /**
197      * If given object is a Number (any), formats the long value of this
198      * number.
199      * @throws IllegalArgumentException if object not a number or date
200      **/

201     public StringBuffer JavaDoc format(Object JavaDoc obj, StringBuffer JavaDoc sb, FieldPosition JavaDoc fp)
202     {
203         long duration= -1L;
204         if (obj instanceof Number JavaDoc) {
205             duration = ((Number JavaDoc)obj).longValue();
206         }
207         else if (obj instanceof Date JavaDoc) {
208             duration = ((Date JavaDoc)obj).getTime();
209         }
210         else {
211             throw new IllegalArgumentException JavaDoc("Unable to convert non-Number");
212         }
213         return format(duration,true,new StringBuffer JavaDoc(50));
214     }
215
216
217
218     /**
219      * Unsupported parse method; DurationFormat only works for output.
220      * @throws UnsupportedOperationException always
221      **/

222     public Object JavaDoc parseObject(String JavaDoc str, ParsePosition JavaDoc pp)
223     {
224         throw new UnsupportedOperationException JavaDoc();
225     }
226
227
228
229     /**
230      * Unsupported parse method; DurationFormat only works for output.
231      * @throws UnsupportedOperationException always
232      **/

233     public Object JavaDoc parseObject(String JavaDoc str)
234     {
235         throw new UnsupportedOperationException JavaDoc();
236     }
237
238
239
240     /**
241      * Determine if incoming object is equivalent DurationFormat.
242      **/

243     public boolean equals(Object JavaDoc o)
244     {
245         if (o==this) {
246             return true;
247         }
248         if (o==null) {
249             return false;
250         }
251         if (o.getClass()==getClass()) {
252             DurationFormat df = (DurationFormat)o;
253             return SPC.equals(df.SPC);
254         }
255         return false;
256     }
257
258
259
260     /**
261      * Returns hash value for this DurationFormat.
262      **/

263     public int hashCode()
264     {
265         return hash_;
266     }
267
268
269
270     /**
271      * VM-shareable singleton that separates fields with '&#46;' (period).
272      **/

273     public static final DurationFormat INSTANCE = new DurationFormat(".");
274
275
276
277     /**
278      * Standard API for retrieving VM-shareable singleton {@linkplain #INSTANCE}.
279      **/

280     public static final DurationFormat getInstance()
281     {
282         return INSTANCE;
283     }
284
285
286
287     private final String JavaDoc SPC;
288     private final int hash_;
289 }
290
291 /* end-of-DurationFormat.java */
292
Popular Tags