source: josm/trunk/src/org/openstreetmap/josm/data/gpx/WithAttributes.java@ 7005

Last change on this file since 7005 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.gpx;
3
4import java.util.Collection;
5import java.util.HashMap;
6import 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 */
16public 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 @Override
59 public Collection<?> getCollection(String key) {
60 Object value = attr.get(key);
61 return (value instanceof Collection) ? (Collection<?>)value : null;
62 }
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 }
91}
Note: See TracBrowser for help on using the repository browser.