source: josm/trunk/src/org/openstreetmap/josm/data/osm/Tag.java@ 13665

Last change on this file since 13665 was 13625, checked in by Don-vip, 6 years ago

extract getNumKeys() from AbstractPrimitive to Tagged

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.Serializable;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.Map;
8import java.util.Map.Entry;
9import java.util.Objects;
10
11import org.openstreetmap.josm.tools.CheckParameterUtil;
12import 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 */
20public 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 * @deprecated since 13597. Use {@link Utils#removeWhiteSpaces(String)} instead
148 */
149 @Deprecated
150 public static String removeWhiteSpaces(String s) {
151 return Utils.removeWhiteSpaces(s);
152 }
153
154 /**
155 * Unsupported.
156 * @param keys ignored
157 * @throws UnsupportedOperationException always
158 */
159 @Override
160 public void setKeys(Map<String, String> keys) {
161 throw new UnsupportedOperationException();
162 }
163
164 @Override
165 public Map<String, String> getKeys() {
166 return Collections.singletonMap(key, value);
167 }
168
169 /**
170 * Unsupported.
171 * @param key ignored
172 * @param value ignored
173 * @throws UnsupportedOperationException always
174 */
175 @Override
176 public void put(String key, String value) {
177 throw new UnsupportedOperationException();
178 }
179
180 @Override
181 public String get(String k) {
182 return key.equals(k) ? value : null;
183 }
184
185 /**
186 * Unsupported.
187 * @param key ignored
188 * @throws UnsupportedOperationException always
189 */
190 @Override
191 public void remove(String key) {
192 throw new UnsupportedOperationException();
193 }
194
195 @Override
196 public boolean hasKeys() {
197 return true;
198 }
199
200 @Override
201 public Collection<String> keySet() {
202 return Collections.singleton(key);
203 }
204
205 @Override
206 public final int getNumKeys() {
207 return 1;
208 }
209
210 /**
211 * Unsupported.
212 * @throws UnsupportedOperationException always
213 */
214 @Override
215 public void removeAll() {
216 throw new UnsupportedOperationException();
217 }
218
219 /**
220 * true if this is a direction dependent tag (e.g. oneway)
221 *
222 * @return {@code true} if this is is a direction dependent tag
223 * @since 10716
224 */
225 public boolean isDirectionKey() {
226 return OsmPrimitive.directionKeys.match(this);
227 }
228
229}
Note: See TracBrowser for help on using the repository browser.