1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.Map.Entry;
|
---|
8 | import java.util.Objects;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
11 | import org.openstreetmap.josm.tools.Utils;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Tag represents an immutable key/value-pair. Both the key and the value may be empty, but not null.
|
---|
15 | * <p>
|
---|
16 | * It implements the {@link Tagged} interface. However, since instances of this class are immutable,
|
---|
17 | * the modifying methods throw an {@link UnsupportedOperationException}.
|
---|
18 | */
|
---|
19 | public class Tag implements Tagged, Entry<String, String> {
|
---|
20 |
|
---|
21 | private final String key;
|
---|
22 | private final String value;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Create an empty tag whose key and value are empty.
|
---|
26 | */
|
---|
27 | public Tag() {
|
---|
28 | this("", "");
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Create a tag whose key is <code>key</code> and whose value is
|
---|
33 | * empty.
|
---|
34 | *
|
---|
35 | * @param key the key. If null, it is set to the empty key.
|
---|
36 | */
|
---|
37 | public Tag(String key) {
|
---|
38 | this(key, "");
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates a tag for a key and a value. If key and/or value are null,
|
---|
43 | * the empty value "" is assumed.
|
---|
44 | *
|
---|
45 | * @param key the key
|
---|
46 | * @param value the value
|
---|
47 | */
|
---|
48 | public Tag(String key, String value) {
|
---|
49 | this.key = key == null ? "" : key;
|
---|
50 | this.value = value == null ? "" : value;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Creates clone of the tag <code>tag</code>.
|
---|
55 | *
|
---|
56 | * @param tag the tag.
|
---|
57 | */
|
---|
58 | public Tag(Tag tag) {
|
---|
59 | this(tag.getKey(), tag.getValue());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Replies the key of the tag. This is never null.
|
---|
64 | *
|
---|
65 | * @return the key of the tag
|
---|
66 | */
|
---|
67 | @Override
|
---|
68 | public String getKey() {
|
---|
69 | return key;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Replies the value of the tag. This is never null.
|
---|
74 | *
|
---|
75 | * @return the value of the tag
|
---|
76 | */
|
---|
77 | @Override
|
---|
78 | public String getValue() {
|
---|
79 | return value;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * This is not supported by this implementation.
|
---|
84 | * @param value ignored
|
---|
85 | * @return (Does not return)
|
---|
86 | * @throws UnsupportedOperationException always
|
---|
87 | */
|
---|
88 | @Override
|
---|
89 | public String setValue(String value) {
|
---|
90 | throw new UnsupportedOperationException();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Replies true if the key of this tag is equal to <code>key</code>.
|
---|
95 | * If <code>key</code> is null, assumes the empty key.
|
---|
96 | *
|
---|
97 | * @param key the key
|
---|
98 | * @return true if the key of this tag is equal to <code>key</code>
|
---|
99 | */
|
---|
100 | public boolean matchesKey(String key) {
|
---|
101 | return this.key.equals(key);
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public int hashCode() {
|
---|
106 | return Objects.hash(key, value);
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public boolean equals(Object obj) {
|
---|
111 | if (this == obj) return true;
|
---|
112 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
113 | Tag tag = (Tag) obj;
|
---|
114 | return Objects.equals(key, tag.key) &&
|
---|
115 | Objects.equals(value, tag.value);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * This constructs a {@link Tag} by splitting {@code s} on the first equality sign.
|
---|
120 | * @param s the string to convert
|
---|
121 | * @return the constructed tag
|
---|
122 | * @see org.openstreetmap.josm.tools.TextTagParser
|
---|
123 | */
|
---|
124 | public static Tag ofString(String s) {
|
---|
125 | CheckParameterUtil.ensureParameterNotNull(s, "s");
|
---|
126 | final String[] x = s.split("=", 2);
|
---|
127 | if (x.length == 2) {
|
---|
128 | return new Tag(x[0], x[1]);
|
---|
129 | } else {
|
---|
130 | throw new IllegalArgumentException('\'' + s + "' does not contain '='");
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public String toString() {
|
---|
136 | return key + '=' + value;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Removes leading, trailing, and multiple inner whitespaces from the given string, to be used as a key or value.
|
---|
141 | * @param s The string
|
---|
142 | * @return The string without leading, trailing or multiple inner whitespaces
|
---|
143 | * @since 6699
|
---|
144 | */
|
---|
145 | public static String removeWhiteSpaces(String s) {
|
---|
146 | if (s == null || s.isEmpty()) {
|
---|
147 | return s;
|
---|
148 | }
|
---|
149 | return Utils.strip(s).replaceAll("\\s+", " ");
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Unsupported.
|
---|
154 | * @param keys ignored
|
---|
155 | * @throws UnsupportedOperationException always
|
---|
156 | */
|
---|
157 | @Override
|
---|
158 | public void setKeys(Map<String, String> keys) {
|
---|
159 | throw new UnsupportedOperationException();
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public Map<String, String> getKeys() {
|
---|
164 | return Collections.singletonMap(key, value);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Unsupported.
|
---|
169 | * @param key ignored
|
---|
170 | * @param value ignored
|
---|
171 | * @throws UnsupportedOperationException always
|
---|
172 | */
|
---|
173 | @Override
|
---|
174 | public void put(String key, String value) {
|
---|
175 | throw new UnsupportedOperationException();
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | public String get(String k) {
|
---|
180 | return key.equals(k) ? value : null;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Unsupported.
|
---|
185 | * @param key ignored
|
---|
186 | * @throws UnsupportedOperationException always
|
---|
187 | */
|
---|
188 | @Override
|
---|
189 | public void remove(String key) {
|
---|
190 | throw new UnsupportedOperationException();
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public boolean hasKeys() {
|
---|
195 | return true;
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public Collection<String> keySet() {
|
---|
200 | return Collections.singleton(key);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Unsupported.
|
---|
205 | * @throws UnsupportedOperationException always
|
---|
206 | */
|
---|
207 | @Override
|
---|
208 | public void removeAll() {
|
---|
209 | throw new UnsupportedOperationException();
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * true if this is a direction dependent tag (e.g. oneway)
|
---|
214 | *
|
---|
215 | * @return {@code true} if this is is a direction dependent tag
|
---|
216 | * @since 10716
|
---|
217 | */
|
---|
218 | public boolean isDirectionKey() {
|
---|
219 | return OsmPrimitive.directionKeys.match(this);
|
---|
220 | }
|
---|
221 |
|
---|
222 | }
|
---|