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