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