1
2
3
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8 import java.util.regex.Pattern;
9
10 import de.powerstat.validation.interfaces.IValueObject;
11
12
13
14
15
16
17
18
19
20 public final class BuildingNr implements Comparable<BuildingNr>, IValueObject
21 {
22
23
24
25
26
27
28
29
30
31
32
33
34
35 private static final Pattern BUILDINGNR_REGEXP = Pattern.compile("^(\\d{1,5})(([-/])(\\d{1,5}))?( (\\d{1,3})/(\\d{1,3}))?( ([a-z]))?$");
36
37
38
39
40 private static final int MAX_KNOWN_BUILDING_NR = 29999;
41
42
43
44
45 private final String buildingNr;
46
47
48
49
50
51
52
53
54
55 private BuildingNr(final String buildingNr)
56 {
57 super();
58 Objects.requireNonNull(buildingNr, "buildingNr");
59 if ((buildingNr.length() < 1) || (buildingNr.length() > 21))
60 {
61 throw new IllegalArgumentException("BuildingNr with wrong length");
62 }
63 final var matcher = BuildingNr.BUILDINGNR_REGEXP.matcher(buildingNr);
64 if (!matcher.matches())
65 {
66 throw new IllegalArgumentException("BuildingNr with wrong format");
67 }
68
69
70
71
72
73 if (Integer.parseInt(matcher.group(1)) > BuildingNr.MAX_KNOWN_BUILDING_NR)
74 {
75 throw new IllegalArgumentException("BuildingNr > " + BuildingNr.MAX_KNOWN_BUILDING_NR);
76 }
77 if ((matcher.group(4) != null) && (Integer.compare(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(4))) >= 0))
78 {
79 throw new IllegalArgumentException("BuildingNr from >= BuildingNr to");
80 }
81 if ((matcher.group(7) != null) && (Integer.compare(Integer.parseInt(matcher.group(6)), Integer.parseInt(matcher.group(7))) > 0))
82 {
83 throw new IllegalArgumentException("BuildingNr numerator > denominator");
84 }
85 this.buildingNr = buildingNr;
86 }
87
88
89
90
91
92
93
94
95 public static BuildingNr of(final String buildingNr)
96 {
97
98
99
100
101
102
103
104
105
106
107
108
109
110 return new BuildingNr(buildingNr);
111 }
112
113
114
115
116
117
118
119 @Override
120 public String stringValue()
121 {
122 return this.buildingNr;
123 }
124
125
126
127
128
129
130
131
132 @Override
133 public int hashCode()
134 {
135 return this.buildingNr.hashCode();
136 }
137
138
139
140
141
142
143
144
145
146 @Override
147 public boolean equals(final Object obj)
148 {
149 if (this == obj)
150 {
151 return true;
152 }
153 if (!(obj instanceof BuildingNr))
154 {
155 return false;
156 }
157 final BuildingNr other = (BuildingNr)obj;
158 return this.buildingNr.equals(other.buildingNr);
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 @Override
173 public String toString()
174 {
175 final var builder = new StringBuilder(23);
176 builder.append("BuildingNr[buildingNr=").append(this.buildingNr).append(']');
177 return builder.toString();
178 }
179
180
181
182
183
184
185
186
187
188
189
190 @Override
191 public int compareTo(final BuildingNr obj)
192 {
193 Objects.requireNonNull(obj, "obj");
194 final var matcher1 = BuildingNr.BUILDINGNR_REGEXP.matcher(this.buildingNr);
195 final var matcher2 = BuildingNr.BUILDINGNR_REGEXP.matcher(obj.buildingNr);
196 matcher1.matches();
197 matcher2.matches();
198
199
200
201
202
203 int result = Integer.compare(Integer.parseInt(matcher1.group(1)), Integer.parseInt(matcher2.group(1)));
204 if (result == 0)
205 {
206 if ((matcher1.group(7) != null) || (matcher2.group(7) != null))
207 {
208 if (matcher1.group(7) == null)
209 {
210 return -1;
211 }
212 if (matcher2.group(7) == null)
213 {
214 return 1;
215 }
216 if (matcher1.group(7).compareTo(matcher2.group(7)) != 0)
217 {
218 throw new IllegalStateException("BuildingNrs do not have the same denominator");
219 }
220 result = Integer.compare(Integer.parseInt(matcher1.group(6)), Integer.parseInt(matcher2.group(6)));
221 if (result != 0)
222 {
223 return result;
224 }
225 }
226 if ((matcher1.group(8) != null) || (matcher2.group(8) != null))
227 {
228 if (matcher1.group(8) == null)
229 {
230 result = -1;
231 }
232 else if (matcher2.group(8) == null)
233 {
234 result = 1;
235 }
236 else
237 {
238 result = matcher1.group(8).compareTo(matcher2.group(8));
239 }
240 }
241 }
242 return result;
243 }
244
245 }