View Javadoc
1   package org.opentrafficsim.road.od;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.djutils.base.Identifiable;
8   import org.djutils.exceptions.Throw;
9   
10  /**
11   * A categorization determines for what part of traffic certain demand data is applicable. By default, this is always for a
12   * given origin-destination pair and time period. A categorization adds to this additional segregation. For example, per lane,
13   * per vehicle class, etc.
14   * <p>
15   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * </p>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public class Categorization implements Serializable, Identifiable
23  {
24  
25      /** Empty categorization. */
26      public static final Categorization UNCATEGORIZED = new Categorization("Uncategorized");
27  
28      /** */
29      private static final long serialVersionUID = 20160921L;
30  
31      /** Id. */
32      private final String id;
33  
34      /** Set of categorization classes. */
35      private final List<Class<?>> classes = new ArrayList<>();
36  
37      /**
38       * @param id String; id
39       */
40      private Categorization(final String id)
41      {
42          Throw.whenNull(id, "Id may not be null.");
43          this.id = id;
44      }
45  
46      /**
47       * @param id String; Id
48       * @param class1 Class&lt;?&gt;; 1st class
49       * @param classes Class&lt;?&gt;...; other classes
50       * @throws IllegalArgumentException if any class is given multiple times
51       * @throws NullPointerException if any input is null
52       */
53      public Categorization(final String id, final Class<?> class1, final Class<?>... classes)
54      {
55          this(id);
56          Throw.whenNull(class1, "Classes may not be null.");
57          this.classes.add(class1);
58          for (Class<?> clazz : classes)
59          {
60              Throw.whenNull(clazz, "Classes may not be null.");
61              Throw.when(this.classes.contains(clazz), IllegalArgumentException.class, "Class %s is given multiple times.",
62                      clazz);
63              this.classes.add(clazz);
64          }
65      }
66  
67      /**
68       * Returns the number of category classes defined.
69       * @return number of category classes defined
70       */
71      public final int size()
72      {
73          return this.classes.size();
74      }
75  
76      /**
77       * Returns the i'th class.
78       * @param i int; index of the class
79       * @return the i'th class
80       * @throws IndexOutOfBoundsException if index i is out of bounds
81       */
82      public final Class<?> get(final int i)
83      {
84          Throw.when(i < 0 || i >= size(), IndexOutOfBoundsException.class,
85                  "Index %d is out of range for categorization of size %d.", i, size());
86          return this.classes.get(i);
87      }
88  
89      /**
90       * @return id.
91       */
92      @Override
93      public final String getId()
94      {
95          return this.id;
96      }
97  
98      /**
99       * Returns whether the categorization contains a class that is, or is a sub type of, the given class.
100      * @param clazz Class&lt;?&gt;; class to check
101      * @return whether the categorization contains a class that is, or is a sub type of, the given class
102      */
103     public final boolean entails(final Class<?> clazz)
104     {
105         for (Class<?> clazz2 : this.classes)
106         {
107             if (clazz.isAssignableFrom(clazz2))
108             {
109                 return true;
110             }
111         }
112         return false;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final int hashCode()
118     {
119         final int prime = 31;
120         int result = 1;
121         result = prime * result + ((this.classes == null) ? 0 : this.classes.hashCode());
122         return result;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public final boolean equals(final Object obj)
128     {
129         if (this == obj)
130         {
131             return true;
132         }
133         if (obj == null)
134         {
135             return false;
136         }
137         if (getClass() != obj.getClass())
138         {
139             return false;
140         }
141         Categorization other = (Categorization) obj;
142         if (this.classes == null)
143         {
144             if (other.classes != null)
145             {
146                 return false;
147             }
148         }
149         else if (!this.classes.equals(other.classes))
150         {
151             return false;
152         }
153         return true;
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     public final String toString()
159     {
160         return "Categorization [id=" + this.id + ", classes=" + this.classes + "]";
161     }
162 
163 }