001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.gpx; 003 004import java.util.Collection; 005import java.util.HashMap; 006import java.util.Map; 007 008/** 009 * Default implementation for IWithAttributes. 010 * 011 * Base class for various classes in the GPX model. 012 * 013 * @author Frederik Ramm <frederik@remote.org> 014 * @since 444 015 */ 016public class WithAttributes implements IWithAttributes, GpxConstants { 017 018 /** 019 * The "attr" hash is used to store the XML payload (not only XML attributes!) 020 */ 021 public Map<String, Object> attr = new HashMap<String, Object>(0); 022 023 /** 024 * Returns the Object value to which the specified key is mapped, 025 * or {@code null} if this map contains no mapping for the key. 026 * 027 * @param key the key whose associated value is to be returned 028 * @return the value 029 */ 030 @Override 031 public Object get(String key) { 032 return attr.get(key); 033 } 034 035 /** 036 * Returns the String value to which the specified key is mapped, 037 * or {@code null} if this map contains no String mapping for the key. 038 * 039 * @param key the key whose associated value is to be returned 040 * @return the String value to which the specified key is mapped, 041 * or {@code null} if this map contains no String mapping for the key 042 */ 043 @Override 044 public String getString(String key) { 045 Object value = attr.get(key); 046 return (value instanceof String) ? (String)value : null; 047 } 048 049 /** 050 * Returns the Collection value to which the specified key is mapped, 051 * or {@code null} if this map contains no Collection mapping for the key. 052 * 053 * @param key the key whose associated value is to be returned 054 * @return the Collection value to which the specified key is mapped, 055 * or {@code null} if this map contains no Collection mapping for the key 056 * @since 5502 057 */ 058 @Override 059 public Collection<?> getCollection(String key) { 060 Object value = attr.get(key); 061 return (value instanceof Collection) ? (Collection<?>)value : null; 062 } 063 064 /** 065 * Put a key / value pair as a new attribute. 066 * 067 * Overrides key / value pair with the same key (if present). 068 * 069 * @param key the key 070 * @param value the value 071 */ 072 @Override 073 public void put(String key, Object value) { 074 attr.put(key, value); 075 } 076 077 /** 078 * Add a key / value pair that is not part of the GPX schema as an extension. 079 * 080 * @param key the key 081 * @param value the value 082 */ 083 @Override 084 public void addExtension(String key, String value) { 085 if (!attr.containsKey(META_EXTENSIONS)) { 086 attr.put(META_EXTENSIONS, new Extensions()); 087 } 088 Extensions ext = (Extensions) attr.get(META_EXTENSIONS); 089 ext.put(key, value); 090 } 091}