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.exceptions.Throw;
8   
9   /**
10   * A category is a set of objects who's class belongs to a certain categorization. One {@code Category} object can specify to
11   * which subset of traffic between on origin and destination certain demand data belongs.
12   * <p>
13   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public class Category implements Serializable
21  {
22  
23      /** Empty category. */
24      public static final Category UNCATEGORIZED = new Category(Categorization.UNCATEGORIZED);
25  
26      /** */
27      private static final long serialVersionUID = 20160921L;
28  
29      /** Categorization. */
30      private final Categorization categorization;
31  
32      /** List of objects defining the category. */
33      private final List<Object> objects = new ArrayList<>();
34  
35      /**
36       * @param categorization categorization
37       */
38      private Category(final Categorization categorization)
39      {
40          Throw.whenNull(categorization, "Categorization may not be null.");
41          this.categorization = categorization;
42      }
43  
44      /**
45       * @param categorization categorization
46       * @param object1 1st object
47       * @param objects other objects
48       * @throws IllegalArgumentException if the objects do not comply with the categorization
49       * @throws NullPointerException if any input is null
50       */
51      public Category(final Categorization categorization, final Object object1, final Object... objects)
52      {
53          this(categorization);
54          Throw.when(categorization.size() != objects.length + 1, IllegalArgumentException.class,
55                  "Objects do not comply with the categorization; bad number of objects.");
56          Throw.whenNull(object1, "Objects may not be null.");
57          Throw.when(!categorization.get(0).isAssignableFrom(object1.getClass()), IllegalArgumentException.class,
58                  "Objects do not comply with the categorization; object 1 is of type %s, should be %s.", object1.getClass(),
59                  categorization.get(0));
60          for (int i = 1; i < categorization.size(); i++)
61          {
62              Throw.whenNull(objects[i - 1], "Objects may not be null.");
63              Throw.when(!categorization.get(i).isAssignableFrom(objects[i - 1].getClass()), IllegalArgumentException.class,
64                      "Objects do not comply with the categorization; object %d is of type %s, should be %s.", i + 1,
65                      objects[i - 1].getClass(), categorization.get(i));
66          }
67          this.objects.add(object1);
68          for (Object object : objects)
69          {
70              this.objects.add(object);
71          }
72      }
73  
74      /**
75       * Returns the i'th object.
76       * @param i index of the object
77       * @return the i'th object
78       * @throws IndexOutOfBoundsException if index i is out of bounds
79       */
80      public final Object get(final int i)
81      {
82          Throw.when(i < 0 || i >= this.objects.size(), IndexOutOfBoundsException.class,
83                  "Index %d is out of range for categorization of size %d", i, this.objects.size());
84          return this.objects.get(i);
85      }
86  
87      /**
88       * @return categorization.
89       */
90      public final Categorization getCategorization()
91      {
92          return this.categorization;
93      }
94  
95      /**
96       * Returns the object of this category pertaining to the specified class from the category.
97       * @param clazz class from categorization to get the category object for
98       * @param <T> type of the object
99       * @return object of this category pertaining to the specified class from the category
100      */
101     @SuppressWarnings("unchecked")
102     public final <T> T get(final Class<T> clazz)
103     {
104         Throw.when(!this.categorization.entails(clazz), RuntimeException.class,
105                 "Trying to get a categorization object by class, with the class not belonging to the categorization.");
106         for (Object obj : this.objects)
107         {
108             if (clazz.isAssignableFrom(obj.getClass()))
109             {
110                 return (T) obj; // assignable so safe
111             }
112         }
113         throw new RuntimeException(
114                 "Categorization contains no object assignable to given class, even though the categorization entails the class.");
115     }
116 
117     @Override
118     public final int hashCode()
119     {
120         final int prime = 31;
121         int result = 1;
122         result = prime * result + ((this.categorization == null) ? 0 : this.categorization.hashCode());
123         result = prime * result + ((this.objects == null) ? 0 : this.objects.hashCode());
124         return result;
125     }
126 
127     @Override
128     public final boolean equals(final Object obj)
129     {
130         if (this == obj)
131         {
132             return true;
133         }
134         if (obj == null)
135         {
136             return false;
137         }
138         if (getClass() != obj.getClass())
139         {
140             return false;
141         }
142         Category other = (Category) obj;
143         if (this.categorization == null)
144         {
145             if (other.categorization != null)
146             {
147                 return false;
148             }
149         }
150         else if (!this.categorization.equals(other.categorization))
151         {
152             return false;
153         }
154         if (this.objects == null)
155         {
156             if (other.objects != null)
157             {
158                 return false;
159             }
160         }
161         else if (!this.objects.equals(other.objects))
162         {
163             return false;
164         }
165         return true;
166     }
167 
168     @Override
169     public final String toString()
170     {
171         return "Category [" + this.objects + "]";
172     }
173 
174 }