001: /**
002: * com.mckoi.util.TimeFrame 08 Jan 2000
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: import java.util.Date;
027: import java.text.StringCharacterIterator;
028:
029: /**
030: * An immutable object that represents a frame of time down to the
031: * accuracy of a millisecond.
032: * <p>
033: * This object wraps around a BigDecimal that represents the number of
034: * milliseconds it takes to pass through the period.
035: *
036: * @author Tobias Downer
037: */
038:
039: public class TimeFrame {
040:
041: private static final BigDecimal BD_ZERO = new BigDecimal(0);
042:
043: /**
044: * Formatting enum.
045: */
046: public static int WEEKS = 1, DAYS = 2, HOURS = 3, MINUTES = 4;
047:
048: /**
049: * A BigDecimal that represents the number of milliseconds the time frame
050: * represents.
051: */
052: private BigDecimal period;
053:
054: /**
055: * Constructs the TimeFrame for the given time.
056: */
057: public TimeFrame(BigDecimal period) {
058: this .period = period;
059: }
060:
061: /**
062: * Returns the number of milliseconds for the period of this time frame.
063: */
064: public BigDecimal getPeriod() {
065: return period;
066: }
067:
068: /**
069: * Returns true if this time frame represents no time.
070: */
071: public boolean isNoTime() {
072: return period.equals(BD_ZERO);
073: }
074:
075: /**
076: * Returns a Date that is the addition of this period of time to the given
077: * date.
078: */
079: public Date addToDate(Date date) {
080: return new Date(date.getTime() + period.longValue());
081: }
082:
083: /**
084: * Returns a string that represents this time frame formatted as a string.
085: * The period is formatted as short hand.
086: *
087: * @param format_type either WEEKS, HOURS, MINUTES
088: */
089: public String format(int format_type) {
090: return format(format_type, true);
091: }
092:
093: /**
094: * Returns a string that represents this time frame formatted as a string.
095: *
096: * @param format_type either WEEKS, HOURS, MINUTES
097: * @param shorthand if false then timeframe is formatted in long hand.
098: * 'ms' -> 'milliseconds'
099: */
100: public String format(int format_type, boolean shorthand) {
101: if (period == null) {
102: return "";
103: }
104: StringBuffer str = new StringBuffer();
105: double val = period.longValue();
106: if (format_type == WEEKS) {
107: GeneralFormatter.appendWeekType(str, val, shorthand);
108: } else if (format_type == DAYS) {
109: GeneralFormatter.appendDayType(str, val, shorthand);
110: } else if (format_type == HOURS) {
111: GeneralFormatter.appendHourType(str, val, shorthand);
112: } else if (format_type == MINUTES) {
113: GeneralFormatter.appendMinuteType(str, val, shorthand);
114: }
115: return str.toString();
116: }
117:
118: /**
119: * Parses the given String and returns a TimeFrame object that represents
120: * the date. This excepts strings such as:
121: * <p><pre>
122: * "3 wks 12 days", "5.4 days", "9d", "12 minutes", "24 mins", etc.
123: * </pre>
124: * <p>
125: * See 'GeneralParser' for more details.
126: */
127: public static TimeFrame parse(String str)
128: throws java.text.ParseException {
129: // The 'null' case.
130: if (str == null || str.equals("")) {
131: return null;
132: }
133:
134: BigDecimal period = GeneralParser
135: .parseTimeMeasure(new StringCharacterIterator(str));
136: return new TimeFrame(period);
137: }
138:
139: /**
140: * Returns true if the TimeFrame is equal to another.
141: */
142: public boolean equals(Object ob) {
143: TimeFrame tf = (TimeFrame) ob;
144: if (tf == null) {
145: return false;
146: }
147: return (this == tf || period.equals(tf.period));
148: }
149:
150: /**
151: * For Debugging.
152: */
153: public String toString() {
154: return format(WEEKS);
155: }
156:
157: }
|