View Javadoc
1   package org.opentrafficsim.road.network.lane.conflict;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.opentrafficsim.road.network.lane.CrossSectionLink;
9   import org.opentrafficsim.road.network.lane.Lane;
10  
11  /**
12   * Contains lane combinations that should be treated differently.
13   * <p>
14   * Copyright (c) 2013-2019 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/node/13">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 23 dec. 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 LaneCombinationList
23  {
24  
25      /** Lane combinations. Each combination is contained in both directions. */
26      private final Map<Lane, Set<Lane>> map = new HashMap<>();
27  
28      /**
29       * Add any combination of lanes on both links to the list. Order of the links does not matter.
30       * @param link1 CrossSectionLink; link 1
31       * @param link2 CrossSectionLink; link 2
32       */
33      public final void addLinkCombination(final CrossSectionLink link1, final CrossSectionLink link2)
34      {
35          for (Lane lane1 : link1.getLanes())
36          {
37              for (Lane lane2 : link2.getLanes())
38              {
39                  addLaneCombination(lane1, lane2);
40              }
41          }
42      }
43  
44      /**
45       * Add lane combination to the list. Order of the lanes does not matter.
46       * @param lane1 Lane; lane 1
47       * @param lane2 Lane; lane 2
48       */
49      public final void addLaneCombination(final Lane lane1, final Lane lane2)
50      {
51          if (!this.map.containsKey(lane1))
52          {
53              this.map.put(lane1, new HashSet<>());
54          }
55          this.map.get(lane1).add(lane2);
56          if (!this.map.containsKey(lane2))
57          {
58              this.map.put(lane2, new HashSet<>());
59          }
60          this.map.get(lane2).add(lane1);
61      }
62  
63      /**
64       * Returns whether the combination of the two lanes is included. Order of the lanes does not matter.
65       * @param lane1 Lane; lane 1
66       * @param lane2 Lane; lane 2
67       * @return whether the combination of the two lanes is included
68       */
69      public final boolean contains(final Lane lane1, final Lane lane2)
70      {
71          if (!this.map.containsKey(lane1))
72          {
73              return false;
74          }
75          return this.map.get(lane1).contains(lane2);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final String toString()
81      {
82          return "LaneCombinationList [map=" + this.map + "]";
83      }
84  
85  }