source: josm/trunk/src/org/openstreetmap/josm/gui/autofilter/AutoFilterRule.java@ 13849

Last change on this file since 13849 was 13849, checked in by Don-vip, 6 years ago

SonarQube - fix minor code issues

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