1
2
3
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8
9 import de.powerstat.validation.interfaces.IValueObject;
10
11
12
13
14
15
16
17
18
19
20
21 public final class Hour implements Comparable<Hour>, IValueObject
22 {
23
24
25
26 private static final String OVERFLOW = "Overflow";
27
28
29
30
31 private static final String UNDERFLOW = "Underflow";
32
33
34
35
36
37
38
39
40
41 private final int hour;
42
43
44
45
46
47
48
49
50 private Hour(final int hour)
51 {
52 super();
53 if ((hour < 0) || (hour > 23))
54 {
55 throw new IndexOutOfBoundsException("Hour number out of range (0-23)!");
56 }
57 this.hour = hour;
58 }
59
60
61
62
63
64
65
66
67 public static Hour of(final int hour)
68 {
69
70
71
72
73
74
75
76
77
78
79
80
81
82 return new Hour(hour);
83 }
84
85
86
87
88
89
90
91
92 public static Hour of(final String value)
93 {
94 return of(Integer.parseInt(value));
95 }
96
97
98
99
100
101
102
103 public int intValue()
104 {
105 return this.hour;
106 }
107
108
109
110
111
112
113
114 @Override
115 public String stringValue()
116 {
117 return String.valueOf(this.hour);
118 }
119
120
121
122
123
124
125
126
127 @Override
128 public int hashCode()
129 {
130 return Integer.hashCode(this.hour);
131 }
132
133
134
135
136
137
138
139
140
141 @Override
142 public boolean equals(final Object obj)
143 {
144 if (this == obj)
145 {
146 return true;
147 }
148 if (!(obj instanceof Hour))
149 {
150 return false;
151 }
152 final Hour other = (Hour)obj;
153 return this.hour == other.hour;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167 @Override
168 public String toString()
169 {
170 final var builder = new StringBuilder();
171 builder.append("Hour[hour=").append(this.hour).append(']');
172 return builder.toString();
173 }
174
175
176
177
178
179
180
181
182
183 @Override
184 public int compareTo(final Hour obj)
185 {
186 Objects.requireNonNull(obj, "obj");
187 return Integer.compare(this.hour, obj.hour);
188 }
189
190
191
192
193
194
195
196
197
198 public Hour add(final Hours hours)
199 {
200 final int newHour = Math.toIntExact(Math.addExact(this.hour, hours.longValue()));
201 if (newHour > 23)
202 {
203
204 throw new ArithmeticException(Hour.OVERFLOW);
205 }
206 return Hour.of(newHour);
207 }
208
209
210
211
212
213
214
215
216
217 public Hour subtract(final Hours hours)
218 {
219 final int newHour = Math.toIntExact(Math.subtractExact(this.hour, hours.longValue()));
220 if (newHour < 0)
221 {
222
223 throw new ArithmeticException(Hour.UNDERFLOW);
224 }
225 return Hour.of(newHour);
226 }
227
228
229
230
231
232
233
234
235 public Hour increment()
236 {
237 final int newHour = Math.incrementExact(this.hour);
238 if (newHour == 24)
239 {
240
241 throw new ArithmeticException(Hour.OVERFLOW);
242 }
243 return Hour.of(newHour);
244 }
245
246
247
248
249
250
251
252
253 public Hour decrement()
254 {
255 final int newHour = Math.decrementExact(this.hour);
256 if (newHour == -1)
257 {
258
259 throw new ArithmeticException(Hour.UNDERFLOW);
260 }
261 return Hour.of(newHour);
262 }
263
264 }