KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.util.GeneralFormatter 26 Dec 1999
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
29 /**
30  * This class provides several static convenience functions for formatting
31  * various types of information such as a time frame.
32  * <p>
33  * @author Tobias Downer
34  */

35
36 public class GeneralFormatter {
37
38   /**
39    * These statics represent switches for the visual formatting of the
40    * time frame. It is desirable to have the time frame represented in
41    * different denominations.
42    */

43   public static final int MAX_WEEKS = 0;
44   public static final int MAX_DAYS = 1;
45   public static final int MAX_HOURS = 2;
46   public static final int MAX_MINUTES = 3;
47   public static final int MAX_SECONDS = 4;
48   public static final int MAX_MILLISECONDS = 5;
49
50   /**
51    * These statics represent some information about how many milliseconds are
52    * in various measures of time.
53    */

54   private static final long MILLIS_IN_WEEK = 7 * 24 * 60 * 60 * 1000;
55   private static final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
56   private static final long MILLIS_IN_HOUR = 60 * 60 * 1000;
57   private static final long MILLIS_IN_MINUTE = 60 * 1000;
58   private static final long MILLIS_IN_SECOND = 1000;
59
60   /**
61    * Appends a frame of time onto the given StringBuffer. This is used to
62    * construct a string representing the current time frame.
63    */

64   private static void appendFrame(StringBuffer JavaDoc str, double num, String JavaDoc frame,
65                                   boolean do_round, boolean append_plural_s) {
66     // Should we round the number? (remove the decimal part)
67
if (do_round) {
68       num = (long) num;
69     }
70     // Don't bother printing 0 length time frames
71
if (num != 0) {
72       str.append(new BigDecimal JavaDoc(num));
73       str.append(' ');
74       str.append(frame);
75       // Append 's' on the end to represent plurals for all except 1 unit
76
if (num != 1 && append_plural_s) {
77         str.append('s');
78       }
79       str.append(' ');
80     }
81   }
82
83   /**
84    * Appends time frame representation information into the given StringBuffer
85    * for various types of visual time frame formats.
86    */

87   public static void appendWeekType(StringBuffer JavaDoc str, double total,
88                                     boolean shorthand) {
89     double num;
90     // Total number of weeks
91
num = total / MILLIS_IN_WEEK;
92     appendFrame(str, num, "week", true, true);
93     // Total number of days
94
num = (total % MILLIS_IN_WEEK) / MILLIS_IN_DAY;
95     appendFrame(str, num, "day", true, true);
96     // Total number of hours
97
num = (total % MILLIS_IN_DAY) / MILLIS_IN_HOUR;
98     appendFrame(str, num, "hr", true, true);
99     // Total number of minutes
100
num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE;
101     appendFrame(str, num, "min", true, true);
102     // Total number of seconds
103
num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
104     appendFrame(str, num, "sec", true, false);
105     // Total number of milliseconds
106
num = total % MILLIS_IN_SECOND;
107     appendFrame(str, num, "ms", true, false);
108   }
109
110   public static void appendDayType(StringBuffer JavaDoc str, double total,
111                                    boolean shorthand) {
112     double num;
113     // Total number of days
114
num = total / MILLIS_IN_DAY;
115     appendFrame(str, num, "day", true, true);
116     // Total number of hours
117
num = (total % MILLIS_IN_DAY) / MILLIS_IN_HOUR;
118     appendFrame(str, num, "hr", true, true);
119     // Total number of minutes
120
num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE;
121     appendFrame(str, num, "min", true, true);
122     // Total number of seconds
123
num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
124     appendFrame(str, num, "sec", true, false);
125     // Total number of milliseconds
126
num = total % MILLIS_IN_SECOND;
127     appendFrame(str, num, "ms", true, false);
128   }
129
130   public static void appendHourType(StringBuffer JavaDoc str, double total,
131                                     boolean shorthand) {
132     double num;
133     // Total number of hours
134
num = total / MILLIS_IN_HOUR;
135     appendFrame(str, num, "hr", true, true);
136     // Total number of minutes
137
num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE;
138     appendFrame(str, num, "min", true, true);
139     // Total number of seconds
140
num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
141     appendFrame(str, num, "sec", true, false);
142     // Total number of milliseconds
143
num = total % MILLIS_IN_SECOND;
144     appendFrame(str, num, "ms", true, false);
145   }
146
147   public static void appendMinuteType(StringBuffer JavaDoc str, double total,
148                                       boolean shorthand) {
149     double num;
150     // Total number of minutes
151
num = total / MILLIS_IN_MINUTE;
152     appendFrame(str, num, "min", true, true);
153     // Total number of seconds
154
num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
155     appendFrame(str, num, "sec", true, false);
156     // Total number of milliseconds
157
num = total % MILLIS_IN_SECOND;
158     appendFrame(str, num, "ms", true, false);
159   }
160
161
162
163
164
165 }
166
Popular Tags