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 org.opentrafficsim.base.Identifiable;
8
9 import nl.tudelft.simulation.language.Throw;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class Categorization implements Serializable, Identifiable
25 {
26
27
28 public static final Categorization UNCATEGORIZED = new Categorization("Uncategorized");
29
30
31 private static final long serialVersionUID = 20160921L;
32
33
34 private final String id;
35
36
37 private final List<Class<?>> classes = new ArrayList<>();
38
39
40
41
42 private Categorization(final String id)
43 {
44 Throw.whenNull(id, "Id may not be null.");
45 this.id = id;
46 }
47
48
49
50
51
52
53
54
55 public Categorization(final String id, final Class<?> class1, final Class<?>... classes)
56 {
57 this(id);
58 Throw.whenNull(class1, "Classes may not be null.");
59 this.classes.add(class1);
60 for (Class<?> clazz : classes)
61 {
62 Throw.whenNull(clazz, "Classes may not be null.");
63 Throw.when(this.classes.contains(clazz), IllegalArgumentException.class, "Class %s is given multiple times.",
64 clazz);
65 this.classes.add(clazz);
66 }
67 }
68
69
70
71
72
73 public final int size()
74 {
75 return this.classes.size();
76 }
77
78
79
80
81
82
83
84 public final Class<?> get(final int i)
85 {
86 Throw.when(i < 0 || i >= size(), IndexOutOfBoundsException.class,
87 "Index %d is out of range for categorization of size %d.", i, size());
88 return this.classes.get(i);
89 }
90
91
92
93
94 @Override
95 public final String getId()
96 {
97 return this.id;
98 }
99
100
101
102
103
104
105 public final boolean entails(final Class<?> clazz)
106 {
107 for (Class<?> clazz2 : this.classes)
108 {
109 if (clazz.isAssignableFrom(clazz2))
110 {
111 return true;
112 }
113 }
114 return false;
115 }
116
117
118 @Override
119 public final int hashCode()
120 {
121 final int prime = 31;
122 int result = 1;
123 result = prime * result + ((this.classes == null) ? 0 : this.classes.hashCode());
124 return result;
125 }
126
127
128 @Override
129 public final boolean equals(final Object obj)
130 {
131 if (this == obj)
132 {
133 return true;
134 }
135 if (obj == null)
136 {
137 return false;
138 }
139 if (getClass() != obj.getClass())
140 {
141 return false;
142 }
143 Categorization other = (Categorization) obj;
144 if (this.classes == null)
145 {
146 if (other.classes != null)
147 {
148 return false;
149 }
150 }
151 else if (!this.classes.equals(other.classes))
152 {
153 return false;
154 }
155 return true;
156 }
157
158
159 @Override
160 public final String toString()
161 {
162 return "Categorization [id=" + this.id + ", classes=" + this.classes + "]";
163 }
164
165 }