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

Last change on this file since 6513 was 6513, checked in by simon04, 10 years ago

see #9414 - convert some tagchecker tests to MapCSS, extend MapCSS by test for false values

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import org.openstreetmap.josm.tools.CheckParameterUtil;
5
6/**
7 * Tag represents an immutable key/value-pair. Both the key and the value may
8 * be empty, but not null.
9 *
10 */
11public class Tag {
12
13 private String key;
14 private String value;
15
16 /**
17 * Create an empty tag whose key and value are empty.
18 */
19 public Tag(){
20 this("", "");
21 }
22
23 /**
24 * Create a tag whose key is <code>key</code> and whose value is
25 * empty.
26 *
27 * @param key the key. If null, it is set to the empty key.
28 */
29 public Tag(String key) {
30 this(key, "");
31 }
32
33 /**
34 * Creates a tag for a key and a value. If key and/or value are null,
35 * the empty value "" is assumed.
36 *
37 * @param key the key
38 * @param value the value
39 */
40 public Tag(String key, String value) {
41 this.key = key == null ? "" : key;
42 this.value = value == null ? "" : value;
43 }
44
45 /**
46 * Creates clone of the tag <code>tag</code>.
47 *
48 * @param tag the tag.
49 */
50 public Tag(Tag tag) {
51 this(tag.getKey(), tag.getValue());
52 }
53
54 /**
55 * Replies the key of the tag. This is never null.
56 *
57 * @return the key of the tag
58 */
59 public String getKey() {
60 return key;
61 }
62
63 /**
64 * Replies the value of the tag. This is never null.
65 *
66 * @return the value of the tag
67 */
68 public String getValue() {
69 return value;
70 }
71
72 /**
73 * Replies true if the key of this tag is equal to <code>key</code>.
74 * If <code>key</code> is null, assumes the empty key.
75 *
76 * @param key the key
77 * @return true if the key of this tag is equal to <code>key</code>
78 */
79 public boolean matchesKey(String key) {
80 return this.key.equals(key);
81 }
82
83 @Override
84 public int hashCode() {
85 final int prime = 31;
86 int result = 1;
87 result = prime * result + key.hashCode();
88 result = prime * result + value.hashCode();
89 return result;
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (obj instanceof Tag) {
95 Tag other = (Tag) obj;
96 return key.equals(other.getKey()) && value.equals(other.getValue());
97 } else
98 return false;
99 }
100
101 /**
102 * This constructs a {@link Tag} by splitting {@code s} on the first equality sign.
103 * @see org.openstreetmap.josm.tools.TextTagParser
104 * @param s the string to convert
105 * @return the constructed tag
106 */
107 public static Tag ofString(String s) {
108 CheckParameterUtil.ensureParameterNotNull(s, "s");
109 final String[] x = s.split("=", 2);
110 if (x.length == 2) {
111 return new Tag(x[0], x[1]);
112 } else {
113 throw new IllegalArgumentException("'" + s + "' does not contain '='");
114 }
115 }
116
117 @Override
118 public String toString() {
119 return key + "=" + value;
120 }
121}
Note: See TracBrowser for help on using the repository browser.