View Javadoc
1   package ca.uhn.hl7v2.hoh.api;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   public abstract class AbstractReceivable<T> implements IReceivable<T> {
8   	private final Map<String, Object> myMetadata = new HashMap<>();
9   
10  	/**
11  	 * Add a metadata value
12  	 * 
13  	 * @param theKey The key
14  	 * @param theValue The value
15  	 * @throws NullPointerException If theKey is null
16  	 */
17  	public void addMetadata(String theKey, Object theValue) {
18  		if (theKey == null) {
19  			throw new NullPointerException("Key may not be null");
20  		}
21  		
22  		if (theValue != null) {
23  			if (MessageMetadataKeys.keyStringSet().contains(theKey)) {
24  				Class<?> valueType = MessageMetadataKeys.valueOf(theKey).getValueType();
25  				if (!valueType.isAssignableFrom(theValue.getClass())) {
26  					throw new IllegalArgumentException("Value for key \"" + theKey + "\" must be of type: " + valueType.getName());
27  				}
28  			}
29  		}
30  		
31  		myMetadata.put(theKey, theValue);
32  	}
33  	
34  	/**
35  	 * {@inheritDoc}
36  	 */
37  	public Map<String, Object> getMetadata() {
38  		return Collections.unmodifiableMap(myMetadata);
39  	}
40  
41  
42  }