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