[5681] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[444] | 2 | package org.openstreetmap.josm.data.gpx;
|
---|
| 3 |
|
---|
[5502] | 4 | import java.util.Collection;
|
---|
[444] | 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.Map;
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
[5681] | 9 | * Default implementation for IWithAttributes.
|
---|
| 10 | *
|
---|
[444] | 11 | * Base class for various classes in the GPX model.
|
---|
[1169] | 12 | *
|
---|
[444] | 13 | * @author Frederik Ramm <frederik@remote.org>
|
---|
[5502] | 14 | * @since 444
|
---|
[444] | 15 | */
|
---|
[5681] | 16 | public class WithAttributes implements IWithAttributes, GpxConstants {
|
---|
[1169] | 17 |
|
---|
[5502] | 18 | /**
|
---|
| 19 | * The "attr" hash is used to store the XML payload (not only XML attributes!)
|
---|
| 20 | */
|
---|
[1169] | 21 | public Map<String, Object> attr = new HashMap<String, Object>(0);
|
---|
| 22 |
|
---|
[5502] | 23 | /**
|
---|
[5684] | 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 | /**
|
---|
[5681] | 36 | * Returns the String value to which the specified key is mapped,
|
---|
[5502] | 37 | * or {@code null} if this map contains no String mapping for the key.
|
---|
[5681] | 38 | *
|
---|
[5502] | 39 | * @param key the key whose associated value is to be returned
|
---|
[5681] | 40 | * @return the String value to which the specified key is mapped,
|
---|
[5502] | 41 | * or {@code null} if this map contains no String mapping for the key
|
---|
| 42 | */
|
---|
[5681] | 43 | @Override
|
---|
[1169] | 44 | public String getString(String key) {
|
---|
| 45 | Object value = attr.get(key);
|
---|
| 46 | return (value instanceof String) ? (String)value : null;
|
---|
| 47 | }
|
---|
[5681] | 48 |
|
---|
[5502] | 49 | /**
|
---|
[5681] | 50 | * Returns the Collection value to which the specified key is mapped,
|
---|
[5502] | 51 | * or {@code null} if this map contains no Collection mapping for the key.
|
---|
[5681] | 52 | *
|
---|
[5502] | 53 | * @param key the key whose associated value is to be returned
|
---|
[5681] | 54 | * @return the Collection value to which the specified key is mapped,
|
---|
[5502] | 55 | * or {@code null} if this map contains no Collection mapping for the key
|
---|
| 56 | * @since 5502
|
---|
| 57 | */
|
---|
[5681] | 58 | @Override
|
---|
[5684] | 59 | public Collection getCollection(String key) {
|
---|
[5502] | 60 | Object value = attr.get(key);
|
---|
[5684] | 61 | return (value instanceof Collection) ? (Collection)value : null;
|
---|
[5502] | 62 | }
|
---|
[5684] | 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Put a key / value pair as a new attribute.
|
---|
| 66 | *
|
---|
| 67 | * Overrides key / value pair with the same key (if present).
|
---|
| 68 | *
|
---|
| 69 | * @param key the key
|
---|
| 70 | * @param value the value
|
---|
| 71 | */
|
---|
| 72 | @Override
|
---|
| 73 | public void put(String key, Object value) {
|
---|
| 74 | attr.put(key, value);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Add a key / value pair that is not part of the GPX schema as an extension.
|
---|
| 79 | *
|
---|
| 80 | * @param key the key
|
---|
| 81 | * @param value the value
|
---|
| 82 | */
|
---|
| 83 | @Override
|
---|
| 84 | public void addExtension(String key, String value) {
|
---|
| 85 | if (!attr.containsKey(META_EXTENSIONS)) {
|
---|
| 86 | attr.put(META_EXTENSIONS, new Extensions());
|
---|
| 87 | }
|
---|
| 88 | Extensions ext = (Extensions) attr.get(META_EXTENSIONS);
|
---|
| 89 | ext.put(key, value);
|
---|
| 90 | }
|
---|
[444] | 91 | }
|
---|