Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/Numbers.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.net;
14
15 import java.util.Objects;
16
17 import uk.me.parabola.log.Logger;
18 import uk.me.parabola.mkgmap.general.CityInfo;
19 import uk.me.parabola.mkgmap.general.ZipCodeInfo;
20
21 /**
22 * Describes the house numbering from a node in the road.
23 * @author Steve Ratcliffe
24 */
25 public class Numbers {
26 private static final Logger log = Logger.getLogger(Numbers.class);
27 public static final boolean LEFT = true;
28 public static final boolean RIGHT = false;
29
30 private static final int MAX_DELTA = 131071; // see NumberPreparer
31
32 /**
33 * Polish format: The point in the road where these numbers apply. Not written to NET!
34 */
35 private int polishPointIndex;
36 /**
37 * The position in the list of nodes (starting with 0). Negative value means it is not yet set.
38 * Identifies the node (not point) in the road. This is needed to encode the bitstream in NET.
39 */
40 private int nodeIndex = -1;
41
42 /** the data on side of the road */
43 private RoadSide leftSide,rightSide;
44
45 private class RoadSide {
46 NumDesc numbers;
47 // to be added
48 CityInfo cityInfo;
49 ZipCodeInfo zipCode;
50 boolean isEmpty(){
51 return cityInfo == null && zipCode == null && numbers == null;
52 }
53 }
54
55 private class NumDesc{
56 NumberStyle numberStyle;
57 int start, end;
58
59 public NumDesc(NumberStyle numberStyle, int start, int end) {
60 this.numberStyle = numberStyle;
61 this.start = start;
62 this.end = end;
63 }
64
65 public boolean contained(int hn) {
66 boolean isEven = (hn % 2 == 0);
67 if (numberStyle == NumberStyle.BOTH
68 || numberStyle == NumberStyle.EVEN && isEven
69 || numberStyle == NumberStyle.ODD && !isEven) {
70 if (start <= end) {
71 return start <= hn && hn <= end;
72 } else {
73 return end <= hn && hn <= start;
74 }
75 }
76 return false;
77 }
78
79 @Override
80 public String toString() {
81 return String.format("%s,%d,%d", numberStyle, start,end);
82 }
83
84 }
85
86 public Numbers() {
87 }
88
89 /**
90 * This constructor takes a comma separated list as in the polish format. Also used in testing as
91 * it is an easy way to set all common parameters at once.
92 *
93 * @param spec Node number, followed by left and then right parameters as in the polish format.
94 */
95 public Numbers(String spec) {
96 String[] strings = spec.split(",");
97 polishPointIndex = Integer.parseInt(strings[0]);
98 NumberStyle numberStyle = NumberStyle.fromChar(strings[1]);
99 int start = Integer.parseInt(strings[2]);
100 int end = Integer.parseInt(strings[3]);
101 setNumbers(LEFT, numberStyle, start, end);
102 numberStyle = NumberStyle.fromChar(strings[4]);
103 start = Integer.parseInt(strings[5]);
104 end = Integer.parseInt(strings[6]);
105 setNumbers(RIGHT, numberStyle, start, end);
106
107 if (strings.length > 8){
108 // zip codes
109 String zip = strings[7];
110 if (!"-1".equals(zip))
111 setZipCode(LEFT, new ZipCodeInfo(zip));
112 zip = strings[8];
113 if (!"-1".equals(zip))
114 setZipCode(RIGHT, new ZipCodeInfo(zip));
115 }
116 if (strings.length > 9) {
117 String city, region, country;
118 int nextPos = 9;
119 city = strings[nextPos];
120 if (!"-1".equals(city)) {
121 region = strings[nextPos + 1];
122 country = strings[nextPos + 2];
123 setCityInfo(LEFT, new CityInfo(city, region, country));
124 nextPos = 12;
125 } else {
126 nextPos = 10;
127 }
128 city = strings[nextPos];
129 if (!"-1".equals(city)) {
130 region = strings[nextPos + 1];
131 country = strings[nextPos + 2];
132 setCityInfo(RIGHT, new CityInfo(city, region, country));
133 }
134 }
135 }
136
137 public void setNumbers(boolean useLeft, NumberStyle numberStyle, int start, int end){
138 if (numberStyle != NumberStyle.NONE || start != -1 || end != -1){
139 RoadSide rs = assureSideIsAllocated(useLeft);
140 rs.numbers = new NumDesc(numberStyle, start, end);
141 } else {
142 RoadSide rs = (useLeft) ? leftSide : rightSide;
143 if (rs != null)
144 rs.numbers = null;
145 removeIfEmpty(useLeft);
146 }
147 }
148
149 public void setCityInfo(boolean left, CityInfo ci){
150 if (ci != null){
151 RoadSide rs = assureSideIsAllocated(left);
152 rs.cityInfo = ci;
153 } else {
154 RoadSide rs = (left) ? leftSide : rightSide;
155 if (rs != null)
156 rs.cityInfo = null;
157 removeIfEmpty(left);
158 }
159 }
160
161 public CityInfo getCityInfo(boolean left){
162 RoadSide rs = (left) ? leftSide : rightSide;
163 return (rs != null) ? rs.cityInfo : null;
164 }
165
166 public void setZipCode(boolean left, ZipCodeInfo zipCode){
167 if (zipCode != null){
168 RoadSide rs = assureSideIsAllocated(left);
169 rs.zipCode = zipCode;
170 } else {
171 RoadSide rs = (left) ? leftSide : rightSide;
172 if (rs != null)
173 rs.zipCode= null;
174 removeIfEmpty(left);
175 }
176 }
177
178
179 public ZipCodeInfo getZipCodeInfo (boolean left){
180 RoadSide rs = (left) ? leftSide : rightSide;
181 return (rs != null) ? rs.zipCode: null;
182 }
183
184 private void removeIfEmpty(boolean left){
185 if (left && leftSide != null && leftSide.isEmpty())
186 leftSide = null;
187 if (!left && rightSide != null && rightSide.isEmpty())
188 rightSide = null;
189 }
190
191 // allocate or return allocated RoadSide instance for the given road side
192 private RoadSide assureSideIsAllocated(boolean left){
193 if (left && leftSide == null)
194 leftSide = new RoadSide();
195 if (!left && rightSide == null)
196 rightSide = new RoadSide();
197 return (left) ? leftSide : rightSide;
198 }
199
200 public int getPolishIndex() {
201 return polishPointIndex;
202 }
203
204 public void setPolishIndex(int n) {
205 this.polishPointIndex = n;
206 }
207
208 /**
209 * @return The index of the nth number node where these numbers apply.
210 */
211 public int getIndex() {
212 if (nodeIndex < 0) {
213 log.error("WARNING: node index not set!!");
214 return 0;
215 }
216 return nodeIndex;
217 }
218
219 public boolean hasIndex() {
220 return nodeIndex >= 0;
221 }
222
223 /**
224 * @param index the nth number node
225 */
226 public void setIndex(int index) {
227 this.nodeIndex = index;
228 }
229
230 private NumDesc getNumbers(boolean left) {
231 RoadSide rs = (left) ? leftSide : rightSide;
232 return (rs != null) ? rs.numbers : null;
233 }
234
235 public NumberStyle getNumberStyle(boolean left) {
236 NumDesc n = getNumbers(left);
237 return (n == null) ? NumberStyle.NONE : n.numberStyle;
238 }
239
240 public int getStart(boolean left) {
241 NumDesc n = getNumbers(left);
242 return (n == null) ? -1 : n.start; // -1 is the default in the polish format
243 }
244
245 public int getEnd(boolean left) {
246 NumDesc n = getNumbers(left);
247 return (n == null) ? -1 : n.end; // -1 is the default in the polish format
248 }
249
250 public String toString() {
251 String nodeStr = "0";
252 if (polishPointIndex > 0)
253 nodeStr = Integer.toString(polishPointIndex);
254 else if (getIndex() > 0)
255 nodeStr = String.format("(n%d)", getIndex());
256
257 nodeStr = String.format("%s,%s,%d,%d,%s,%d,%d",
258 nodeStr,
259 getNumberStyle(LEFT),
260 getStart(LEFT),
261 getEnd(LEFT),
262 getNumberStyle(RIGHT),
263 getStart(RIGHT),
264 getEnd(RIGHT));
265
266 if (getCityInfo(LEFT) != null || getCityInfo(RIGHT) != null
267 || getZipCodeInfo(LEFT) != null || getZipCodeInfo(RIGHT) != null) {
268 nodeStr = String.format("%s,%s,%s", nodeStr,
269 getPolishZipCode(LEFT), getPolishZipCode(RIGHT));
270 if (getCityInfo(LEFT) != null || getCityInfo(RIGHT) != null) {
271 nodeStr = String.format("%s,%s,%s",nodeStr,
272 getPolishCityInfo(LEFT),getPolishCityInfo(RIGHT));
273 }
274 }
275 return nodeStr;
276 }
277
278 public NumberStyle getLeftNumberStyle() {
279 return getNumberStyle(LEFT);
280 }
281 public NumberStyle getRightNumberStyle() {
282 return getNumberStyle(RIGHT);
283 }
284 public int getLeftStart(){
285 return getStart(LEFT);
286 }
287 public int getRightStart(){
288 return getStart(RIGHT);
289 }
290 public int getLeftEnd(){
291 return getEnd(LEFT);
292 }
293 public int getRightEnd(){
294 return getEnd(RIGHT);
295 }
296
297 public boolean equals(Object obj) {
298 if (!(obj instanceof Numbers))
299 return false;
300
301 Numbers other = (Numbers) obj;
302 return toString().equals(other.toString());
303 }
304
305 public int hashCode() {
306 return toString().hashCode();
307 }
308
309 public boolean isPlausible(){
310 if (!isPlausible(getLeftNumberStyle(), getLeftStart(), getLeftEnd()))
311 return false;
312 if (!isPlausible(getRightNumberStyle(), getRightStart(), getRightEnd()))
313 return false;
314 if (getLeftNumberStyle() == NumberStyle.NONE || getRightNumberStyle() == NumberStyle.NONE) {
315 return true; // no need to compare values of road sides
316 }
317 if (!Objects.equals(getCityInfo(LEFT), getCityInfo(RIGHT))) {
318 return true;
319 }
320 if (!Objects.equals(getZipCodeInfo(LEFT), getZipCodeInfo(RIGHT))) {
321 return true;
322 }
323 // city and zip codes say that the intervals of numbers should not overlap
324 if (getLeftNumberStyle() == getRightNumberStyle()
325 || getLeftNumberStyle() == NumberStyle.BOTH
326 || getRightNumberStyle() == NumberStyle.BOTH) {
327 // check if intervals are overlapping
328 int start1, start2, end1, end2;
329 if (getLeftStart() < getLeftEnd()) {
330 start1 = getLeftStart();
331 end1 = getLeftEnd();
332 } else {
333 start1 = getLeftEnd();
334 end1 = getLeftStart();
335 }
336 if (getRightStart() < getRightEnd()) {
337 start2 = getRightStart();
338 end2 = getRightEnd();
339 } else {
340 start2 = getRightEnd();
341 end2 = getRightStart();
342 }
343 if (start2 > end1 || end2 < start1)
344 return true;
345 // single number on both sides of the road?
346 return getLeftStart() == getLeftEnd() && getRightStart() == getRightEnd()
347 && getLeftStart() == getRightStart();
348 }
349
350 return true;
351 }
352
353 private static boolean isPlausible(NumberStyle style, int start, int end){
354 if (Math.abs(start - end) > MAX_DELTA)
355 return false;
356 if (style == NumberStyle.EVEN)
357 return start % 2 == 0 && end % 2 == 0;
358 if (style == NumberStyle.ODD)
359 return start % 2 != 0 && end % 2 != 0;
360 return true;
361 }
362
363 public boolean isContained(int hn, boolean useLeft){
364 RoadSide rs = useLeft ? leftSide : rightSide;
365 if (rs == null || rs.numbers == null)
366 return false;
367 return rs.numbers.contained(hn);
368 }
369
370 /**
371 * @param hn a house number
372 * @param left left or right side
373 * @return 0 if the number is not within the intervals, 1 if it is on one side, 2 if it on both sides
374 */
375 public int countMatches(int hn) {
376 int matches = 0;
377 if (isContained(hn, LEFT))
378 matches++;
379 if (isContained(hn, RIGHT))
380 matches++;
381 if (matches > 1 && getLeftStart() == getLeftEnd() && getRightStart() == getRightEnd()) {
382 matches = 1; // single number on both sides of the road
383 }
384 return matches;
385 }
386
387 /**
388 * Compare all fields that describe the interval, but not the position
389 * @param other
390 * @return true if these fields are equal
391 */
392 public boolean isSimilar(Numbers other){
393 if (other == null)
394 return false;
395 return !(getLeftNumberStyle() != other.getLeftNumberStyle()
396 || getLeftStart() != other.getLeftStart()
397 || getLeftEnd() != other.getLeftEnd()
398 || getRightNumberStyle() != other.getRightNumberStyle()
399 || getRightStart() != other.getRightStart()
400 || getRightEnd() != other.getRightEnd());
401
402 }
403
404 public boolean isEmpty(){
405 return getLeftNumberStyle() == NumberStyle.NONE && getRightNumberStyle() == NumberStyle.NONE;
406 }
407
408
409 private String getPolishCityInfo (boolean left){
410 CityInfo ci = getCityInfo(left);
411 if (ci == null)
412 return "-1";
413 StringBuilder sb = new StringBuilder();
414 if (ci.getCity() != null)
415 sb.append(ci.getCity());
416 sb.append(",");
417 if (ci.getRegion() != null)
418 sb.append(ci.getRegion());
419 sb.append(",");
420 if (ci.getCountry() != null)
421 sb.append(ci.getCountry());
422 return sb.toString();
423 }
424
425 private String getPolishZipCode (boolean left){
426 ZipCodeInfo zip = getZipCodeInfo(left);
427 return (zip != null && zip.getZipCode() != null ) ? zip.getZipCode() : "-1";
428 }
429
430
431 }