1 package org.opentrafficsim.road.network.lane;
2
3 import java.io.Serializable;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.opentrafficsim.base.Type;
9 import org.opentrafficsim.core.gtu.GTUType;
10
11 import nl.tudelft.simulation.immutablecollections.ImmutableHashSet;
12 import nl.tudelft.simulation.immutablecollections.ImmutableSet;
13 import nl.tudelft.simulation.language.Throw;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class LaneType extends Type<LaneType> implements Serializable
30 {
31
32 private static final long serialVersionUID = 20140821L;
33
34
35 private final String id;
36
37
38 private final ImmutableSet<GTUType> compatibilitySet;
39
40
41 public static final LaneType NONE;
42
43
44 public static final LaneType ALL;
45
46 static
47 {
48 NONE = new LaneType("NONE", new HashSet<GTUType>());
49 Set<GTUType> allSet = new HashSet<>();
50 allSet.add(GTUType.ALL);
51 ALL = new LaneType("ALL", allSet);
52 }
53
54
55
56
57
58
59
60 public LaneType(final String id, final Collection<GTUType> compatibility) throws NullPointerException
61 {
62 Throw.whenNull(id, "id cannot be null for LaneType");
63 Throw.whenNull(compatibility, "compatibility collection cannot be null for LaneType with id = %s", id);
64
65 this.id = id;
66 this.compatibilitySet = new ImmutableHashSet<>(compatibility);
67 }
68
69
70
71
72
73 public final boolean isCompatible(final GTUType gtuType)
74 {
75 return this.compatibilitySet.contains(gtuType) || this.compatibilitySet.contains(GTUType.ALL);
76 }
77
78
79
80
81 public final String getId()
82 {
83 return this.id;
84 }
85
86
87 @Override
88 @SuppressWarnings("checkstyle:designforextension")
89 public String toString()
90 {
91 return "LaneType [id=" + this.id + ", compatibilitySet=" + this.compatibilitySet + "]";
92 }
93
94
95 @Override
96 @SuppressWarnings("checkstyle:designforextension")
97 public int hashCode()
98 {
99 final int prime = 31;
100 int result = 1;
101 result = prime * result + this.id.hashCode();
102 return result;
103 }
104
105
106 @Override
107 @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
108 public boolean equals(final Object obj)
109 {
110 if (this == obj)
111 return true;
112 if (obj == null)
113 return false;
114 if (getClass() != obj.getClass())
115 return false;
116 LaneType other = (LaneType) obj;
117 if (!this.id.equals(other.id))
118 return false;
119 return true;
120 }
121
122 }