1 package org.opentrafficsim.kpi.sampling.meta;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import java.util.Set;
8
9 import org.opentrafficsim.base.immutablecollections.ImmutableIterator;
10
11 import nl.tudelft.simulation.language.Throw;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class MetaData
25 {
26
27
28 private final Map<MetaDataType<?>, Object> metaDataMap = new HashMap<>();
29
30
31
32
33 public MetaData()
34 {
35
36 }
37
38
39
40
41 public MetaData(final MetaData metaData)
42 {
43 Throw.whenNull(metaData, "Meta data may not be null.");
44 for (MetaDataType<?> metaDataType : metaData.metaDataMap.keySet())
45 {
46 this.metaDataMap.put(metaDataType, metaData.metaDataMap.get(metaDataType));
47 }
48 }
49
50
51
52
53
54
55 public final <T> void put(final MetaDataType<T> metaDataType, final T value)
56 {
57 Throw.whenNull(metaDataType, "Meta data type may not be null.");
58 this.metaDataMap.put(metaDataType, value);
59 }
60
61
62
63
64
65 public final boolean contains(final MetaDataType<?> metaDataType)
66 {
67 return this.metaDataMap.containsKey(metaDataType);
68 }
69
70
71
72
73
74
75 @SuppressWarnings("unchecked")
76 public final <T> T get(final MetaDataType<T> metaDataType)
77 {
78 return (T) this.metaDataMap.get(metaDataType);
79 }
80
81
82
83
84 public final Set<MetaDataType<?>> getMetaDataTypes()
85 {
86 return this.metaDataMap.keySet();
87 }
88
89
90
91
92 public final int size()
93 {
94 return this.metaDataMap.size();
95 }
96
97
98
99
100 public final Iterator<Entry<MetaDataType<?>, Object>> getMetaDataIterator()
101 {
102 return new ImmutableIterator<>(this.metaDataMap.entrySet().iterator());
103 }
104
105
106 @Override
107 public final int hashCode()
108 {
109 final int prime = 31;
110 int result = 1;
111 result = prime * result + ((this.metaDataMap == null) ? 0 : this.metaDataMap.hashCode());
112 return result;
113 }
114
115
116 @Override
117 public final boolean equals(final Object obj)
118 {
119 if (this == obj)
120 {
121 return true;
122 }
123 if (obj == null)
124 {
125 return false;
126 }
127 if (getClass() != obj.getClass())
128 {
129 return false;
130 }
131 MetaData other = (MetaData) obj;
132 if (this.metaDataMap == null)
133 {
134 if (other.metaDataMap != null)
135 {
136 return false;
137 }
138 }
139 else if (!this.metaDataMap.equals(other.metaDataMap))
140 {
141 return false;
142 }
143 return true;
144 }
145
146
147 @Override
148 public final String toString()
149 {
150 return "MetaData [" + this.metaDataMap + "]";
151 }
152
153 }