You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/10/22 16:44:19 UTC
svn commit: r707093 -
/geronimo/gshell/trunk/gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java
Author: jdillon
Date: Wed Oct 22 07:44:18 2008
New Revision: 707093
URL: http://svn.apache.org/viewvc?rev=707093&view=rev
Log:
Remove crap we ain't using
Modified:
geronimo/gshell/trunk/gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java
Modified: geronimo/gshell/trunk/gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java?rev=707093&r1=707092&r2=707093&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java (original)
+++ geronimo/gshell/trunk/gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java Wed Oct 22 07:44:18 2008
@@ -24,112 +24,21 @@
//
import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.TimeZone;
/**
- * <p>Duration formatting utilities and constants. The following table describes the tokens
- * used in the pattern language for formatting. </p>
- * <table border="1">
- * <tr><th>character</th><th>duration element</th></tr>
- * <tr><td>y</td><td>years</td></tr>
- * <tr><td>M</td><td>months</td></tr>
- * <tr><td>d</td><td>days</td></tr>
- * <tr><td>H</td><td>hours</td></tr>
- * <tr><td>m</td><td>minutes</td></tr>
- * <tr><td>s</td><td>seconds</td></tr>
- * <tr><td>S</td><td>milliseconds</td></tr>
- * </table>
- *
- * @author Apache Ant - DateUtils
- * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
- * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
- * @author Stephen Colebourne
- * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
- * @since 2.1
- * @version $Id$
+ * Utilities for formatting a {@link Duration}.
+ *
+ * @version $Rev$ $Date$
*/
-public class DurationFormatUtils
+class DurationFormatUtils
{
- /**
- * <p>DurationFormatUtils instances should NOT be constructed in standard programming.</p>
- *
- * <p>This constructor is public to permit tools that require a JavaBean instance
- * to operate.</p>
- */
- public DurationFormatUtils() {
- super();
- }
+ public DurationFormatUtils() {}
- /**
- * <p>Pattern used with <code>FastDateFormat</code> and <code>SimpleDateFormat</code>
- * for the ISO8601 period format used in durations.</p>
- *
- * @see java.text.SimpleDateFormat
- */
- public static final String ISO_EXTENDED_FORMAT_PATTERN = "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'";
-
- //-----------------------------------------------------------------------
- /**
- * <p>Formats the time gap as a string.</p>
- *
- * <p>The format used is ISO8601-like:
- * <i>H</i>:<i>m</i>:<i>s</i>.<i>S</i>.</p>
- *
- * @param durationMillis the duration to format
- * @return the time as a String
- */
public static String formatDurationHMS(long durationMillis) {
- return formatDuration(durationMillis, "H:mm:ss.SSS");
- }
-
- /**
- * <p>Formats the time gap as a string.</p>
- *
- * <p>The format used is the ISO8601 period format.</p>
- *
- * <p>This method formats durations using the days and lower fields of the
- * ISO format pattern, such as P7D6TH5M4.321S.</p>
- *
- * @param durationMillis the duration to format
- * @return the time as a String
- */
- public static String formatDurationISO(long durationMillis) {
- return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false);
+ return formatDuration(durationMillis, "H:mm:ss.SSS", true);
}
- /**
- * <p>Formats the time gap as a string, using the specified format, and padding with zeros and
- * using the default timezone.</p>
- *
- * <p>This method formats durations using the days and lower fields of the
- * format pattern. Months and larger are not used.</p>
- *
- * @param durationMillis the duration to format
- * @param format the way in which to format the duration
- * @return the time as a String
- */
- public static String formatDuration(long durationMillis, String format) {
- return formatDuration(durationMillis, format, true);
- }
-
- /**
- * <p>Formats the time gap as a string, using the specified format.
- * Padding the left hand side of numbers with zeroes is optional and
- * the timezone may be specified.</p>
- *
- * <p>This method formats durations using the days and lower fields of the
- * format pattern. Months and larger are not used.</p>
- *
- * @param durationMillis the duration to format
- * @param format the way in which to format the duration
- * @param padWithZeros whether to pad the left hand side of numbers with 0's
- * @return the time as a String
- */
- public static String formatDuration(long durationMillis, String format, boolean padWithZeros) {
-
+ private static String formatDuration(long durationMillis, String format, boolean padWithZeros) {
Token[] tokens = lexx(format);
int days = 0;
@@ -161,278 +70,7 @@
return format(tokens, 0, 0, days, hours, minutes, seconds, milliseconds, padWithZeros);
}
- // Copied from DateUtils;
-
- private static final long DateUtils_MILLIS_PER_SECOND = 1000;
-
- private static final long DateUtils_MILLIS_PER_MINUTE = 60 * DateUtils_MILLIS_PER_SECOND;
-
- private static final long DateUtils_MILLIS_PER_HOUR = 60 * DateUtils_MILLIS_PER_MINUTE;
-
- private static final long DateUtils_MILLIS_PER_DAY = 24 * DateUtils_MILLIS_PER_HOUR;
-
- /**
- * <p>Formats an elapsed time into a plurialization correct string.</p>
- *
- * <p>This method formats durations using the days and lower fields of the
- * format pattern. Months and larger are not used.</p>
- *
- * @param durationMillis the elapsed time to report in milliseconds
- * @param suppressLeadingZeroElements suppresses leading 0 elements
- * @param suppressTrailingZeroElements suppresses trailing 0 elements
- * @return the formatted text in days/hours/minutes/seconds
- */
- public static String formatDurationWords(
- long durationMillis,
- boolean suppressLeadingZeroElements,
- boolean suppressTrailingZeroElements) {
-
- // This method is generally replacable by the format method, but
- // there are a series of tweaks and special cases that require
- // trickery to replicate.
- String duration = formatDuration(durationMillis, "d' days 'H' hours 'm' minutes 's' seconds'");
- if (suppressLeadingZeroElements) {
- // this is a temporary marker on the front. Like ^ in regexp.
- duration = " " + duration;
- String tmp = replaceOnce(duration, " 0 days", "");
- if (tmp.length() != duration.length()) {
- duration = tmp;
- tmp = replaceOnce(duration, " 0 hours", "");
- if (tmp.length() != duration.length()) {
- duration = tmp;
- tmp = replaceOnce(duration, " 0 minutes", "");
- duration = tmp;
- if (tmp.length() != duration.length()) {
- duration = replaceOnce(tmp, " 0 seconds", "");
- }
- }
- }
- if (duration.length() != 0) {
- // strip the space off again
- duration = duration.substring(1);
- }
- }
- if (suppressTrailingZeroElements) {
- String tmp = replaceOnce(duration, " 0 seconds", "");
- if (tmp.length() != duration.length()) {
- duration = tmp;
- tmp = replaceOnce(duration, " 0 minutes", "");
- if (tmp.length() != duration.length()) {
- duration = tmp;
- tmp = replaceOnce(duration, " 0 hours", "");
- if (tmp.length() != duration.length()) {
- duration = replaceOnce(tmp, " 0 days", "");
- }
- }
- }
- }
- // handle plurals
- duration = " " + duration;
- duration = replaceOnce(duration, " 1 seconds", " 1 second");
- duration = replaceOnce(duration, " 1 minutes", " 1 minute");
- duration = replaceOnce(duration, " 1 hours", " 1 hour");
- duration = replaceOnce(duration, " 1 days", " 1 day");
- return duration.trim();
- }
-
- //-----------------------------------------------------------------------
- /**
- * <p>Formats the time gap as a string.</p>
- *
- * <p>The format used is the ISO8601 period format.</p>
- *
- * @param startMillis the start of the duration to format
- * @param endMillis the end of the duration to format
- * @return the time as a String
- */
- public static String formatPeriodISO(long startMillis, long endMillis) {
- return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault());
- }
-
- /**
- * <p>Formats the time gap as a string, using the specified format.
- * Padding the left hand side of numbers with zeroes is optional.
- *
- * @param startMillis the start of the duration
- * @param endMillis the end of the duration
- * @param format the way in which to format the duration
- * @return the time as a String
- */
- public static String formatPeriod(long startMillis, long endMillis, String format) {
- return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault());
- }
-
- /**
- * <p>Formats the time gap as a string, using the specified format.
- * Padding the left hand side of numbers with zeroes is optional and
- * the timezone may be specified. </p>
- *
- * <p>When calculating the difference between months/days, it chooses to
- * calculate months first. So when working out the number of months and
- * days between January 15th and March 10th, it choose 1 month and
- * 23 days gained by choosing January->February = 1 month and then
- * calculating days forwards, and not the 1 month and 26 days gained by
- * choosing March -> February = 1 month and then calculating days
- * backwards. </p>
- *
- * <p>For more control, the <a href="http://joda-time.sf.net/">Joda-Time</a>
- * library is recommended.</p>
- *
- * @param startMillis the start of the duration
- * @param endMillis the end of the duration
- * @param format the way in which to format the duration
- * @param padWithZeros whether to pad the left hand side of numbers with 0's
- * @param timezone the millis are defined in
- * @return the time as a String
- */
- public static String formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros,
- TimeZone timezone) {
-
- // Used to optimise for differences under 28 days and
- // called formatDuration(millis, format); however this did not work
- // over leap years.
- // T O D O: Compare performance to see if anything was lost by
- // losing this optimisation.
-
- Token[] tokens = lexx(format);
-
- // timezones get funky around 0, so normalizing everything to GMT
- // stops the hours being off
- Calendar start = Calendar.getInstance(timezone);
- start.setTime(new Date(startMillis));
- Calendar end = Calendar.getInstance(timezone);
- end.setTime(new Date(endMillis));
-
- // initial estimates
- int milliseconds = end.get(Calendar.MILLISECOND) - start.get(Calendar.MILLISECOND);
- int seconds = end.get(Calendar.SECOND) - start.get(Calendar.SECOND);
- int minutes = end.get(Calendar.MINUTE) - start.get(Calendar.MINUTE);
- int hours = end.get(Calendar.HOUR_OF_DAY) - start.get(Calendar.HOUR_OF_DAY);
- int days = end.get(Calendar.DAY_OF_MONTH) - start.get(Calendar.DAY_OF_MONTH);
- int months = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
- int years = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
-
- // each initial estimate is adjusted in case it is under 0
- while (milliseconds < 0) {
- milliseconds += 1000;
- seconds -= 1;
- }
- while (seconds < 0) {
- seconds += 60;
- minutes -= 1;
- }
- while (minutes < 0) {
- minutes += 60;
- hours -= 1;
- }
- while (hours < 0) {
- hours += 24;
- days -= 1;
- }
-
- if (Token.containsTokenWithValue(tokens, M)) {
- while (days < 0) {
- days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
- months -= 1;
- start.add(Calendar.MONTH, 1);
- }
-
- while (months < 0) {
- months += 12;
- years -= 1;
- }
-
- if (!Token.containsTokenWithValue(tokens, y) && years != 0) {
- while (years != 0) {
- months += 12 * years;
- years = 0;
- }
- }
- } else {
- // there are no M's in the format string
-
- if( !Token.containsTokenWithValue(tokens, y) ) {
- int target = end.get(Calendar.YEAR);
- if (months < 0) {
- // target is end-year -1
- target -= 1;
- }
-
- while ( (start.get(Calendar.YEAR) != target)) {
- days += start.getActualMaximum(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR);
-
- // Not sure I grok why this is needed, but the brutal tests show it is
- if(start instanceof GregorianCalendar) {
- if( (start.get(Calendar.MONTH) == Calendar.FEBRUARY) &&
- (start.get(Calendar.DAY_OF_MONTH) == 29 ) )
- {
- days += 1;
- }
- }
-
- start.add(Calendar.YEAR, 1);
-
- days += start.get(Calendar.DAY_OF_YEAR);
- }
-
- years = 0;
- }
-
- while( start.get(Calendar.MONTH) != end.get(Calendar.MONTH) ) {
- days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
- start.add(Calendar.MONTH, 1);
- }
-
- months = 0;
-
- while (days < 0) {
- days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
- months -= 1;
- start.add(Calendar.MONTH, 1);
- }
-
- }
-
- // The rest of this code adds in values that
- // aren't requested. This allows the user to ask for the
- // number of months and get the real count and not just 0->11.
-
- if (!Token.containsTokenWithValue(tokens, d)) {
- hours += 24 * days;
- days = 0;
- }
- if (!Token.containsTokenWithValue(tokens, H)) {
- minutes += 60 * hours;
- hours = 0;
- }
- if (!Token.containsTokenWithValue(tokens, m)) {
- seconds += 60 * minutes;
- minutes = 0;
- }
- if (!Token.containsTokenWithValue(tokens, s)) {
- milliseconds += 1000 * seconds;
- seconds = 0;
- }
-
- return format(tokens, years, months, days, hours, minutes, seconds, milliseconds, padWithZeros);
- }
-
- //-----------------------------------------------------------------------
- /**
- * <p>The internal method to do the formatting.</p>
- *
- * @param tokens the tokens
- * @param years the number of years
- * @param months the number of months
- * @param days the number of days
- * @param hours the number of hours
- * @param minutes the number of minutes
- * @param seconds the number of seconds
- * @param milliseconds the number of millis
- * @param padWithZeros whether to pad
- * @return the formatted string
- */
- static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, boolean padWithZeros) {
+ private static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, boolean padWithZeros) {
StringBuilder buffer = new StringBuilder();
boolean lastOutputSeconds = false;
int sz = tokens.length;
@@ -442,39 +80,41 @@
int count = token.getCount();
if (value instanceof StringBuilder) {
buffer.append(value.toString());
- } else {
+ }
+ else {
if (value == y) {
- buffer.append(padWithZeros ? leftPad(Integer.toString(years), count, "0") : Integer
- .toString(years));
+ buffer.append(padWithZeros ? leftPad(Integer.toString(years), count, "0") : Integer.toString(years));
lastOutputSeconds = false;
- } else if (value == M) {
- buffer.append(padWithZeros ? leftPad(Integer.toString(months), count, "0") : Integer
- .toString(months));
+ }
+ else if (value == M) {
+ buffer.append(padWithZeros ? leftPad(Integer.toString(months), count, "0") : Integer.toString(months));
lastOutputSeconds = false;
- } else if (value == d) {
- buffer.append(padWithZeros ? leftPad(Integer.toString(days), count, "0") : Integer
- .toString(days));
+ }
+ else if (value == d) {
+ buffer.append(padWithZeros ? leftPad(Integer.toString(days), count, "0") : Integer.toString(days));
lastOutputSeconds = false;
- } else if (value == H) {
- buffer.append(padWithZeros ? leftPad(Integer.toString(hours), count, "0") : Integer
- .toString(hours));
+ }
+ else if (value == H) {
+ buffer.append(padWithZeros ? leftPad(Integer.toString(hours), count, "0") : Integer.toString(hours));
lastOutputSeconds = false;
- } else if (value == m) {
- buffer.append(padWithZeros ? leftPad(Integer.toString(minutes), count, "0") : Integer
- .toString(minutes));
+ }
+ else if (value == m) {
+ buffer.append(padWithZeros ? leftPad(Integer.toString(minutes), count, "0") : Integer.toString(minutes));
lastOutputSeconds = false;
- } else if (value == s) {
- buffer.append(padWithZeros ? leftPad(Integer.toString(seconds), count, "0") : Integer
- .toString(seconds));
+ }
+ else if (value == s) {
+ buffer.append(padWithZeros ? leftPad(Integer.toString(seconds), count, "0") : Integer.toString(seconds));
lastOutputSeconds = true;
- } else if (value == S) {
+ }
+ else if (value == S) {
if (lastOutputSeconds) {
milliseconds += 1000;
String str = padWithZeros
? leftPad(Integer.toString(milliseconds), count, "0")
: Integer.toString(milliseconds);
buffer.append(str.substring(1));
- } else {
+ }
+ else {
buffer.append(padWithZeros
? leftPad(Integer.toString(milliseconds), count, "0")
: Integer.toString(milliseconds));
@@ -486,21 +126,15 @@
return buffer.toString();
}
- static final Object y = "y";
- static final Object M = "M";
- static final Object d = "d";
- static final Object H = "H";
- static final Object m = "m";
- static final Object s = "s";
- static final Object S = "S";
-
- /**
- * Parses a classic date format string into Tokens
- *
- * @param format to parse
- * @return Token[] of tokens
- */
- static Token[] lexx(String format) {
+ private static final Object y = "y";
+ private static final Object M = "M";
+ private static final Object d = "d";
+ private static final Object H = "H";
+ private static final Object m = "m";
+ private static final Object s = "s";
+ private static final Object S = "S";
+
+ private static Token[] lexx(String format) {
char[] array = format.toCharArray();
ArrayList<Token> list = new ArrayList<Token>(array.length);
@@ -515,37 +149,39 @@
continue;
}
Object value = null;
- switch(ch) {
+ switch (ch) {
// T O D O: Need to handle escaping of '
case '\'' :
- if(inLiteral) {
- buffer = null;
- inLiteral = false;
- } else {
- buffer = new StringBuilder();
- list.add(new Token(buffer));
- inLiteral = true;
- }
- break;
- case 'y' : value = y; break;
- case 'M' : value = M; break;
- case 'd' : value = d; break;
- case 'H' : value = H; break;
- case 'm' : value = m; break;
- case 's' : value = s; break;
- case 'S' : value = S; break;
+ if (inLiteral) {
+ buffer = null;
+ inLiteral = false;
+ }
+ else {
+ buffer = new StringBuilder();
+ list.add(new Token(buffer));
+ inLiteral = true;
+ }
+ break;
+ case 'y': value = y; break;
+ case 'M': value = M; break;
+ case 'd': value = d; break;
+ case 'H': value = H; break;
+ case 'm': value = m; break;
+ case 's': value = s; break;
+ case 'S': value = S; break;
default :
- if(buffer == null) {
- buffer = new StringBuilder();
- list.add(new Token(buffer));
- }
- buffer.append(ch);
+ if (buffer == null) {
+ buffer = new StringBuilder();
+ list.add(new Token(buffer));
+ }
+ buffer.append(ch);
}
- if(value != null) {
- if(previous != null && previous.getValue() == value) {
+ if (value != null) {
+ if (previous != null && previous.getValue() == value) {
previous.increment();
- } else {
+ }
+ else {
Token token = new Token(value);
list.add(token);
previous = token;
@@ -556,18 +192,8 @@
return list.toArray( new Token[list.size()] );
}
- /**
- * Element that is parsed from the format pattern.
- */
- static class Token {
-
- /**
- * Helper method to determine if a set of tokens contain a value
- *
- * @param tokens set to look in
- * @param value to look for
- * @return boolean <code>true</code> if contained
- */
+ private static class Token
+ {
static boolean containsTokenWithValue(Token[] tokens, Object value) {
int sz = tokens.length;
for (int i = 0; i < sz; i++) {
@@ -581,59 +207,28 @@
private Object value;
private int count;
- /**
- * Wraps a token around a value. A value would be something like a 'Y'.
- *
- * @param value to wrap
- */
Token(Object value) {
this.value = value;
this.count = 1;
}
- /**
- * Wraps a token around a repeated number of a value, for example it would
- * store 'yyyy' as a value for y and a count of 4.
- *
- * @param value to wrap
- * @param count to wrap
- */
Token(Object value, int count) {
this.value = value;
this.count = count;
}
- /**
- * Adds another one of the value
- */
void increment() {
count++;
}
- /**
- * Gets the current number of values represented
- *
- * @return int number of values represented
- */
int getCount() {
return count;
}
- /**
- * Gets the particular value this token represents.
- *
- * @return Object value
- */
Object getValue() {
return value;
}
- /**
- * Supports equality of this Token to another Token.
- *
- * @param obj2 Object to consider equality of
- * @return boolean <code>true</code> if equal
- */
public boolean equals(Object obj2) {
if (obj2 instanceof Token) {
Token tok2 = (Token) obj2;
@@ -645,31 +240,21 @@
}
if (this.value instanceof StringBuilder) {
return this.value.toString().equals(tok2.value.toString());
- } else if (this.value instanceof Number) {
+ }
+ else if (this.value instanceof Number) {
return this.value.equals(tok2.value);
- } else {
+ }
+ else {
return this.value == tok2.value;
}
}
return false;
}
- /**
- * Returns a hashcode for the token equal to the
- * hashcode for the token's value. Thus 'TT' and 'TTTT'
- * will have the same hashcode.
- *
- * @return The hashcode for the token
- */
public int hashCode() {
return this.value.hashCode();
}
- /**
- * Represents this token as a String.
- *
- * @return String representation of the token
- */
public String toString() {
return repeat(this.value.toString(), this.count);
}
@@ -679,29 +264,6 @@
// NOTE: Copied from plexus-utils StringUtils 1.5.5
//
- private static String replaceOnce(String text, String repl, String with) {
- return replace(text, repl, with, 1);
- }
-
- private static String replace(String text, String repl, String with, int max) {
- if ((text == null) || (repl == null) || (with == null) || (repl.length() == 0)) {
- return text;
- }
-
- StringBuilder buf = new StringBuilder(text.length());
- int start = 0, end = 0;
- while ((end = text.indexOf(repl, start)) != -1) {
- buf.append(text.substring(start, end)).append(with);
- start = end + repl.length();
-
- if (--max == 0) {
- break;
- }
- }
- buf.append(text.substring(start));
- return buf.toString();
- }
-
private static String repeat(String str, int repeat) {
StringBuilder buffer = new StringBuilder(repeat * str.length());
for (int i = 0; i < repeat; i++) {
@@ -710,10 +272,6 @@
return buffer.toString();
}
- private static String leftPad(String str, int size) {
- return leftPad(str, size, " ");
- }
-
private static String leftPad(String str, int size, String delim) {
size = (size - str.length()) / delim.length();
if (size > 0) {
@@ -721,4 +279,14 @@
}
return str;
}
+
+ // Copied from commons-lang DateUtils 2.3
+
+ private static final long DateUtils_MILLIS_PER_SECOND = 1000;
+
+ private static final long DateUtils_MILLIS_PER_MINUTE = 60 * DateUtils_MILLIS_PER_SECOND;
+
+ private static final long DateUtils_MILLIS_PER_HOUR = 60 * DateUtils_MILLIS_PER_MINUTE;
+
+ private static final long DateUtils_MILLIS_PER_DAY = 24 * DateUtils_MILLIS_PER_HOUR;
}