Skip to content

Commit 6c5338c

Browse files
committed
Add defaultValue to Dates.stringToDuration(), re #62
1 parent 144c4e7 commit 6c5338c

File tree

1 file changed

+90
-33
lines changed

1 file changed

+90
-33
lines changed

src/main/java/org/libj/util/Dates.java

+90-33
Original file line numberDiff line numberDiff line change
@@ -689,35 +689,7 @@ public static int currentTimeDays() {
689689
return (int)(System.currentTimeMillis() / Dates.MILLISECONDS_IN_DAY);
690690
}
691691

692-
/**
693-
* Returns the milliseconds duration representation of the specified string duration.
694-
* <p>
695-
* The duration format is expressed with the regular expression:
696-
* <pre>
697-
* -?(\d+w)?(\d+d)?(\d+h)?(\d+m)?(\d*(.\d{1,3})s)?
698-
* </pre>
699-
* With the following key for units:
700-
* <p>
701-
* @formatter:off
702-
* <table>
703-
* <caption>Unit Key</caption>
704-
* <tr><td><b>Unit</b></td><td><b>Description</b></td></tr>
705-
* <tr><td><code><b>w</b></code></td><td>Week</td></tr>
706-
* <tr><td><code><b>d</b></code></td><td>Day</td></tr>
707-
* <tr><td><code><b>h</b></code></td><td>Hour</td></tr>
708-
* <tr><td><code><b>m</b></code></td><td>Minute</td></tr>
709-
* <tr><td><code><b>s</b></code></td><td>Second</td></tr>
710-
* </table>
711-
* @formatter:on
712-
*
713-
* @implSpec The second unit duration supports 3 fractional decimal digits.
714-
* @param str The duration string.
715-
* @return The milliseconds duration representation of the specified string duration.
716-
* @throws NullPointerException If {@code str} is null.
717-
* @throws IllegalArgumentException If {@code str} does not match the regular expression format.
718-
* @see #durationToString(long)
719-
*/
720-
public static long stringToDuration(final String str) {
692+
private static long stringToDuration(final String str, final long defaultValue, final boolean hasDefaultValue) {
721693
long dur = 0;
722694

723695
long v = 0;
@@ -741,6 +713,8 @@ else if (d == 2)
741713
a = 10;
742714
else if (d == 3)
743715
a = 1;
716+
else if (hasDefaultValue)
717+
return defaultValue;
744718
else
745719
throw new IllegalArgumentException("Fractional values are only supported for 3 digits of second precision: " + str);
746720

@@ -752,29 +726,45 @@ else if (ch == 'm') {
752726
dur += v * MILLISECONDS_IN_MINUTE;
753727
v = 0;
754728
d = 0;
755-
if (dot)
729+
if (dot) {
730+
if (hasDefaultValue)
731+
return defaultValue;
732+
756733
throw new IllegalArgumentException("Fractional values are only supported for second precision: " + str);
734+
}
757735
}
758736
else if (ch == 'h') {
759737
dur += v * MILLISECONDS_IN_HOUR;
760738
v = 0;
761739
d = 0;
762-
if (dot)
740+
if (dot) {
741+
if (hasDefaultValue)
742+
return defaultValue;
743+
763744
throw new IllegalArgumentException("Fractional values are only supported for second precision: " + str);
745+
}
764746
}
765747
else if (ch == 'd') {
766748
dur += v * MILLISECONDS_IN_DAY;
767749
v = 0;
768750
d = 0;
769-
if (dot)
751+
if (dot) {
752+
if (hasDefaultValue)
753+
return defaultValue;
754+
770755
throw new IllegalArgumentException("Fractional values are only supported for second precision: " + str);
756+
}
771757
}
772758
else if (ch == 'w') {
773759
dur += v * MILLISECONDS_IN_WEEK;
774760
v = 0;
775761
d = 0;
776-
if (dot)
762+
if (dot) {
763+
if (hasDefaultValue)
764+
return defaultValue;
765+
777766
throw new IllegalArgumentException("Fractional values are only supported for second precision: " + str);
767+
}
778768
}
779769
else if ('0' <= ch && ch <= '9') {
780770
v *= 10;
@@ -785,6 +775,9 @@ else if (ch == '.') {
785775
dot = true;
786776
d = 0;
787777
}
778+
else if (hasDefaultValue) {
779+
return defaultValue;
780+
}
788781
else {
789782
throw new IllegalArgumentException("Unsupported character " + ch + " at position " + i + ": " + str);
790783
}
@@ -796,6 +789,70 @@ else if (ch == '.') {
796789
return isNegative ? -dur : dur;
797790
}
798791

792+
/**
793+
* Returns the milliseconds duration representation of the specified string duration.
794+
* <p>
795+
* The duration format is expressed with the regular expression:
796+
* <pre>
797+
* -?(\d+w)?(\d+d)?(\d+h)?(\d+m)?(\d*(.\d{1,3})s)?
798+
* </pre>
799+
* With the following key for units:
800+
* <p>
801+
* @formatter:off
802+
* <table>
803+
* <caption>Unit Key</caption>
804+
* <tr><td><b>Unit</b></td><td><b>Description</b></td></tr>
805+
* <tr><td><code><b>w</b></code></td><td>Week</td></tr>
806+
* <tr><td><code><b>d</b></code></td><td>Day</td></tr>
807+
* <tr><td><code><b>h</b></code></td><td>Hour</td></tr>
808+
* <tr><td><code><b>m</b></code></td><td>Minute</td></tr>
809+
* <tr><td><code><b>s</b></code></td><td>Second</td></tr>
810+
* </table>
811+
* @formatter:on
812+
*
813+
* @implSpec The second unit duration supports 3 fractional decimal digits.
814+
* @param str The duration string.
815+
* @param defaultValue The value to return if {@code str} cannot be parsed as a duration.
816+
* @return The milliseconds duration representation of the specified string duration.
817+
* @throws NullPointerException If {@code str} is null.
818+
* @see #durationToString(long)
819+
*/
820+
public static long stringToDuration(final String str, final long defaultValue) {
821+
return stringToDuration(str, defaultValue, true);
822+
}
823+
824+
/**
825+
* Returns the milliseconds duration representation of the specified string duration.
826+
* <p>
827+
* The duration format is expressed with the regular expression:
828+
* <pre>
829+
* -?(\d+w)?(\d+d)?(\d+h)?(\d+m)?(\d*(.\d{1,3})s)?
830+
* </pre>
831+
* With the following key for units:
832+
* <p>
833+
* @formatter:off
834+
* <table>
835+
* <caption>Unit Key</caption>
836+
* <tr><td><b>Unit</b></td><td><b>Description</b></td></tr>
837+
* <tr><td><code><b>w</b></code></td><td>Week</td></tr>
838+
* <tr><td><code><b>d</b></code></td><td>Day</td></tr>
839+
* <tr><td><code><b>h</b></code></td><td>Hour</td></tr>
840+
* <tr><td><code><b>m</b></code></td><td>Minute</td></tr>
841+
* <tr><td><code><b>s</b></code></td><td>Second</td></tr>
842+
* </table>
843+
* @formatter:on
844+
*
845+
* @implSpec The second unit duration supports 3 fractional decimal digits.
846+
* @param str The duration string.
847+
* @return The milliseconds duration representation of the specified string duration.
848+
* @throws NullPointerException If {@code str} is null.
849+
* @throws IllegalArgumentException If {@code str} does not match the regular expression format.
850+
* @see #durationToString(long)
851+
*/
852+
public static long stringToDuration(final String str) {
853+
return stringToDuration(str, 0, false);
854+
}
855+
799856
/**
800857
* Returns the string duration representation of the specified duration in milliseconds.
801858
* <p>

0 commit comments

Comments
 (0)