View Javadoc
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   * Collection of objects, one object per meta data type included.
14   * <p>
15   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 25, 2016 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   */
23  public class MetaData
24  {
25  
26      /** Meta data. */
27      private final Map<MetaDataType<?>, Object> metaDataMap = new HashMap<>();
28  
29      /**
30       * Default constructor.
31       */
32      public MetaData()
33      {
34          //
35      }
36  
37      /**
38       * @param metaData MetaData; meta data to copy into new meta data
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       * @param metaDataType MetaDataType&lt;T&gt;; meta data type
51       * @param <T> class of meta data
52       * @param value T; value of meta data
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       * @param metaDataType MetaDataType&lt;?&gt;; meta data type
62       * @return whether the trajectory contains the meta data of give type
63       */
64      public final boolean contains(final MetaDataType<?> metaDataType)
65      {
66          return this.metaDataMap.containsKey(metaDataType);
67      }
68  
69      /**
70       * @param metaDataType MetaDataType&lt;T&gt;; meta data type
71       * @param <T> class of meta data
72       * @return value of meta data
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       * @return set of meta data types
82       */
83      public final Set<MetaDataType<?>> getMetaDataTypes()
84      {
85          return this.metaDataMap.keySet();
86      }
87  
88      /**
89       * @return number of meta data entries
90       */
91      public final int size()
92      {
93          return this.metaDataMap.size();
94      }
95  
96      /**
97       * @return iterator over meta data entries, removal is not allowed
98       */
99      public final Iterator<Entry<MetaDataType<?>, Object>> getMetaDataIterator()
100     {
101         return new ImmutableIterator<>(this.metaDataMap.entrySet().iterator());
102     }
103 
104     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
146     @Override
147     public final String toString()
148     {
149         return "MetaData [" + this.metaDataMap + "]";
150     }
151 
152 }