View Javadoc
1   package org.opentrafficsim.road.gtu.lane;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djutils.event.Event;
6   import org.djutils.event.EventListener;
7   import org.opentrafficsim.core.gtu.MoveCheckerException;
8   import org.opentrafficsim.core.network.Network;
9   
10  /**
11   * Abstract class that listens to move events of GTUs so checks can be performed.
12   * <p>
13   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public abstract class AbstractLaneBasedMoveChecker implements EventListener
21  {
22      /** */
23      private static final long serialVersionUID = 1L;
24  
25      /** Network. */
26      private final Network network;
27  
28      /**
29       * Constructor.
30       * @param network Network; network
31       */
32      public AbstractLaneBasedMoveChecker(final Network network)
33      {
34          network.addListener(this, Network.GTU_ADD_EVENT);
35          network.addListener(this, Network.GTU_REMOVE_EVENT);
36          this.network = network;
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public void notify(final Event event) throws RemoteException
42      {
43          if (event.getType().equals(LaneBasedGtu.LANEBASED_MOVE_EVENT))
44          {
45              try
46              {
47                  Object[] payload = (Object[]) event.getContent();
48                  checkMove((LaneBasedGtu) this.network.getGTU((String) payload[0]));
49              }
50              catch (Exception ex)
51              {
52                  throw new MoveCheckerException(ex);
53              }
54          }
55          else if (event.getType().equals(Network.GTU_ADD_EVENT))
56          {
57              this.network.getGTU((String) event.getContent()).addListener(this, LaneBasedGtu.LANEBASED_MOVE_EVENT);
58          }
59          else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
60          {
61              this.network.getGTU((String) event.getContent()).removeListener(this, LaneBasedGtu.LANEBASED_MOVE_EVENT);
62          }
63          else
64          {
65              throw new RemoteException("AbstractMoveChecker is a listener to an unknown event type.");
66          }
67      }
68  
69      /**
70       * Check the move of the given GTU.
71       * @param gtu LaneBasedGtu; GTU.
72       * @throws Exception thrown when something is not all right
73       */
74      public abstract void checkMove(LaneBasedGtu gtu) throws Exception;
75  
76  }