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