Skip to content

Commit 35f35e9

Browse files
committed
Add DurationUtils.get(String, TemporalUnit, long)
- Add DurationUtils.getMillis(String, long) - Add DurationUtils.getSeconds(String, long)
1 parent f626e53 commit 35f35e9

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ The <action> type attribute can be add,update,fix,remove.
9393
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DoubleRange.fit(double).</action>
9494
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IntegerRange.fit(int).</action>
9595
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LongRange.fit(long).</action>
96+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.get(String, TemporalUnit, long).</action>
97+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.getMillis(String, long).</action>
98+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.getSeconds(String, long).</action>
9699
<!-- UPDATE -->
97100
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump org.apache.commons:commons-text from 1.13.1 to 1.14.0.</action>
98101
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>

src/main/java/org/apache/commons/lang3/time/DurationUtils.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import java.time.Instant;
2222
import java.time.temporal.ChronoUnit;
2323
import java.time.temporal.Temporal;
24+
import java.time.temporal.TemporalUnit;
2425
import java.util.Objects;
2526
import java.util.concurrent.TimeUnit;
2627

2728
import org.apache.commons.lang3.LongRange;
2829
import org.apache.commons.lang3.ObjectUtils;
30+
import org.apache.commons.lang3.StringUtils;
2931
import org.apache.commons.lang3.function.FailableBiConsumer;
3032
import org.apache.commons.lang3.function.FailableConsumer;
3133
import org.apache.commons.lang3.function.FailableRunnable;
@@ -60,6 +62,35 @@ public static <T extends Throwable> void accept(final FailableBiConsumer<Long, I
6062
}
6163
}
6264

65+
/**
66+
* Gets a Duration of unit stored in the system property at the given key.
67+
*
68+
* @param key The property name.
69+
* @param unit The unit that the duration is measured in, not null.
70+
* @param def The default value in the given unit.
71+
* @return a Duration of seconds.
72+
* @since 3.19.0
73+
*/
74+
public static Duration get(final String key, final TemporalUnit unit, final long def) {
75+
return Duration.of(getLong(key, def), unit);
76+
}
77+
78+
private static long getLong(final String key, final long def) {
79+
return StringUtils.isEmpty(key) ? def : Long.getLong(key, def);
80+
}
81+
82+
/**
83+
* Gets a Duration of milliseconds stored in the system property at the given key.
84+
*
85+
* @param key The property name.
86+
* @param def The default value in milliseconds.
87+
* @return a Duration of milliseconds.
88+
* @since 3.19.0
89+
*/
90+
public static Duration getMillis(final String key, final long def) {
91+
return Duration.ofMillis(getLong(key, def));
92+
}
93+
6394
/**
6495
* Gets the nanosecond part of a Duration converted to milliseconds.
6596
* <p>
@@ -97,6 +128,18 @@ public static int getNanosOfMilli(final Duration duration) {
97128
return zeroIfNull(duration).getNano() % 1_000_000;
98129
}
99130

131+
/**
132+
* Gets a Duration of seconds stored in the system property at the given key.
133+
*
134+
* @param key The property name.
135+
* @param def The default value in seconds.
136+
* @return a Duration of seconds.
137+
* @since 3.19.0
138+
*/
139+
public static Duration getSeconds(final String key, final long def) {
140+
return Duration.ofSeconds(getLong(key, def));
141+
}
142+
100143
/**
101144
* Tests whether the given Duration is positive (duration &gt; 0).
102145
*

src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,48 @@
2626
import java.io.IOException;
2727
import java.time.Duration;
2828
import java.time.Instant;
29+
import java.time.temporal.ChronoUnit;
2930
import java.util.concurrent.TimeUnit;
3031

3132
import org.apache.commons.lang3.AbstractLangTest;
3233
import org.apache.commons.lang3.math.NumberUtils;
3334
import org.junit.jupiter.api.Test;
35+
import org.junitpioneer.jupiter.SetSystemProperty;
36+
import org.junitpioneer.jupiter.SetSystemProperty.SetSystemProperties;
3437

3538
/**
3639
* Tests {@link DurationUtils}.
3740
*/
3841
class DurationUtilsTest extends AbstractLangTest {
3942

43+
@Test
44+
@SetSystemProperties({
45+
@SetSystemProperty(key = "Seconds1", value = "1"),
46+
@SetSystemProperty(key = "Seconds2", value = "9223372036854775807") }) // Long.MAX_VALUE
47+
void testGet() {
48+
// ChronoUnit.SECONDS
49+
assertEquals(Duration.ofSeconds(0), DurationUtils.get(null, ChronoUnit.SECONDS, 0));
50+
assertEquals(Duration.ofSeconds(0), DurationUtils.get("", ChronoUnit.SECONDS, 0));
51+
assertEquals(Duration.ofSeconds(1), DurationUtils.get("Seconds1", ChronoUnit.SECONDS, 0));
52+
assertEquals(Duration.ofSeconds(Long.MAX_VALUE), DurationUtils.get("Seconds2", ChronoUnit.SECONDS, 0));
53+
// ChronoUnit.MILLIS
54+
assertEquals(Duration.ofMillis(0), DurationUtils.get(null, ChronoUnit.MILLIS, 0));
55+
assertEquals(Duration.ofMillis(0), DurationUtils.get("", ChronoUnit.MILLIS, 0));
56+
assertEquals(Duration.ofMillis(1), DurationUtils.get("Seconds1", ChronoUnit.MILLIS, 0));
57+
assertEquals(Duration.ofMillis(Long.MAX_VALUE), DurationUtils.get("Seconds2", ChronoUnit.MILLIS, 0));
58+
}
59+
60+
@Test
61+
@SetSystemProperties({
62+
@SetSystemProperty(key = "Seconds1", value = "1"),
63+
@SetSystemProperty(key = "Seconds2", value = "9223372036854775807") }) // Long.MAX_VALUE
64+
void testGetMilliseconds() {
65+
assertEquals(Duration.ofMillis(0), DurationUtils.getMillis(null, 0));
66+
assertEquals(Duration.ofMillis(0), DurationUtils.getMillis("", 0));
67+
assertEquals(Duration.ofMillis(1), DurationUtils.getMillis("Seconds1", 0));
68+
assertEquals(Duration.ofMillis(Long.MAX_VALUE), DurationUtils.getMillis("Seconds2", 0));
69+
}
70+
4071
@Test
4172
void testGetNanosOfMiili() {
4273
assertEquals(0, DurationUtils.getNanosOfMiili(null));
@@ -65,6 +96,17 @@ void testGetNanosOfMilli() {
6596
assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_001)));
6697
}
6798

99+
@Test
100+
@SetSystemProperties({
101+
@SetSystemProperty(key = "Seconds1", value = "1"),
102+
@SetSystemProperty(key = "Seconds2", value = "9223372036854775807") }) // Long.MAX_VALUE
103+
void testGetSeconds() {
104+
assertEquals(Duration.ofSeconds(0), DurationUtils.getSeconds(null, 0));
105+
assertEquals(Duration.ofSeconds(0), DurationUtils.getSeconds("", 0));
106+
assertEquals(Duration.ofSeconds(1), DurationUtils.getSeconds("Seconds1", 0));
107+
assertEquals(Duration.ofSeconds(Long.MAX_VALUE), DurationUtils.getSeconds("Seconds2", 0));
108+
}
109+
68110
@Test
69111
void testIsPositive() {
70112
assertFalse(DurationUtils.isPositive(Duration.ZERO));

0 commit comments

Comments
 (0)