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; 007import java.util.Objects; 008 009/** 010 * Default implementation for IWithAttributes. 011 * 012 * Base class for various classes in the GPX model. 013 * 014 * @author Frederik Ramm 015 * @since 444 016 */ 017public class WithAttributes implements IWithAttributes, GpxConstants { 018 019 /** 020 * The "attr" hash is used to store the XML payload (not only XML attributes!) 021 */ 022 public Map<String, Object> attr = new HashMap<>(0); 023 024 /** 025 * The "exts" collection contains all extensions. 026 */ 027 private final GpxExtensionCollection exts = new GpxExtensionCollection(this); 028 029 /** 030 * Returns the Object value to which the specified key is mapped, 031 * or {@code null} if this map contains no mapping for the key. 032 * 033 * @param key the key whose associated value is to be returned 034 * @return the value 035 */ 036 @Override 037 public Object get(String key) { 038 return attr.get(key); 039 } 040 041 /** 042 * Returns the String value to which the specified key is mapped, 043 * or {@code null} if this map contains no String mapping for the key. 044 * 045 * @param key the key whose associated value is to be returned 046 * @return the String value to which the specified key is mapped, 047 * or {@code null} if this map contains no String mapping for the key 048 */ 049 @Override 050 public String getString(String key) { 051 Object value = attr.get(key); 052 return (value instanceof String) ? (String) value : null; 053 } 054 055 /** 056 * Returns the Collection value to which the specified key is mapped, 057 * or {@code null} if this map contains no Collection mapping for the key. 058 * 059 * @param key the key whose associated value is to be returned 060 * @return the Collection value to which the specified key is mapped, 061 * or {@code null} if this map contains no Collection mapping for the key 062 * @since 5502 063 */ 064 @SuppressWarnings("unchecked") 065 @Override 066 public <T> Collection<T> getCollection(String key) { 067 Object value = attr.get(key); 068 return (value instanceof Collection) ? (Collection<T>) value : null; 069 } 070 071 /** 072 * Put a key / value pair as a new attribute. 073 * Overrides key / value pair with the same key (if present). 074 * 075 * @param key the key 076 * @param value the value 077 */ 078 @Override 079 public void put(String key, Object value) { 080 attr.put(key, value); 081 } 082 083 @Override 084 public Map<String, Object> getAttributes() { 085 return attr; 086 } 087 088 @Override 089 public GpxExtensionCollection getExtensions() { 090 return exts; 091 } 092 093 @Override 094 public int hashCode() { 095 return Objects.hash(attr, exts); 096 } 097 098 @Override 099 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 if (exts == null) { 113 if (other.exts != null) 114 return false; 115 } else if (!exts.equals(other.exts)) 116 return false; 117 return true; 118 } 119}