LANG-1003: DurationFormatUtils are not able to handle negative duration/periods. Document new behavior for negative inputs.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1592324 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2014-05-04 08:49:27 +00:00
parent 2486300548
commit 4b74c38562
1 changed files with 10 additions and 2 deletions

View File

@ -77,6 +77,7 @@ public class DurationFormatUtils {
* *
* @param durationMillis the duration to format * @param durationMillis the duration to format
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if durationMillis is negative
*/ */
public static String formatDurationHMS(final long durationMillis) { public static String formatDurationHMS(final long durationMillis) {
return formatDuration(durationMillis, "H:mm:ss.SSS"); return formatDuration(durationMillis, "H:mm:ss.SSS");
@ -92,6 +93,7 @@ public class DurationFormatUtils {
* *
* @param durationMillis the duration to format * @param durationMillis the duration to format
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if durationMillis is negative
*/ */
public static String formatDurationISO(final long durationMillis) { public static String formatDurationISO(final long durationMillis) {
return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false); return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false);
@ -106,6 +108,7 @@ public class DurationFormatUtils {
* @param durationMillis the duration to format * @param durationMillis the duration to format
* @param format the way in which to format the duration, not null * @param format the way in which to format the duration, not null
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if durationMillis is negative
*/ */
public static String formatDuration(final long durationMillis, final String format) { public static String formatDuration(final long durationMillis, final String format) {
return formatDuration(durationMillis, format, true); return formatDuration(durationMillis, format, true);
@ -122,10 +125,11 @@ public class DurationFormatUtils {
* @param format the way in which to format the duration, not null * @param format the way in which to format the duration, not null
* @param padWithZeros whether to pad the left hand side of numbers with 0's * @param padWithZeros whether to pad the left hand side of numbers with 0's
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if durationMillis is negative
*/ */
public static String formatDuration(final long durationMillis, final String format, final boolean padWithZeros) { public static String formatDuration(final long durationMillis, final String format, final boolean padWithZeros) {
if(durationMillis < 0) { if(durationMillis < 0) {
throw new IllegalArgumentException("Duration must not be less than 0"); throw new IllegalArgumentException("durationMillis must not be negative");
} }
final Token[] tokens = lexx(format); final Token[] tokens = lexx(format);
@ -166,6 +170,7 @@ public class DurationFormatUtils {
* @param suppressLeadingZeroElements suppresses leading 0 elements * @param suppressLeadingZeroElements suppresses leading 0 elements
* @param suppressTrailingZeroElements suppresses trailing 0 elements * @param suppressTrailingZeroElements suppresses trailing 0 elements
* @return the formatted text in days/hours/minutes/seconds, not null * @return the formatted text in days/hours/minutes/seconds, not null
* @throws java.lang.IllegalArgumentException if durationMillis is negative
*/ */
public static String formatDurationWords( public static String formatDurationWords(
final long durationMillis, final long durationMillis,
@ -229,6 +234,7 @@ public class DurationFormatUtils {
* @param startMillis the start of the duration to format * @param startMillis the start of the duration to format
* @param endMillis the end of the duration to format * @param endMillis the end of the duration to format
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if startMillis is greater than endMillis
*/ */
public static String formatPeriodISO(final long startMillis, final long endMillis) { public static String formatPeriodISO(final long startMillis, final long endMillis) {
return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault()); return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault());
@ -242,6 +248,7 @@ public class DurationFormatUtils {
* @param endMillis the end of the duration * @param endMillis the end of the duration
* @param format the way in which to format the duration, not null * @param format the way in which to format the duration, not null
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if startMillis is greater than endMillis
*/ */
public static String formatPeriod(final long startMillis, final long endMillis, final String format) { public static String formatPeriod(final long startMillis, final long endMillis, final String format) {
return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault()); return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault());
@ -269,11 +276,12 @@ public class DurationFormatUtils {
* @param padWithZeros whether to pad the left hand side of numbers with 0's * @param padWithZeros whether to pad the left hand side of numbers with 0's
* @param timezone the millis are defined in * @param timezone the millis are defined in
* @return the formatted duration, not null * @return the formatted duration, not null
* @throws java.lang.IllegalArgumentException if startMillis is greater than endMillis
*/ */
public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros, public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros,
final TimeZone timezone) { final TimeZone timezone) {
if(startMillis > endMillis) { if(startMillis > endMillis) {
throw new IllegalArgumentException("endMillis must be greater than startMillis"); throw new IllegalArgumentException("startMillis must not be greater than endMillis");
} }
// Used to optimise for differences under 28 days and // Used to optimise for differences under 28 days and