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://tudelft.nl/staff/p.knoppers-1">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; 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; categorization
46       * @param object1 Object; 1st object
47       * @param objects Object...; 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 int; 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&lt;T&gt;; class from categorization to get the category object for
98       * @param <T> type of the object
99       * @return Object; 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     /** {@inheritDoc} */
118     @Override
119     public final int hashCode()
120     {
121         final int prime = 31;
122         int result = 1;
123         result = prime * result + ((this.categorization == null) ? 0 : this.categorization.hashCode());
124         result = prime * result + ((this.objects == null) ? 0 : this.objects.hashCode());
125         return result;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public final boolean equals(final Object obj)
131     {
132         if (this == obj)
133         {
134             return true;
135         }
136         if (obj == null)
137         {
138             return false;
139         }
140         if (getClass() != obj.getClass())
141         {
142             return false;
143         }
144         Category other = (Category) obj;
145         if (this.categorization == null)
146         {
147             if (other.categorization != null)
148             {
149                 return false;
150             }
151         }
152         else if (!this.categorization.equals(other.categorization))
153         {
154             return false;
155         }
156         if (this.objects == null)
157         {
158             if (other.objects != null)
159             {
160                 return false;
161             }
162         }
163         else if (!this.objects.equals(other.objects))
164         {
165             return false;
166         }
167         return true;
168     }
169 
170     /** {@inheritDoc} */
171     @Override
172     public final String toString()
173     {
174         return "Category [" + this.objects + "]";
175     }
176 
177 }