Skip to content

Commit 18b6ae0

Browse files
committed
Add support for time units in ObjectSleeper (similar to Catmandu). (#559)
See also https://metacpan.org/pod/Catmandu::Fix::sleep (defaults to `SECONDS`, doesn't support `NANOSECONDS` and `DAYS`).
1 parent f069a04 commit 18b6ae0

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

metafacture-flowcontrol/src/main/java/org/metafacture/flowcontrol/ObjectSleeper.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,26 @@
2424
import org.metafacture.framework.annotations.Out;
2525
import org.metafacture.framework.helpers.DefaultObjectPipe;
2626

27+
import java.util.concurrent.TimeUnit;
2728

2829
/**
29-
* Lets the process between objects sleep for a specific ms.
30+
* Lets the process sleep for a specific amount of time between objects.
3031
*
3132
* @param <T> object type
3233
* @author Tobias Bülte
3334
*/
34-
@Description("Lets the process between objects sleep for a specific ms.")
35+
@Description("Lets the process sleep for a specific amount of time between objects.")
3536
@In(Object.class)
3637
@Out(Object.class)
3738
@FluxCommand("sleep")
3839
public final class ObjectSleeper<T> extends DefaultObjectPipe<T, ObjectReceiver<T>> {
3940

41+
public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MILLISECONDS;
4042
public static final long DEFAULT_SLEEP_TIME = 1000;
4143

44+
private static final String TIME_UNIT_SUFFIX = "S";
45+
46+
private TimeUnit timeUnit = DEFAULT_TIME_UNIT;
4247
private long sleepTime = DEFAULT_SLEEP_TIME;
4348

4449
/**
@@ -48,29 +53,75 @@ public ObjectSleeper() {
4853
}
4954

5055
/**
51-
* Sets the time in ms for the sleep phase.
56+
* Sets the amount of time for the sleep phase (measured in {@link
57+
* #setTimeUnit time unit}).
5258
*
5359
* @param sleepTime the time to sleep
5460
*/
5561
public void setSleepTime(final int sleepTime) {
62+
// NOTE: ConfigurableClass.convertValue() doesn't support long.
63+
setSleepTime((long) sleepTime);
64+
}
65+
66+
/**
67+
* Sets the amount of time for the sleep phase (measured in {@link
68+
* #setTimeUnit time unit}).
69+
*
70+
* @param sleepTime the time to sleep
71+
*/
72+
public void setSleepTime(final long sleepTime) {
5673
this.sleepTime = sleepTime;
5774
}
5875

5976
/**
60-
* Gets the time in ms for the sleep phase.
77+
* Gets the amount of time for the sleep phase (measured in {@link
78+
* #setTimeUnit time unit}).
6179
*
6280
* @return the time to sleep
6381
*/
6482
public long getSleepTime() {
6583
return sleepTime;
6684
}
6785

86+
/**
87+
* Sets the time unit for the sleep phase. See {@link TimeUnit available
88+
* time units}, case-insensitive, trailing "s" optional.
89+
*
90+
* @param timeUnit the time unit
91+
*/
92+
public void setTimeUnit(final String timeUnit) {
93+
// NOTE: Adds NANOSECONDS and DAYS over Catmandu's supported time units.
94+
95+
final String timeUnitName = timeUnit.toUpperCase();
96+
final String timeUnitSuffix = timeUnitName.endsWith(TIME_UNIT_SUFFIX) ? "" : TIME_UNIT_SUFFIX;
97+
98+
setTimeUnit(TimeUnit.valueOf(timeUnitName + timeUnitSuffix));
99+
}
100+
101+
/**
102+
* Sets the time unit for the sleep phase.
103+
*
104+
* @param timeUnit the time unit
105+
*/
106+
public void setTimeUnit(final TimeUnit timeUnit) {
107+
this.timeUnit = timeUnit;
108+
}
109+
110+
/**
111+
* Gets the time unit for the sleep phase.
112+
*
113+
* @return the time unit
114+
*/
115+
public TimeUnit getTimeUnit() {
116+
return timeUnit;
117+
}
118+
68119
/**
69120
* Sleeps for the specified amount of time.
70121
*/
71122
public void sleep() {
72123
try {
73-
Thread.sleep(sleepTime);
124+
timeUnit.sleep(sleepTime);
74125
}
75126
catch (final InterruptedException e) {
76127
Thread.currentThread().interrupt();

0 commit comments

Comments
 (0)