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