1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Set;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
12 | import org.openstreetmap.josm.data.validation.Test;
|
---|
13 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
14 | import org.openstreetmap.josm.tools.Logging;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Test that validates {@code lane:} tags.
|
---|
19 | * @since 6592
|
---|
20 | */
|
---|
21 | public class Lanes extends Test.TagTest {
|
---|
22 |
|
---|
23 | private static final String[] BLACKLIST = {
|
---|
24 | "source:lanes",
|
---|
25 | "note:lanes",
|
---|
26 | "proposed:lanes",
|
---|
27 | "source:proposed:lanes",
|
---|
28 | "piste:lanes",
|
---|
29 | };
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new {@code Lanes} test.
|
---|
33 | */
|
---|
34 | public Lanes() {
|
---|
35 | super(tr("Lane tags"), tr("Test that validates ''lane:'' tags."));
|
---|
36 | }
|
---|
37 |
|
---|
38 | static int getLanesCount(String value) {
|
---|
39 | return value.isEmpty() ? 0 : value.replaceAll("[^|]", "").length() + 1;
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected void checkNumberOfLanesByKey(final OsmPrimitive p, String lanesKey, String message) {
|
---|
43 | final Set<Integer> lanesCount =
|
---|
44 | p.keySet().stream()
|
---|
45 | .filter(x -> x.endsWith(":" + lanesKey))
|
---|
46 | .filter(x -> !Arrays.asList(BLACKLIST).contains(x))
|
---|
47 | .map(key -> getLanesCount(p.get(key)))
|
---|
48 | .collect(Collectors.toSet());
|
---|
49 |
|
---|
50 | if (lanesCount.size() > 1) {
|
---|
51 | // if not all numbers are the same
|
---|
52 | errors.add(TestError.builder(this, Severity.WARNING, 3100)
|
---|
53 | .message(message)
|
---|
54 | .primitives(p)
|
---|
55 | .build());
|
---|
56 | } else if (lanesCount.size() == 1 && p.hasKey(lanesKey)) {
|
---|
57 | // ensure that lanes <= *:lanes
|
---|
58 | try {
|
---|
59 | if (Integer.parseInt(p.get(lanesKey)) > lanesCount.iterator().next()) {
|
---|
60 | errors.add(TestError.builder(this, Severity.WARNING, 3100)
|
---|
61 | .message(tr("Number of {0} greater than {1}", lanesKey, "*:" + lanesKey))
|
---|
62 | .primitives(p)
|
---|
63 | .build());
|
---|
64 | }
|
---|
65 | } catch (NumberFormatException ignore) {
|
---|
66 | Logging.debug(ignore.getMessage());
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected void checkNumberOfLanes(final OsmPrimitive p) {
|
---|
72 | final String lanes = p.get("lanes");
|
---|
73 | if (lanes == null) return;
|
---|
74 | final String forward = Utils.firstNonNull(p.get("lanes:forward"), "0");
|
---|
75 | final String backward = Utils.firstNonNull(p.get("lanes:backward"), "0");
|
---|
76 | try {
|
---|
77 | if (Integer.parseInt(lanes) < Integer.parseInt(forward) + Integer.parseInt(backward)) {
|
---|
78 | errors.add(TestError.builder(this, Severity.WARNING, 3101)
|
---|
79 | .message(tr("Number of {0} greater than {1}", tr("{0}+{1}", "lanes:forward", "lanes:backward"), "lanes"))
|
---|
80 | .primitives(p)
|
---|
81 | .build());
|
---|
82 | }
|
---|
83 | } catch (NumberFormatException ignore) {
|
---|
84 | Logging.debug(ignore.getMessage());
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public void check(OsmPrimitive p) {
|
---|
90 | checkNumberOfLanesByKey(p, "lanes", tr("Number of lane dependent values inconsistent"));
|
---|
91 | checkNumberOfLanesByKey(p, "lanes:forward", tr("Number of lane dependent values inconsistent in forward direction"));
|
---|
92 | checkNumberOfLanesByKey(p, "lanes:backward", tr("Number of lane dependent values inconsistent in backward direction"));
|
---|
93 | checkNumberOfLanes(p);
|
---|
94 | }
|
---|
95 | }
|
---|