1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.autofilter;
|
---|
3 |
|
---|
4 | import java.util.Comparator;
|
---|
5 | import java.util.Objects;
|
---|
6 | import java.util.function.Function;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * An auto filter rule determines how auto filter can be built from visible map data.
|
---|
10 | * Several rules can be registered, but only one rule is active at the same time.
|
---|
11 | * Rules are identified by the OSM key on which they apply.
|
---|
12 | * The dynamic values discovering operates only below a certain zoom level, for performance reasons.
|
---|
13 | * @since 12400
|
---|
14 | */
|
---|
15 | public class AutoFilterRule {
|
---|
16 |
|
---|
17 | private final String key;
|
---|
18 |
|
---|
19 | private final int minZoomLevel;
|
---|
20 |
|
---|
21 | private Function<String, String> valueFormatter = s -> s;
|
---|
22 |
|
---|
23 | private Comparator<String> valueComparator = Comparator.comparingInt(s -> Integer.parseInt(valueFormatter.apply(s)));
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code AutoFilterRule}.
|
---|
27 | * @param key the OSM key on which the rule applies
|
---|
28 | * @param minZoomLevel the minimum zoom level at which the rule applies
|
---|
29 | */
|
---|
30 | public AutoFilterRule(String key, int minZoomLevel) {
|
---|
31 | this.key = key;
|
---|
32 | this.minZoomLevel = minZoomLevel;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns the OSM key on which the rule applies.
|
---|
37 | * @return the OSM key on which the rule applies
|
---|
38 | */
|
---|
39 | public String getKey() {
|
---|
40 | return key;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Returns the minimum zoom level at which the rule applies.
|
---|
45 | * @return the minimum zoom level at which the rule applies
|
---|
46 | */
|
---|
47 | public int getMinZoomLevel() {
|
---|
48 | return minZoomLevel;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Returns the OSM value formatter that defines the associated button label.
|
---|
53 | * @return the OSM value formatter that defines the associated button label (identity by default)
|
---|
54 | */
|
---|
55 | public Function<String, String> getValueFormatter() {
|
---|
56 | return valueFormatter;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Sets a OSM value formatter that defines the associated button label.
|
---|
61 | * @param valueFormatter OSM value formatter. Cannot be null
|
---|
62 | * @return {@code this}
|
---|
63 | * @throws NullPointerException if {@code valueFormatter} is null
|
---|
64 | */
|
---|
65 | public AutoFilterRule setValueFormatter(Function<String, String> valueFormatter) {
|
---|
66 | this.valueFormatter = Objects.requireNonNull(valueFormatter);
|
---|
67 | return this;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Returns the OSM value comparator used to order the buttons.
|
---|
72 | * @return the OSM value comparator
|
---|
73 | */
|
---|
74 | public Comparator<String> getValueComparator() {
|
---|
75 | return valueComparator;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Sets the OSM value comparator used to order the buttons.
|
---|
80 | * @param valueComparator the OSM value comparator
|
---|
81 | * @return {@code this}
|
---|
82 | * @throws NullPointerException if {@code valueComparator} is null
|
---|
83 | */
|
---|
84 | public AutoFilterRule setValueComparator(Comparator<String> valueComparator) {
|
---|
85 | this.valueComparator = valueComparator;
|
---|
86 | return this;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Returns the default list of auto filter rules. Plugins can extend the list by registering additional rules.
|
---|
91 | * @return the default list of auto filter rules
|
---|
92 | */
|
---|
93 | public static AutoFilterRule[] defaultRules() {
|
---|
94 | return new AutoFilterRule[] {
|
---|
95 | new AutoFilterRule("level", 17),
|
---|
96 | new AutoFilterRule("layer", 16),
|
---|
97 | new AutoFilterRule("maxspeed", 16)
|
---|
98 | .setValueFormatter(s -> s.replaceAll(" mph", "")),
|
---|
99 | new AutoFilterRule("voltage", 5)
|
---|
100 | .setValueFormatter(s -> s.replaceAll("000$", "k") + 'V')
|
---|
101 | .setValueComparator(Comparator.comparingInt(Integer::parseInt))
|
---|
102 | };
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public String toString() {
|
---|
107 | return key + '[' + minZoomLevel + ']';
|
---|
108 | }
|
---|
109 | }
|
---|