File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\util\TimeDuration.java.html

1 package jenkins.util;
2
3 import org.apache.commons.beanutils.Converter;
4 import org.kohsuke.stapler.QueryParameter;
5
6 import java.util.concurrent.TimeUnit;
7
8 /**
9 * Represents a length of something, like "3 seconds"
10 *
11 * This supports parameter injection, such as via {@link QueryParameter}.
12 *
13 * @author Kohsuke Kawaguchi
14 * @since 1.489
15 */
16 public class TimeDuration {
17 private final long millis;
18
19 public TimeDuration(long millis) {
20 this.millis = millis;
21 }
22
23 public int getTime() {
24 return (int)millis;
25 }
26
27 public long getTimeInMillis() {
28 return millis;
29 }
30
31 public long as(TimeUnit t) {
32 return t.convert(millis,TimeUnit.MILLISECONDS);
33 }
34
35 public static TimeDuration fromString(String delay) {
36 if (delay==null)
37 return null;
38
39 try {
40 // TODO: more unit handling
41 if(delay.endsWith("sec")) delay=delay.substring(0,delay.length()-3);
42 if(delay.endsWith("secs")) delay=delay.substring(0,delay.length()-4);
43 return new TimeDuration(Long.parseLong(delay));
44 } catch (NumberFormatException e) {
45 throw new IllegalArgumentException("Invalid time duration value: "+delay);
46 }
47 }
48
49 public static class StaplerConverterImpl implements Converter {
50 public Object convert(Class type, Object value) {
51 if (value==null)
52 return null;
53 if (value instanceof String) {
54 return fromString((String) value);
55 }
56 throw new UnsupportedOperationException();
57 }
58 }
59 }
60