001: /**
002: * com.mckoi.util.GeneralFormatter 26 Dec 1999
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: import java.math.BigDecimal;
026:
027: /**
028: * This class provides several static convenience functions for formatting
029: * various types of information such as a time frame.
030: * <p>
031: * @author Tobias Downer
032: */
033:
034: public class GeneralFormatter {
035:
036: /**
037: * These statics represent switches for the visual formatting of the
038: * time frame. It is desirable to have the time frame represented in
039: * different denominations.
040: */
041: public static final int MAX_WEEKS = 0;
042: public static final int MAX_DAYS = 1;
043: public static final int MAX_HOURS = 2;
044: public static final int MAX_MINUTES = 3;
045: public static final int MAX_SECONDS = 4;
046: public static final int MAX_MILLISECONDS = 5;
047:
048: /**
049: * These statics represent some information about how many milliseconds are
050: * in various measures of time.
051: */
052: private static final long MILLIS_IN_WEEK = 7 * 24 * 60 * 60 * 1000;
053: private static final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
054: private static final long MILLIS_IN_HOUR = 60 * 60 * 1000;
055: private static final long MILLIS_IN_MINUTE = 60 * 1000;
056: private static final long MILLIS_IN_SECOND = 1000;
057:
058: /**
059: * Appends a frame of time onto the given StringBuffer. This is used to
060: * construct a string representing the current time frame.
061: */
062: private static void appendFrame(StringBuffer str, double num,
063: String frame, boolean do_round, boolean append_plural_s) {
064: // Should we round the number? (remove the decimal part)
065: if (do_round) {
066: num = (long) num;
067: }
068: // Don't bother printing 0 length time frames
069: if (num != 0) {
070: str.append(new BigDecimal(num));
071: str.append(' ');
072: str.append(frame);
073: // Append 's' on the end to represent plurals for all except 1 unit
074: if (num != 1 && append_plural_s) {
075: str.append('s');
076: }
077: str.append(' ');
078: }
079: }
080:
081: /**
082: * Appends time frame representation information into the given StringBuffer
083: * for various types of visual time frame formats.
084: */
085: public static void appendWeekType(StringBuffer str, double total,
086: boolean shorthand) {
087: double num;
088: // Total number of weeks
089: num = total / MILLIS_IN_WEEK;
090: appendFrame(str, num, "week", true, true);
091: // Total number of days
092: num = (total % MILLIS_IN_WEEK) / MILLIS_IN_DAY;
093: appendFrame(str, num, "day", true, true);
094: // Total number of hours
095: num = (total % MILLIS_IN_DAY) / MILLIS_IN_HOUR;
096: appendFrame(str, num, "hr", true, true);
097: // Total number of minutes
098: num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE;
099: appendFrame(str, num, "min", true, true);
100: // Total number of seconds
101: num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
102: appendFrame(str, num, "sec", true, false);
103: // Total number of milliseconds
104: num = total % MILLIS_IN_SECOND;
105: appendFrame(str, num, "ms", true, false);
106: }
107:
108: public static void appendDayType(StringBuffer str, double total,
109: boolean shorthand) {
110: double num;
111: // Total number of days
112: num = total / MILLIS_IN_DAY;
113: appendFrame(str, num, "day", true, true);
114: // Total number of hours
115: num = (total % MILLIS_IN_DAY) / MILLIS_IN_HOUR;
116: appendFrame(str, num, "hr", true, true);
117: // Total number of minutes
118: num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE;
119: appendFrame(str, num, "min", true, true);
120: // Total number of seconds
121: num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
122: appendFrame(str, num, "sec", true, false);
123: // Total number of milliseconds
124: num = total % MILLIS_IN_SECOND;
125: appendFrame(str, num, "ms", true, false);
126: }
127:
128: public static void appendHourType(StringBuffer str, double total,
129: boolean shorthand) {
130: double num;
131: // Total number of hours
132: num = total / MILLIS_IN_HOUR;
133: appendFrame(str, num, "hr", true, true);
134: // Total number of minutes
135: num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE;
136: appendFrame(str, num, "min", true, true);
137: // Total number of seconds
138: num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
139: appendFrame(str, num, "sec", true, false);
140: // Total number of milliseconds
141: num = total % MILLIS_IN_SECOND;
142: appendFrame(str, num, "ms", true, false);
143: }
144:
145: public static void appendMinuteType(StringBuffer str, double total,
146: boolean shorthand) {
147: double num;
148: // Total number of minutes
149: num = total / MILLIS_IN_MINUTE;
150: appendFrame(str, num, "min", true, true);
151: // Total number of seconds
152: num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND;
153: appendFrame(str, num, "sec", true, false);
154: // Total number of milliseconds
155: num = total % MILLIS_IN_SECOND;
156: appendFrame(str, num, "ms", true, false);
157: }
158:
159: }
|