1 package org.opentrafficsim.road.network.conflict;
2
3 import java.util.LinkedHashMap;
4 import java.util.LinkedHashSet;
5 import java.util.Map;
6 import java.util.Set;
7
8 import org.opentrafficsim.road.network.CrossSectionLink;
9 import org.opentrafficsim.road.network.Lane;
10
11 /**
12 * Contains lane combinations that should be treated differently.
13 * <p>
14 * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16 * </p>
17 * @author Alexander Verbraeck
18 * @author Peter Knoppers
19 * @author Wouter Schakel
20 */
21 public class LaneCombinationList
22 {
23
24 /** Lane combinations. Each combination is contained in both directions. */
25 private final Map<Lane, Set<Lane>> map = new LinkedHashMap<>();
26
27 /**
28 * Constructor.
29 */
30 public LaneCombinationList()
31 {
32 //
33 }
34
35 /**
36 * Add any combination of lanes on both links to the list. Order of the links does not matter.
37 * @param link1 link 1
38 * @param link2 link 2
39 */
40 public final void addLinkCombination(final CrossSectionLink link1, final CrossSectionLink link2)
41 {
42 for (Lane lane1 : link1.getLanes())
43 {
44 for (Lane lane2 : link2.getLanes())
45 {
46 addLaneCombination(lane1, lane2);
47 }
48 }
49 }
50
51 /**
52 * Add lane combination to the list. Order of the lanes does not matter.
53 * @param lane1 lane 1
54 * @param lane2 lane 2
55 */
56 public final void addLaneCombination(final Lane lane1, final Lane lane2)
57 {
58 if (!this.map.containsKey(lane1))
59 {
60 this.map.put(lane1, new LinkedHashSet<>());
61 }
62 this.map.get(lane1).add(lane2);
63 if (!this.map.containsKey(lane2))
64 {
65 this.map.put(lane2, new LinkedHashSet<>());
66 }
67 this.map.get(lane2).add(lane1);
68 }
69
70 /**
71 * Returns whether the combination of the two lanes is included. Order of the lanes does not matter.
72 * @param lane1 lane 1
73 * @param lane2 lane 2
74 * @return whether the combination of the two lanes is included
75 */
76 public final boolean contains(final Lane lane1, final Lane lane2)
77 {
78 if (!this.map.containsKey(lane1))
79 {
80 return false;
81 }
82 return this.map.get(lane1).contains(lane2);
83 }
84
85 @Override
86 public final String toString()
87 {
88 return "LaneCombinationList [map=" + this.map + "]";
89 }
90
91 }