CompleteRoute.java

  1. package org.opentrafficsim.core.network.route;

  2. import java.util.List;

  3. import org.opentrafficsim.core.gtu.GTUType;
  4. import org.opentrafficsim.core.network.Link;
  5. import org.opentrafficsim.core.network.Network;
  6. import org.opentrafficsim.core.network.NetworkException;
  7. import org.opentrafficsim.core.network.Node;

  8. /**
  9.  * A CompleteRoute is a Route with directly connected Nodes.
  10.  * <p>
  11.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * $LastChangedDate: 2015-07-16 10:20:53 +0200 (Thu, 16 Jul 2015) $, @version $Revision: 1124 $, by $Author: pknoppers $,
  15.  * initial version Jul 22, 2015 <br>
  16.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  18.  */
  19. public class CompleteRoute extends Route
  20. {
  21.     /** */
  22.     private static final long serialVersionUID = 20150722L;

  23.     /** The GTUType for which this is a route. */
  24.     private final GTUType gtuType;

  25.     /**
  26.      * Create an empty route for the given GTUType.
  27.      * @param id String; the name of the route
  28.      * @param gtuType GTUType; the GTUType for which this is a route
  29.      */
  30.     public CompleteRoute(final String id, final GTUType gtuType)
  31.     {
  32.         super(id);
  33.         this.gtuType = gtuType;
  34.     }

  35.     /**
  36.      * Create a route based on an initial list of nodes. <br>
  37.      * This constructor makes a defensive copy of the provided List.
  38.      * @param id String; the name of the route.
  39.      * @param gtuType GTUType; the GTUType for which this is a route
  40.      * @param nodes List&lt;Node&gt;; the initial list of nodes.
  41.      * @throws NetworkException if intermediate nodes are missing in the route.
  42.      */
  43.     public CompleteRoute(final String id, final GTUType gtuType, final List<Node> nodes) throws NetworkException
  44.     {
  45.         super(id, nodes);
  46.         this.gtuType = gtuType;
  47.         Node fromNode = null;
  48.         for (Node toNode : getNodes())
  49.         {
  50.             if (null != fromNode)
  51.             {
  52.                 if (!fromNode.isDirectionallyConnectedTo(this.gtuType, toNode))
  53.                 {
  54.                     throw new NetworkException("CompleteRoute: node " + fromNode
  55.                             + " not directly or not directionally connected to node " + toNode);
  56.                 }
  57.             }
  58.             fromNode = toNode;
  59.         }
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public final CompleteRoute addNode(final Node node) throws NetworkException
  64.     {
  65.         if (getNodes().size() > 0)
  66.         {
  67.             Node lastNode = getNodes().get(getNodes().size() - 1);
  68.             if (!lastNode.isDirectionallyConnectedTo(this.gtuType, node))
  69.             {
  70.                 throw new NetworkException("CompleteRoute: last node " + lastNode
  71.                         + " not directly or not directionally connected to node " + node);
  72.             }
  73.         }
  74.         super.addNode(node);
  75.         return this;
  76.     }

  77.     /**
  78.      * Determine if this Route contains the specified Link.
  79.      * @param link Link; the link to check in the route.
  80.      * @return whether the link is part of the route or not.
  81.      */
  82.     public final boolean containsLink(final Link link)
  83.     {
  84.         int index1 = getNodes().indexOf(link.getStartNode());
  85.         int index2 = getNodes().indexOf(link.getEndNode());
  86.         return index1 >= 0 && index2 >= 0 && Math.abs(index2 - index1) == 1;
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     public final String toString()
  91.     {
  92.         return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     @SuppressWarnings("checkstyle:designforextension")
  97.     public CompleteRoute clone(final Network newNetwork)
  98.             throws NetworkException
  99.     {
  100.         CompleteRoute newRoute = new CompleteRoute(getId(), this.gtuType);
  101.         for (Node node : getNodes())
  102.         {
  103.             newRoute.addNode(newNetwork.getNode(node.getId()));
  104.         }
  105.         return newRoute;
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  110.     public int hashCode()
  111.     {
  112.         final int prime = 31;
  113.         int result = super.hashCode(); // BE WARE
  114.         result = prime * result + ((gtuType == null) ? 0 : gtuType.hashCode());
  115.         return result;
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  120.     public boolean equals(final Object obj)
  121.     {
  122.         if (this == obj)
  123.             return true;
  124.         if (obj == null)
  125.             return false;
  126.         if (getClass() != obj.getClass())
  127.             return false;
  128.         CompleteRoute other = (CompleteRoute) obj;
  129.         if (gtuType == null)
  130.         {
  131.             if (other.gtuType != null)
  132.                 return false;
  133.         }
  134.         else if (!gtuType.equals(other.gtuType))
  135.             return false;
  136.         return super.equals(other); // BE WARE
  137.     }

  138. }