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

Last change on this file since 3779 was 3479, checked in by jttt, 14 years ago

cosmetics

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package 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 */
9public 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 /**
71 * Replies true if the key of this tag is equal to <code>key</code>.
72 * If <code>key</code> is null, assumes the empty key.
73 *
74 * @param key the key
75 * @return true if the key of this tag is equal to <code>key</code>
76 */
77 public boolean matchesKey(String key) {
78 return this.key.equals(key);
79 }
80
81 /**
82 * Normalizes the key and the value of the tag by
83 * <ul>
84 * <li>removing leading and trailing white space</li>
85 * <ul>
86 *
87 */
88 public void normalize() {
89 key = key.trim();
90 value = value.trim();
91 }
92
93 @Override
94 public int hashCode() {
95 final int prime = 31;
96 int result = 1;
97 result = prime * result + key.hashCode();
98 result = prime * result + value.hashCode();
99 return result;
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (obj instanceof Tag) {
105 Tag other = (Tag) obj;
106 return key.equals(other.getKey()) && value.equals(other.getValue());
107 } else
108 return false;
109 }
110
111 @Override
112 public String toString() {
113 return key + "=" + value;
114 }
115}
Note: See TracBrowser for help on using the repository browser.