1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.webdav.util;
14
15
16
17
18
19
20 public class Timeout {
21
22
23 public static final Timeout INFINITE = new Timeout(-1);
24
25
26 protected int seconds;
27
28
29
30
31
32 public Timeout(int timeout) {
33 this.seconds = timeout;
34 }
35
36
37
38
39
40 public String asString() {
41 if (seconds == -1) {
42 return "Infinite";
43 } else {
44 return "Second-" + seconds;
45 }
46 }
47
48
49
50
51
52 public String toString() {
53 if (seconds == -1) {
54 return "Timeout[Infinite]";
55 } else {
56 return "Timeout[seconds-" + seconds + "]";
57 }
58 }
59
60 public long calculateValidity() {
61 long now = System.currentTimeMillis();
62 now = now + seconds * 1000;
63 return now;
64 }
65
66 public static Timeout parse(String s) {
67 if ("Infinite".equals(s)) {
68 return INFINITE;
69 }
70 if (s.startsWith("Second-") && (s.length() > 7)) {
71 try {
72 int timeout = Integer.parseInt(s.substring(7));
73 return new Timeout(timeout);
74 } catch (NumberFormatException ignore) {
75 }
76 }
77 return null;
78 }
79 }