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