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