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 |
|
---|
9 | /**
|
---|
10 | * Objects implement Tagged if they provide a map of key/value pairs.
|
---|
11 | *
|
---|
12 | * @since 2115
|
---|
13 | */
|
---|
14 | // FIXME: better naming? setTags(), getTags(), getKeys() instead of keySet() ?
|
---|
15 | //
|
---|
16 | public interface Tagged {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The maximum tag length allowed by OSM API
|
---|
20 | * @since 13414
|
---|
21 | */
|
---|
22 | int MAX_TAG_LENGTH = 255;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Sets the map of key/value pairs
|
---|
26 | *
|
---|
27 | * @param keys the map of key value pairs. If null, reset to the empty map.
|
---|
28 | */
|
---|
29 | void setKeys(Map<String, String> keys);
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Replies the map of key/value pairs. Never null, but may be the empty map.
|
---|
33 | *
|
---|
34 | * @return the map of key/value pairs
|
---|
35 | */
|
---|
36 | Map<String, String> getKeys();
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Calls the visitor for every key/value pair.
|
---|
40 | *
|
---|
41 | * @param visitor The visitor to call.
|
---|
42 | * @see #getKeys()
|
---|
43 | * @since 13668
|
---|
44 | */
|
---|
45 | default void visitKeys(KeyValueVisitor visitor) {
|
---|
46 | getKeys().forEach((k, v) -> visitor.visitKeyValue(this, k, v));
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Sets a key/value pairs
|
---|
51 | *
|
---|
52 | * @param key the key
|
---|
53 | * @param value the value. If null, removes the key/value pair.
|
---|
54 | */
|
---|
55 | void put(String key, String value);
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Sets a key/value pairs
|
---|
59 | *
|
---|
60 | * @param tag The tag to set.
|
---|
61 | * @since 10736
|
---|
62 | */
|
---|
63 | default void put(Tag tag) {
|
---|
64 | put(tag.getKey(), tag.getValue());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Replies the value of the given key; null, if there is no value for this key
|
---|
69 | *
|
---|
70 | * @param key the key
|
---|
71 | * @return the value
|
---|
72 | */
|
---|
73 | String get(String key);
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Removes a given key/value pair
|
---|
77 | *
|
---|
78 | * @param key the key
|
---|
79 | */
|
---|
80 | void remove(String key);
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Replies true, if there is at least one key/value pair; false, otherwise
|
---|
84 | *
|
---|
85 | * @return true, if there is at least one key/value pair; false, otherwise
|
---|
86 | */
|
---|
87 | boolean hasKeys();
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Replies true if there is a tag with key <code>key</code>.
|
---|
91 | * The value could however be empty. See {@link #hasTag(String)} to check for non-empty tags.
|
---|
92 | *
|
---|
93 | * @param key the key
|
---|
94 | * @return true, if there is a tag with key <code>key</code>
|
---|
95 | * @see #hasTag(String)
|
---|
96 | * @since 11608
|
---|
97 | */
|
---|
98 | default boolean hasKey(String key) {
|
---|
99 | return get(key) != null;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Replies true if there is a non-empty tag with key <code>key</code>.
|
---|
104 | *
|
---|
105 | * @param key the key
|
---|
106 | * @return true, if there is a non-empty tag with key <code>key</code>
|
---|
107 | * @see Tagged#hasKey(String)
|
---|
108 | * @since 13430
|
---|
109 | */
|
---|
110 | default boolean hasTag(String key) {
|
---|
111 | String v = get(key);
|
---|
112 | return v != null && !v.isEmpty();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Tests whether this primitive contains a tag consisting of {@code key} and {@code value}.
|
---|
117 | * @param key the key forming the tag.
|
---|
118 | * @param value value forming the tag.
|
---|
119 | * @return true if primitive contains a tag consisting of {@code key} and {@code value}.
|
---|
120 | * @since 13668
|
---|
121 | */
|
---|
122 | default boolean hasTag(String key, String value) {
|
---|
123 | return Objects.equals(value, get(key));
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Tests whether this primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
128 | * @param key the key forming the tag.
|
---|
129 | * @param values one or many values forming the tag.
|
---|
130 | * @return true if primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
131 | * @since 13668
|
---|
132 | */
|
---|
133 | default boolean hasTag(String key, String... values) {
|
---|
134 | return hasTag(key, Arrays.asList(values));
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Tests whether this primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
139 | * @param key the key forming the tag.
|
---|
140 | * @param values one or many values forming the tag.
|
---|
141 | * @return true if primitive contains a tag consisting of {@code key} and any of {@code values}.
|
---|
142 | * @since 13668
|
---|
143 | */
|
---|
144 | default boolean hasTag(String key, Collection<String> values) {
|
---|
145 | return values.contains(get(key));
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Tests whether this primitive contains a tag consisting of {@code key} and a value different from {@code value}.
|
---|
150 | * @param key the key forming the tag.
|
---|
151 | * @param value value not forming the tag.
|
---|
152 | * @return true if primitive contains a tag consisting of {@code key} and a value different from {@code value}.
|
---|
153 | * @since 13668
|
---|
154 | */
|
---|
155 | default boolean hasTagDifferent(String key, String value) {
|
---|
156 | String v = get(key);
|
---|
157 | return v != null && !v.equals(value);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Tests whether this primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
162 | * @param key the key forming the tag.
|
---|
163 | * @param values one or many values forming the tag.
|
---|
164 | * @return true if primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
165 | * @since 13668
|
---|
166 | */
|
---|
167 | default boolean hasTagDifferent(String key, String... values) {
|
---|
168 | return hasTagDifferent(key, Arrays.asList(values));
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Tests whether this primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
173 | * @param key the key forming the tag.
|
---|
174 | * @param values one or many values forming the tag.
|
---|
175 | * @return true if primitive contains a tag consisting of {@code key} and none of {@code values}.
|
---|
176 | * @since 13668
|
---|
177 | */
|
---|
178 | default boolean hasTagDifferent(String key, Collection<String> values) {
|
---|
179 | String v = get(key);
|
---|
180 | return v != null && !values.contains(v);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Replies the set of keys
|
---|
185 | *
|
---|
186 | * @return the set of keys
|
---|
187 | */
|
---|
188 | Collection<String> keySet();
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Gets the number of keys
|
---|
192 | * @return The number of keys set for this tagged object.
|
---|
193 | * @since 13625
|
---|
194 | */
|
---|
195 | int getNumKeys();
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Removes all tags
|
---|
199 | */
|
---|
200 | void removeAll();
|
---|
201 | }
|
---|