1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Tag represents an immutable key/value-pair. Both the key and the value may
|
---|
6 | * be empty, but not null.
|
---|
7 | *
|
---|
8 | */
|
---|
9 | public class Tag {
|
---|
10 |
|
---|
11 | private String key;
|
---|
12 | private String value;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Create an empty tag whose key and value are empty.
|
---|
16 | */
|
---|
17 | public Tag(){
|
---|
18 | this("", "");
|
---|
19 | }
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Create a tag whose key is <code>key</code> and whose value is
|
---|
23 | * empty.
|
---|
24 | *
|
---|
25 | * @param key the key. If null, it is set to the empty key.
|
---|
26 | */
|
---|
27 | public Tag(String key) {
|
---|
28 | this(key, "");
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Creates a tag for a key and a value. If key and/or value are null,
|
---|
33 | * the empty value "" is assumed.
|
---|
34 | *
|
---|
35 | * @param key the key
|
---|
36 | * @param value the value
|
---|
37 | */
|
---|
38 | public Tag(String key, String value) {
|
---|
39 | this.key = key == null ? "" : key;
|
---|
40 | this.value = value == null ? "" : value;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates clone of the tag <code>tag</code>.
|
---|
45 | *
|
---|
46 | * @param tag the tag.
|
---|
47 | */
|
---|
48 | public Tag(Tag tag) {
|
---|
49 | this(tag.getKey(), tag.getValue());
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Replies the key of the tag. This is never null.
|
---|
54 | *
|
---|
55 | * @return the key of the tag
|
---|
56 | */
|
---|
57 | public String getKey() {
|
---|
58 | return key;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Replies the value of the tag. This is never null.
|
---|
63 | *
|
---|
64 | * @return the value of the tag
|
---|
65 | */
|
---|
66 | public String getValue() {
|
---|
67 | return value;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Tag clone() {
|
---|
72 | return new Tag(this);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Replies true if the key of this tag is equal to <code>key</code>.
|
---|
77 | * If <code>key</code> is null, assumes the empty key.
|
---|
78 | *
|
---|
79 | * @param key the key
|
---|
80 | * @return true if the key of this tag is equal to <code>key</code>
|
---|
81 | */
|
---|
82 | public boolean matchesKey(String key) {
|
---|
83 | return this.key.equals(key);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Normalizes the key and the value of the tag by
|
---|
88 | * <ul>
|
---|
89 | * <li>removing leading and trailing white space</li>
|
---|
90 | * <ul>
|
---|
91 | *
|
---|
92 | */
|
---|
93 | public void normalize() {
|
---|
94 | key = key.trim();
|
---|
95 | value = value.trim();
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public int hashCode() {
|
---|
100 | final int prime = 31;
|
---|
101 | int result = 1;
|
---|
102 | result = prime * result + key.hashCode();
|
---|
103 | result = prime * result + value.hashCode();
|
---|
104 | return result;
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public boolean equals(Object obj) {
|
---|
109 | if (obj instanceof Tag) {
|
---|
110 | Tag other = (Tag) obj;
|
---|
111 | return key.equals(other.getKey()) && value.equals(other.getValue());
|
---|
112 | } else
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public String toString() {
|
---|
118 | return key + "=" + value;
|
---|
119 | }
|
---|
120 | }
|
---|