24
24
import org .metafacture .framework .annotations .Out ;
25
25
import org .metafacture .framework .helpers .DefaultObjectPipe ;
26
26
27
+ import java .util .concurrent .TimeUnit ;
27
28
28
29
/**
29
- * Lets the process between objects sleep for a specific ms .
30
+ * Lets the process sleep for a specific amount of time between objects .
30
31
*
31
32
* @param <T> object type
32
33
* @author Tobias Bülte
33
34
*/
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 ." )
35
36
@ In (Object .class )
36
37
@ Out (Object .class )
37
38
@ FluxCommand ("sleep" )
38
39
public final class ObjectSleeper <T > extends DefaultObjectPipe <T , ObjectReceiver <T >> {
39
40
41
+ public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit .MILLISECONDS ;
40
42
public static final long DEFAULT_SLEEP_TIME = 1000 ;
41
43
44
+ private static final String TIME_UNIT_SUFFIX = "S" ;
45
+
46
+ private TimeUnit timeUnit = DEFAULT_TIME_UNIT ;
42
47
private long sleepTime = DEFAULT_SLEEP_TIME ;
43
48
44
49
/**
@@ -48,29 +53,75 @@ public ObjectSleeper() {
48
53
}
49
54
50
55
/**
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}).
52
58
*
53
59
* @param sleepTime the time to sleep
54
60
*/
55
61
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 ) {
56
73
this .sleepTime = sleepTime ;
57
74
}
58
75
59
76
/**
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}).
61
79
*
62
80
* @return the time to sleep
63
81
*/
64
82
public long getSleepTime () {
65
83
return sleepTime ;
66
84
}
67
85
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
+
68
119
/**
69
120
* Sleeps for the specified amount of time.
70
121
*/
71
122
public void sleep () {
72
123
try {
73
- Thread .sleep (sleepTime );
124
+ timeUnit .sleep (sleepTime );
74
125
}
75
126
catch (final InterruptedException e ) {
76
127
Thread .currentThread ().interrupt ();
0 commit comments