| 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 | /** |
|---|
| 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 | @Override |
|---|
| 82 | public int hashCode() { |
|---|
| 83 | final int prime = 31; |
|---|
| 84 | int result = 1; |
|---|
| 85 | result = prime * result + key.hashCode(); |
|---|
| 86 | result = prime * result + value.hashCode(); |
|---|
| 87 | return result; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | @Override |
|---|
| 91 | public boolean equals(Object obj) { |
|---|
| 92 | if (obj instanceof Tag) { |
|---|
| 93 | Tag other = (Tag) obj; |
|---|
| 94 | return key.equals(other.getKey()) && value.equals(other.getValue()); |
|---|
| 95 | } else |
|---|
| 96 | return false; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | @Override |
|---|
| 100 | public String toString() { |
|---|
| 101 | return key + "=" + value; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|