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.base.OtsRuntimeException;
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://github.com/peter-knoppers">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
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      @Override
40      public void notify(final Event event) throws RemoteException
41      {
42          if (event.getType().equals(LaneBasedGtu.LANEBASED_MOVE_EVENT))
43          {
44              try
45              {
46                  Object[] payload = (Object[]) event.getContent();
47                  checkMove((LaneBasedGtu) this.network.getGTU((String) payload[0]));
48              }
49              catch (Exception ex)
50              {
51                  throw new OtsRuntimeException(ex);
52              }
53          }
54          else if (event.getType().equals(Network.GTU_ADD_EVENT))
55          {
56              this.network.getGTU((String) event.getContent()).addListener(this, LaneBasedGtu.LANEBASED_MOVE_EVENT);
57          }
58          else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
59          {
60              this.network.getGTU((String) event.getContent()).removeListener(this, LaneBasedGtu.LANEBASED_MOVE_EVENT);
61          }
62          else
63          {
64              throw new RemoteException("AbstractMoveChecker is a listener to an unknown event type.");
65          }
66      }
67  
68      /**
69       * Check the move of the given GTU.
70       * @param gtu GTU.
71       * @throws Exception thrown when something is not all right
72       */
73      public abstract void checkMove(LaneBasedGtu gtu) throws Exception;
74  
75  }