1 package org.opentrafficsim.road.gtu.strategical.od;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import nl.tudelft.simulation.language.Throw;
8
9 /**
10 * A categorization determines for what part of traffic certain demand data is applicable. By default, this is always for a
11 * given origin-destination pair and time period. A categorization adds to this additional segregation. For example, per lane,
12 * per vehicle class, etc.
13 * <p>
14 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
16 * <p>
17 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 15, 2016 <br>
18 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21 */
22 public class Categorization implements Serializable
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 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 Id
48 * @param class1 1st class
49 * @param classes 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,
62 "Class %s is given multiple times.", 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 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 public final String getId()
93 {
94 return this.id;
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public final int hashCode()
100 {
101 final int prime = 31;
102 int result = 1;
103 result = prime * result + ((this.classes == null) ? 0 : this.classes.hashCode());
104 return result;
105 }
106
107 /** {@inheritDoc} */
108 @Override
109 public final boolean equals(final Object obj)
110 {
111 if (this == obj)
112 {
113 return true;
114 }
115 if (obj == null)
116 {
117 return false;
118 }
119 if (getClass() != obj.getClass())
120 {
121 return false;
122 }
123 Categorization other = (Categorization) obj;
124 if (this.classes == null)
125 {
126 if (other.classes != null)
127 {
128 return false;
129 }
130 }
131 else if (!this.classes.equals(other.classes))
132 {
133 return false;
134 }
135 return true;
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public final String toString()
141 {
142 return "Categorization [id=" + this.id + ", classes=" + this.classes + "]";
143 }
144
145 }