1 | package genius.core;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 |
|
---|
5 | import javax.xml.bind.annotation.XmlAccessType;
|
---|
6 | import javax.xml.bind.annotation.XmlAccessorType;
|
---|
7 | import javax.xml.bind.annotation.XmlElement;
|
---|
8 | import javax.xml.bind.annotation.XmlRootElement;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Contains the deadline - either rounds based or time based.
|
---|
12 | * <p>
|
---|
13 | * Deadline is a final object and can be serialized to xml. Immutable.
|
---|
14 | *
|
---|
15 | * @author W.Pasman
|
---|
16 | */
|
---|
17 | @SuppressWarnings("serial")
|
---|
18 | @XmlRootElement
|
---|
19 | @XmlAccessorType(XmlAccessType.FIELD)
|
---|
20 | public class Deadline implements Serializable {
|
---|
21 |
|
---|
22 | @XmlElement
|
---|
23 | private final Integer value;
|
---|
24 | @XmlElement
|
---|
25 | private final DeadlineType type;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Default timeout in seconds; also used as the realtime timeout
|
---|
29 | * to abort the round-based negotiations.
|
---|
30 | */
|
---|
31 | private final static Integer DEFAULT_TIME_OUT = 60;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create default value.
|
---|
35 | */
|
---|
36 | public Deadline() {
|
---|
37 | value = DEFAULT_TIME_OUT;
|
---|
38 | type = DeadlineType.TIME;
|
---|
39 | };
|
---|
40 |
|
---|
41 | public Deadline(int val, DeadlineType tp) {
|
---|
42 | if (val <= 0) {
|
---|
43 | throw new IllegalArgumentException("value must be >0 but got " + val);
|
---|
44 | }
|
---|
45 | if (tp == null) {
|
---|
46 | throw new NullPointerException("type is null");
|
---|
47 | }
|
---|
48 | value = val;
|
---|
49 | type = tp;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @return the total value of the deadline (seconds or rounds)
|
---|
54 | */
|
---|
55 | public int getValue() {
|
---|
56 | return value;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | *
|
---|
61 | * @return the {@link DeadlineType} of this deadline
|
---|
62 | */
|
---|
63 | public DeadlineType getType() {
|
---|
64 | return type;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @return the default time-out for function calls in the agents
|
---|
69 | */
|
---|
70 | public Integer getDefaultTimeout() {
|
---|
71 | return DEFAULT_TIME_OUT;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public String toString() {
|
---|
75 | return "Deadline:" + valueString();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @return just the value of this deadline, eg "10s".
|
---|
80 | */
|
---|
81 | public String valueString() {
|
---|
82 | return value + type.units();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | *
|
---|
87 | * @return the time, or a default time time-out. This is needed to determine
|
---|
88 | * the time-out for code execution with {@link DeadlineType#ROUND}
|
---|
89 | * deadlines.
|
---|
90 | */
|
---|
91 | public int getTimeOrDefaultTimeout() {
|
---|
92 | if (type == DeadlineType.ROUND) {
|
---|
93 | return DEFAULT_TIME_OUT;
|
---|
94 | }
|
---|
95 | return value;
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public int hashCode() {
|
---|
101 | final int prime = 31;
|
---|
102 | int result = 1;
|
---|
103 | result = prime * result + ((type == null) ? 0 : type.hashCode());
|
---|
104 | result = prime * result + ((value == null) ? 0 : value.hashCode());
|
---|
105 | return result;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public boolean equals(Object obj) {
|
---|
110 | if (this == obj)
|
---|
111 | return true;
|
---|
112 | if (obj == null)
|
---|
113 | return false;
|
---|
114 | if (getClass() != obj.getClass())
|
---|
115 | return false;
|
---|
116 | Deadline other = (Deadline) obj;
|
---|
117 | if (type != other.type)
|
---|
118 | return false;
|
---|
119 | if (value == null) {
|
---|
120 | if (other.value != null)
|
---|
121 | return false;
|
---|
122 | } else if (!value.equals(other.value))
|
---|
123 | return false;
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | } |
---|