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