8
8
import java .util .List ;
9
9
import java .util .Map ;
10
10
11
+ /**
12
+ * A utility class for matching source feature properties to values.
13
+ *
14
+ * <p>
15
+ * Use the {@link #rule} function to create entries for a Planetiler {@link MultiExpression}. A rule consists of
16
+ * multiple contitions that get joined by a logical AND, and key-value pairs that should be used if all conditions of
17
+ * the rule are true. The key-value pairs of rules that get added later override the key-value pairs of rules that were
18
+ * added earlier.
19
+ * </p>
20
+ *
21
+ * <p>
22
+ * The MultiExpression can be used on a source feature and the resulting list of matches can be used in
23
+ * {@link #getString} and similar functions to retrieve a value.
24
+ * </p>
25
+ *
26
+ * <p>
27
+ * Example usage:
28
+ * </p>
29
+ *
30
+ * <pre>
31
+ * <code>
32
+ *var index = MultiExpression.of(List.of(rule(with("highway", "primary"), use("kind", "major_road")))).index();
33
+ *var matches = index.getMatches(sourceFeature);
34
+ *String kind = getString(sourceFeature, matches, "kind", "other");
35
+ * </code>
36
+ * </pre>
37
+ */
11
38
public class Matcher {
12
39
public record Use (String key , Object value ) {}
13
40
41
+ /**
42
+ * Creates a matching rule with conditions and values.
43
+ *
44
+ * <p>
45
+ * Create conditions by calling the {@link #with} or {@link #without} functions. All conditions are joined by a
46
+ * logical AND.
47
+ * </p>
48
+ *
49
+ * <p>
50
+ * Create key-value pairs with the {@link #use} function.
51
+ * </p>
52
+ *
53
+ * @param arguments A mix of {@link Use} instances for key-value pairs and {@link Expression} instances for
54
+ * conditions.
55
+ * @return A {@link MultiExpression.Entry} containing the rule definition.
56
+ */
14
57
public static MultiExpression .Entry <Map <String , Object >> rule (Object ... arguments ) {
15
58
Map <String , Object > result = new HashMap <>();
16
59
List <Expression > conditions = new ArrayList <>();
@@ -24,10 +67,50 @@ public static MultiExpression.Entry<Map<String, Object>> rule(Object... argument
24
67
return MultiExpression .entry (result , Expression .and (conditions ));
25
68
}
26
69
70
+ /**
71
+ * Creates a {@link Use} instance representing a key-value pair to be supplied to the {@link #rule} function.
72
+ *
73
+ * <p>
74
+ * While in principle any Object can be supplied as value, retrievalbe later on are only Strings with
75
+ * {@link #getString}, Integers with {@link #getInteger}, Doubles with {@link #getDouble}, Booleans with
76
+ * {@link #getBoolean}.
77
+ * </p>
78
+ *
79
+ * @param key The key.
80
+ * @param value The value associated with the key.
81
+ * @return A new {@link Use} instance.
82
+ */
27
83
public static Use use (String key , Object value ) {
28
84
return new Use (key , value );
29
85
}
30
86
87
+ /**
88
+ * Creates an {@link Expression} that matches any of the specified arguments.
89
+ *
90
+ * <p>
91
+ * If no argument is supplied, matches everything.
92
+ * </p>
93
+ *
94
+ * <p>
95
+ * If one argument is supplied, matches all source features that have this tag, e.g., {@code with("highway")} matches
96
+ * to all source features with a highway tag.
97
+ * </p>
98
+ *
99
+ * <p>
100
+ * If two arguments are supplied, matches to all source features that have this tag-value pair, e.g.,
101
+ * {@code with("highway", "primary")} matches to all source features with highway=primary.
102
+ * </p>
103
+ *
104
+ * <p>
105
+ * If more than two arguments are supplied, matches to all source features that have the first argument as tag and the
106
+ * later arguments as possible values, e.g., {@code with("highway", "primary", "secondary")} matches to all source
107
+ * features that have highway=primary or highway=secondary.
108
+ * </p>
109
+ * .
110
+ *
111
+ * @param arguments Field names to match.
112
+ * @return An {@link Expression} for the given field names.
113
+ */
31
114
public static Expression with (String ... arguments ) {
32
115
List <String > argumentList = List .of (arguments );
33
116
if (argumentList .isEmpty ()) {
@@ -38,12 +121,39 @@ public static Expression with(String... arguments) {
38
121
return Expression .matchAny (argumentList .getFirst (), argumentList .subList (1 , argumentList .size ()));
39
122
}
40
123
124
+ /**
125
+ * Same as {@link #with}, but negated.
126
+ */
41
127
public static Expression without (String ... arguments ) {
42
128
return Expression .not (with (arguments ));
43
129
}
44
130
45
131
public record FromTag (String key ) {}
46
132
133
+ /**
134
+ * Creates a {@link FromTag} instance representing a tag reference.
135
+ *
136
+ * <p>
137
+ * Use this function if to retrieve a value from a source feature when calling {@link #getString} and similar.
138
+ * </p>
139
+ *
140
+ * <p>
141
+ * Example usage:
142
+ * </p>
143
+ *
144
+ * <pre>
145
+ * <code>
146
+ *var index = MultiExpression.of(List.of(rule(with("highway", "primary", "secondary"), use("kind", fromTag("highway"))))).index();
147
+ *var matches = index.getMatches(sourceFeature);
148
+ *String kind = getString(sourceFeature, matches, "kind", "other");
149
+ * </code>
150
+ * </pre>
151
+ * <p>
152
+ * On a source feature with highway=primary the above will result in kind=primary.
153
+ *
154
+ * @param key The key of the tag.
155
+ * @return A new {@link FromTag} instance.
156
+ */
47
157
public static FromTag fromTag (String key ) {
48
158
return new FromTag (key );
49
159
}
0 commit comments