Skip to content

Commit 5536b93

Browse files
committed
Add docs
1 parent 6374d37 commit 5536b93

File tree

1 file changed

+110
-0
lines changed
  • tiles/src/main/java/com/protomaps/basemap/feature

1 file changed

+110
-0
lines changed

tiles/src/main/java/com/protomaps/basemap/feature/Matcher.java

+110
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,52 @@
88
import java.util.List;
99
import java.util.Map;
1010

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+
*/
1138
public class Matcher {
1239
public record Use(String key, Object value) {}
1340

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+
*/
1457
public static MultiExpression.Entry<Map<String, Object>> rule(Object... arguments) {
1558
Map<String, Object> result = new HashMap<>();
1659
List<Expression> conditions = new ArrayList<>();
@@ -24,10 +67,50 @@ public static MultiExpression.Entry<Map<String, Object>> rule(Object... argument
2467
return MultiExpression.entry(result, Expression.and(conditions));
2568
}
2669

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+
*/
2783
public static Use use(String key, Object value) {
2884
return new Use(key, value);
2985
}
3086

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+
*/
31114
public static Expression with(String... arguments) {
32115
List<String> argumentList = List.of(arguments);
33116
if (argumentList.isEmpty()) {
@@ -38,12 +121,39 @@ public static Expression with(String... arguments) {
38121
return Expression.matchAny(argumentList.getFirst(), argumentList.subList(1, argumentList.size()));
39122
}
40123

124+
/**
125+
* Same as {@link #with}, but negated.
126+
*/
41127
public static Expression without(String... arguments) {
42128
return Expression.not(with(arguments));
43129
}
44130

45131
public record FromTag(String key) {}
46132

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+
*/
47157
public static FromTag fromTag(String key) {
48158
return new FromTag(key);
49159
}

0 commit comments

Comments
 (0)