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