View Javadoc
1   package org.opentrafficsim.kpi.sampling.meta;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   import java.util.Map;
7   import java.util.Map.Entry;
8   import java.util.Set;
9   
10  import org.opentrafficsim.base.immutablecollections.ImmutableIterator;
11  
12  import nl.tudelft.simulation.language.Throw;
13  
14  /**
15   * Collection of object sets, one object set per meta data type included. This defines constraints to which meta data has to
16   * comply, e.g. having any of the objects in the set, or covered all in the set, etc., depending on the meta data type.
17   * <p>
18   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 25, 2016 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class MetaDataSet
27  {
28  
29      /** Meta data. */
30      private final Map<MetaDataType<?>, Set<?>> metaDataMap = new HashMap<>();
31  
32      /**
33       * Default constructor.
34       */
35      public MetaDataSet()
36      {
37          //
38      }
39  
40      /**
41       * @param metaDataSet set of meta data to copy into new meta data set
42       */
43      public MetaDataSet(final MetaDataSet metaDataSet)
44      {
45          Throw.whenNull(metaDataSet, "Meta data set may not be null.");
46          for (MetaDataType<?> metaDataType : metaDataSet.metaDataMap.keySet())
47          {
48              this.metaDataMap.put(metaDataType, metaDataSet.metaDataMap.get(metaDataType));
49          }
50      }
51  
52      /**
53       * @param metaDataType meta data type
54       * @param <T> class of meta data
55       * @param values values of meta data
56       */
57      public final <T> void put(final MetaDataType<T> metaDataType, final Set<T> values)
58      {
59          Throw.whenNull(metaDataType, "Meta data type may not be null.");
60          Throw.whenNull(values, "Values may not be null.");
61          this.metaDataMap.put(metaDataType, values);
62      }
63  
64      /**
65       * @param metaDataType meta data type
66       * @return whether the trajectory contains the meta data of give type
67       */
68      public final boolean contains(final MetaDataType<?> metaDataType)
69      {
70          return this.metaDataMap.containsKey(metaDataType);
71      }
72  
73      /**
74       * @param metaDataType meta data type
75       * @param <T> class of meta data
76       * @return value of meta data
77       */
78      @SuppressWarnings("unchecked")
79      public final <T> Set<T> get(final MetaDataType<T> metaDataType)
80      {
81          return (Set<T>) this.metaDataMap.get(metaDataType);
82      }
83  
84      /**
85       * @return set of meta data types
86       */
87      public final Set<MetaDataType<?>> getMetaDataTypes()
88      {
89          return new HashSet<>(this.metaDataMap.keySet());
90      }
91  
92      /**
93       * @return number of meta data entries
94       */
95      public final int size()
96      {
97          return this.metaDataMap.size();
98      }
99  
100     /**
101      * @return iterator over meta data entries, removal is not allowed
102      */
103     public final Iterator<Entry<MetaDataType<?>, Set<?>>> getMetaDataSetIterator()
104     {
105         return new ImmutableIterator<>(this.metaDataMap.entrySet().iterator());
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public final int hashCode()
111     {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + ((this.metaDataMap == null) ? 0 : this.metaDataMap.hashCode());
115         return result;
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public final boolean equals(final Object obj)
121     {
122         if (this == obj)
123         {
124             return true;
125         }
126         if (obj == null)
127         {
128             return false;
129         }
130         if (getClass() != obj.getClass())
131         {
132             return false;
133         }
134         MetaDataSet other = (MetaDataSet) obj;
135         if (this.metaDataMap == null)
136         {
137             if (other.metaDataMap != null)
138             {
139                 return false;
140             }
141         }
142         else if (!this.metaDataMap.equals(other.metaDataMap))
143         {
144             return false;
145         }
146         return true;
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     public final String toString()
152     {
153         return "MetaDataSet [metaDataMap=" + this.metaDataMap + "]";
154     }
155 
156 }