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