001package ca.uhn.hl7v2.hoh.api; 002 003import java.util.Collections; 004import java.util.HashMap; 005import java.util.Map; 006 007public abstract class AbstractReceivable<T> implements IReceivable<T> { 008 private final Map<String, Object> myMetadata = new HashMap<String, Object>(); 009 010 /** 011 * Add a metadata value 012 * 013 * @param theKey The key 014 * @param theValue The value 015 * @throws NullPointerException If theKey is null 016 */ 017 public void addMetadata(String theKey, Object theValue) { 018 if (theKey == null) { 019 throw new NullPointerException("Key may not be null"); 020 } 021 022 if (theValue != null) { 023 if (MessageMetadataKeys.keyStringSet().contains(theKey)) { 024 Class<?> valueType = MessageMetadataKeys.valueOf(theKey).getValueType(); 025 if (!valueType.isAssignableFrom(theValue.getClass())) { 026 throw new IllegalArgumentException("Value for key \"" + theKey + "\" must be of type: " + valueType.getName()); 027 } 028 } 029 } 030 031 myMetadata.put(theKey, theValue); 032 } 033 034 /** 035 * {@inheritDoc} 036 */ 037 public Map<String, Object> getMetadata() { 038 return Collections.unmodifiableMap(myMetadata); 039 } 040 041 042}