1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Default implementation for IWithAttributes.
|
---|
10 | *
|
---|
11 | * Base class for various classes in the GPX model.
|
---|
12 | *
|
---|
13 | * @author Frederik Ramm
|
---|
14 | * @since 444
|
---|
15 | */
|
---|
16 | public class WithAttributes implements IWithAttributes, GpxConstants {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The "attr" hash is used to store the XML payload (not only XML attributes!)
|
---|
20 | */
|
---|
21 | public Map<String, Object> attr = new HashMap<>(0);
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Returns the Object value to which the specified key is mapped,
|
---|
25 | * or {@code null} if this map contains no mapping for the key.
|
---|
26 | *
|
---|
27 | * @param key the key whose associated value is to be returned
|
---|
28 | * @return the value
|
---|
29 | */
|
---|
30 | @Override
|
---|
31 | public Object get(String key) {
|
---|
32 | return attr.get(key);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns the String value to which the specified key is mapped,
|
---|
37 | * or {@code null} if this map contains no String mapping for the key.
|
---|
38 | *
|
---|
39 | * @param key the key whose associated value is to be returned
|
---|
40 | * @return the String value to which the specified key is mapped,
|
---|
41 | * or {@code null} if this map contains no String mapping for the key
|
---|
42 | */
|
---|
43 | @Override
|
---|
44 | public String getString(String key) {
|
---|
45 | Object value = attr.get(key);
|
---|
46 | return (value instanceof String) ? (String) value : null;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Returns the Collection value to which the specified key is mapped,
|
---|
51 | * or {@code null} if this map contains no Collection mapping for the key.
|
---|
52 | *
|
---|
53 | * @param key the key whose associated value is to be returned
|
---|
54 | * @return the Collection value to which the specified key is mapped,
|
---|
55 | * or {@code null} if this map contains no Collection mapping for the key
|
---|
56 | * @since 5502
|
---|
57 | */
|
---|
58 | @SuppressWarnings("unchecked")
|
---|
59 | @Override
|
---|
60 | public <T> Collection<T> getCollection(String key) {
|
---|
61 | Object value = attr.get(key);
|
---|
62 | return (value instanceof Collection) ? (Collection<T>) value : null;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Put a key / value pair as a new attribute.
|
---|
67 | *
|
---|
68 | * Overrides key / value pair with the same key (if present).
|
---|
69 | *
|
---|
70 | * @param key the key
|
---|
71 | * @param value the value
|
---|
72 | */
|
---|
73 | @Override
|
---|
74 | public void put(String key, Object value) {
|
---|
75 | attr.put(key, value);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Add a key / value pair that is not part of the GPX schema as an extension.
|
---|
80 | *
|
---|
81 | * @param key the key
|
---|
82 | * @param value the value
|
---|
83 | */
|
---|
84 | @Override
|
---|
85 | public void addExtension(String key, String value) {
|
---|
86 | if (!attr.containsKey(META_EXTENSIONS)) {
|
---|
87 | attr.put(META_EXTENSIONS, new Extensions());
|
---|
88 | }
|
---|
89 | Extensions ext = (Extensions) attr.get(META_EXTENSIONS);
|
---|
90 | ext.put(key, value);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public int hashCode() {
|
---|
95 | return 31 + ((attr == null) ? 0 : attr.hashCode());
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public boolean equals(Object obj) {
|
---|
100 | if (this == obj)
|
---|
101 | return true;
|
---|
102 | if (obj == null)
|
---|
103 | return false;
|
---|
104 | if (getClass() != obj.getClass())
|
---|
105 | return false;
|
---|
106 | WithAttributes other = (WithAttributes) obj;
|
---|
107 | if (attr == null) {
|
---|
108 | if (other.attr != null)
|
---|
109 | return false;
|
---|
110 | } else if (!attr.equals(other.attr))
|
---|
111 | return false;
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 | }
|
---|