1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.Objects;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 | import java.util.stream.Stream;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Objects implement Tagged if they provide a map of key/value pairs.
|
---|
13 | *
|
---|
14 | * @since 2115
|
---|
15 | */
|
---|
16 | // FIXME: better naming? setTags(), getTags(), getKeys() instead of keySet() ?
|
---|
17 | //
|
---|
18 | public interface Tagged {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The maximum tag length allowed by OSM API
|
---|
22 | * @since 13414
|
---|
23 | */
|
---|
24 | int MAX_TAG_LENGTH = 255;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Sets the map of key/value pairs
|
---|
28 | *
|
---|
29 | * @param keys the map of key value pairs. If null, reset to the empty map.
|
---|
30 | */
|
---|
31 | void setKeys(Map<String, String> keys);
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Replies the map of key/value pairs. Never null, but may be the empty map.
|
---|
35 | *
|
---|
36 | * @return the map of key/value pairs
|
---|
37 | */
|
---|
38 | Map<String, String> getKeys();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Calls the visitor for every key/value pair.
|
---|
42 | *
|
---|
43 | * @param visitor The visitor to call.
|
---|
44 | * @see #getKeys()
|
---|
45 | * @since 13668
|
---|
46 | */
|
---|
47 | default void visitKeys(KeyValueVisitor visitor) {
|
---|
48 | getKeys().forEach((k, v) -> visitor.visitKeyValue(this, k, v));
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Sets a key/value pairs
|
---|
53 | *
|
---|
54 | * @param key the key
|
---|
55 | * @param value the value. If null, removes the key/value pair.
|
---|
56 | */
|
---|
57 | void put(String key, String value);
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Sets a key/value pairs
|
---|
61 | *
|
---|
62 | * @param tag The tag to set.
|
---|
63 | * @since 10736
|
---|
64 | */
|
---|
65 | default void put(Tag tag) {
|
---|
66 | put(tag.getKey(), tag.getValue());
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Replies the value of the given key; null, if there is no value for this key
|
---|
71 | *
|
---|
72 | * @param key the key
|
---|
73 | * @return the value
|
---|
74 | */
|
---|
75 | String get(String key);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Removes a given key/value pair
|
---|
79 | *
|
---|
80 | * @param key the key
|
---|
81 | */
|
---|
82 | void remove(String key);
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Replies true, if there is at least one key/value pair; false, otherwise
|
---|
86 | *
|
---|
87 | * @return true, if there is at least one key/value pair; false, otherwise
|
---|
88 | */
|
---|
89 | boolean hasKeys();
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Replies true if there is a tag with key <code>key</code>.
|
---|
93 | * The value could however be empty. See {@link #hasTag(String)} to check for non-empty tags.
|
---|
94 | *
|
---|
95 | * @param key the key
|
---|
96 | * @return true, if there is a tag with key <code>key</code>
|
---|
97 | * @see #hasTag(String)
|
---|
98 | * @since 11608
|
---|
99 | */
|
---|
100 | default boolean hasKey(String key) {
|
---|
101 | return get(key) != null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Replies true if there is a non-empty tag with key <code>key</code>.
|
---|
106 | *
|
---|
107 | * @param key the key
|
---|
108 | * @return true, if there is a non-empty tag with key <code>key</code>
|
---|
109 | * @see Tagged#hasKey(String)
|
---|
110 | * @since 13430
|
---|
111 | */
|
---|
112 | default boolean hasTag(String key) {
|
---|
113 | String v = get(key);
|
---|
114 | return v != null && !v.isEmpty();
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Tests whether this primitive contains a tag consisting of {@code key} and {@code value}.
|
---|
119 | * @param key the key forming the tag.
|
---|
120 | * @param value value forming the tag.
|
---|
121 | * @return true if primitive contains a tag consisting of {@code key} and {@code value}.
|
---|
122 | * @since 13668
|
---|
123 | */
|
---|
124 | default boolean hasTag(String key, String value) {
|
---|
125 | return Objects.equals(value, get(key));
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Tests whether this primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
130 | * @param key the key forming the tag.
|
---|
131 | * @param values one or many values forming the tag.
|
---|
132 | * @return true if primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
133 | * @since 13668
|
---|
134 | */
|
---|
135 | default boolean hasTag(String key, String... values) {
|
---|
136 | return hasTag(key, Arrays.asList(values));
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Tests whether this primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
141 | * @param key the key forming the tag.
|
---|
142 | * @param values one or many values forming the tag.
|
---|
143 | * @return true if primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
144 | * @since 13668
|
---|
145 | */
|
---|
146 | default boolean hasTag(String key, Collection<String> values) {
|
---|
147 | String v = get(key);
|
---|
148 | return v != null && values.contains(v);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Tests whether this primitive contains a tag consisting of {@code key} and a value different from {@code value}.
|
---|
153 | * @param key the key forming the tag.
|
---|
154 | * @param value value not forming the tag.
|
---|
155 | * @return true if primitive contains a tag consisting of {@code key} and a value different from {@code value}.
|
---|
156 | * @since 13668
|
---|
157 | */
|
---|
158 | default boolean hasTagDifferent(String key, String value) {
|
---|
159 | String v = get(key);
|
---|
160 | return v != null && !v.equals(value);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Tests whether this primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
165 | * @param key the key forming the tag.
|
---|
166 | * @param values one or many values forming the tag.
|
---|
167 | * @return true if primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
168 | * @since 13668
|
---|
169 | */
|
---|
170 | default boolean hasTagDifferent(String key, String... values) {
|
---|
171 | return hasTagDifferent(key, Arrays.asList(values));
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Tests whether this primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
176 | * @param key the key forming the tag.
|
---|
177 | * @param values one or many values forming the tag.
|
---|
178 | * @return true if primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
179 | * @since 13668
|
---|
180 | */
|
---|
181 | default boolean hasTagDifferent(String key, Collection<String> values) {
|
---|
182 | String v = get(key);
|
---|
183 | return v != null && !values.contains(v);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Replies the set of keys
|
---|
188 | *
|
---|
189 | * @return the set of keys
|
---|
190 | * @see #keys()
|
---|
191 | */
|
---|
192 | Collection<String> keySet();
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Replies the keys as stream
|
---|
196 | *
|
---|
197 | * @return the keys as stream
|
---|
198 | * @see #keySet()
|
---|
199 | * @since 17584
|
---|
200 | */
|
---|
201 | default Stream<String> keys() {
|
---|
202 | return keySet().stream();
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Gets the number of keys
|
---|
207 | * @return The number of keys set for this tagged object.
|
---|
208 | * @since 13625
|
---|
209 | */
|
---|
210 | int getNumKeys();
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Removes all tags
|
---|
214 | */
|
---|
215 | void removeAll();
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns true if the {@code key} corresponds to an OSM true value.
|
---|
219 | * @param key OSM key
|
---|
220 | * @return {@code true} if the {@code key} corresponds to an OSM true value
|
---|
221 | * @see OsmUtils#isTrue(String)
|
---|
222 | */
|
---|
223 | default boolean isKeyTrue(String key) {
|
---|
224 | return OsmUtils.isTrue(get(key));
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Returns true if the {@code key} corresponds to an OSM false value.
|
---|
229 | * @param key OSM key
|
---|
230 | * @return {@code true} if the {@code key} corresponds to an OSM false value
|
---|
231 | * @see OsmUtils#isFalse(String)
|
---|
232 | */
|
---|
233 | default boolean isKeyFalse(String key) {
|
---|
234 | return OsmUtils.isFalse(get(key));
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Returns a Tagged instance for the given tag collection
|
---|
239 | * @param tags the tag collection
|
---|
240 | * @return a Tagged instance for the given tag collection
|
---|
241 | */
|
---|
242 | static Tagged ofTags(Collection<Tag> tags) {
|
---|
243 | return ofMap(tags.stream().collect(Collectors.toMap(Tag::getKey, Tag::getValue, (a, b) -> a)));
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Returns a Tagged instance for the given tag map
|
---|
248 | * @param tags the tag map
|
---|
249 | * @return a Tagged instance for the given tag map
|
---|
250 | */
|
---|
251 | static Tagged ofMap(Map<String, String> tags) {
|
---|
252 | return new Tagged() {
|
---|
253 |
|
---|
254 | @Override
|
---|
255 | public String get(String key) {
|
---|
256 | return tags.get(key);
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public Map<String, String> getKeys() {
|
---|
261 | return tags;
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public Collection<String> keySet() {
|
---|
266 | return tags.keySet();
|
---|
267 | }
|
---|
268 |
|
---|
269 | @Override
|
---|
270 | public void put(String key, String value) {
|
---|
271 | tags.put(key, value);
|
---|
272 | }
|
---|
273 |
|
---|
274 | @Override
|
---|
275 | public void setKeys(Map<String, String> keys) {
|
---|
276 | tags.putAll(keys);
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Override
|
---|
280 | public boolean hasKeys() {
|
---|
281 | return !tags.isEmpty();
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Override
|
---|
285 | public int getNumKeys() {
|
---|
286 | return tags.size();
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Override
|
---|
290 | public void remove(String key) {
|
---|
291 | tags.remove(key);
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void removeAll() {
|
---|
296 | tags.clear();
|
---|
297 | }
|
---|
298 | };
|
---|
299 | }
|
---|
300 | }
|
---|