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