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