001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.text;
017
018 /**
019 * A text formatter that formats time periods (durations.)
020 *
021 * @author Keith Donald
022 */
023 public abstract class TimeFormat {
024 private static final TimeFormat daysInstance = new TimeFormat() {
025 public String format(long time) {
026 int totalSeconds = (int)time / 1000;
027 int days = totalSeconds / 86400;
028 int dSecs = days * 86400;
029
030 int hours = (totalSeconds - dSecs) / 3600;
031 int hSecs = hours * 3600;
032
033 int minutes = (totalSeconds - dSecs - hSecs) / 60;
034 int mSecs = minutes * 60;
035
036 int seconds = totalSeconds - dSecs - hSecs - mSecs;
037
038 StringBuffer buf = new StringBuffer();
039 if (days > 0) {
040 buf.append(days);
041 buf.append("d ");
042 }
043 buf.append(hours);
044 buf.append("h ");
045 buf.append(minutes);
046 buf.append("m ");
047 buf.append(seconds);
048 buf.append("s");
049 return buf.toString();
050 }
051 };
052
053 private static final TimeFormat millisecondsInstance = new TimeFormat() {
054 public String format(long time) {
055 int totalSeconds = (int)time / 1000;
056 int days = totalSeconds / 86400;
057 int dSecs = days * 86400;
058
059 int hours = (totalSeconds - dSecs) / 3600;
060 int hSecs = hours * 3600;
061
062 int minutes = (totalSeconds - dSecs - hSecs) / 60;
063 int mSecs = minutes * 60;
064
065 int seconds = totalSeconds - dSecs - hSecs - mSecs;
066
067 long milliseconds = time - dSecs * 1000 - hSecs * 1000 - mSecs * 1000 - seconds * 1000;
068
069 StringBuffer buf = new StringBuffer();
070 if (days > 0) {
071 buf.append(days);
072 buf.append("d ");
073 }
074 if (hours > 0) {
075 buf.append(hours);
076 buf.append("h ");
077 }
078 if (minutes > 0) {
079 buf.append(minutes);
080 buf.append("m ");
081 }
082 if (seconds > 0) {
083 buf.append(seconds);
084 buf.append("s ");
085 }
086 if (milliseconds > 0) {
087 buf.append(milliseconds);
088 buf.append("ms");
089 }
090 if (buf.length() == 0)
091 return "0";
092
093 return buf.toString();
094 }
095 };
096
097 /**
098 * Returns a standard TimeFormat that formats using days, hours, minutes,
099 * and seconds.
100 * <p>
101 * The format looks like: 5d 23h 12m 52s.
102 *
103 * @return The daysInstance TimeFormatter.
104 */
105 public static synchronized TimeFormat getDaysInstance() {
106 return daysInstance;
107 }
108
109 /**
110 * Returns a TimeFormat that formats using days, hours, minutes, seconds,
111 * and milliseconds.
112 * <p>
113 * The format looks like: 5d 23h 12m 52s 10ms.
114 *
115 * @return The millisecondsInstance TimeFormatter.
116 */
117 public static synchronized TimeFormat getMillisecondsInstance() {
118 return millisecondsInstance;
119 }
120
121 /**
122 * Returns a formatted time string for the specified time period.
123 *
124 * @param time
125 * the time period.
126 * @return The formatted time string.
127 */
128 public abstract String format(long time);
129
130 }