LongitudinalDirectionality.java

  1. package org.opentrafficsim.core.network;

  2. import java.util.Arrays;
  3. import java.util.LinkedHashSet;

  4. import org.djutils.immutablecollections.Immutable;
  5. import org.djutils.immutablecollections.ImmutableHashSet;
  6. import org.djutils.immutablecollections.ImmutableSet;
  7. import org.opentrafficsim.core.gtu.GTUDirectionality;

  8. /**
  9.  * Permitted longitudinal driving directions.
  10.  * <p>
  11.  * Copyright (c) 2013-2022 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: 2022-02-09 18:05:59 +0100 (Wed, 09 Feb 2022) $, @version $Revision: 7012 $, by $Author: averbraeck $,
  15.  * initial version Oct 15, 2014 <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.  * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
  19.  */
  20. public enum LongitudinalDirectionality
  21. {
  22.     /** Direction the same as the direction of the graph, increasing fractional position when driving in this direction. */
  23.     DIR_PLUS(new GTUDirectionality[] { GTUDirectionality.DIR_PLUS }),
  24.     /** Direction opposite to the direction of the graph, decreasing fractional position when driving in this direction. */
  25.     DIR_MINUS(new GTUDirectionality[] { GTUDirectionality.DIR_MINUS }),
  26.     /** Bidirectional. */
  27.     DIR_BOTH(new GTUDirectionality[] { GTUDirectionality.DIR_PLUS, GTUDirectionality.DIR_MINUS }),
  28.     /** No traffic possible. */
  29.     DIR_NONE(new GTUDirectionality[] {});

  30.     /** Array of permitted driving directions. */
  31.     private final ImmutableSet<GTUDirectionality> directions;

  32.     /**
  33.      * Construct a new LongitudinalDirectionality.
  34.      * @param directions GTUDirectionality[]; array containing the permitted driving directions
  35.      */
  36.     LongitudinalDirectionality(final GTUDirectionality[] directions)
  37.     {
  38.         this.directions = new ImmutableHashSet<>(new LinkedHashSet<>(Arrays.asList(directions)), Immutable.WRAP);
  39.     }

  40.     /**
  41.      * Retrieve the permitted driving directions.
  42.      * @return ImmutableSet&lt;GTUDirectionality&gt;; immutable set containing the permitted driving directions
  43.      */
  44.     public final ImmutableSet<GTUDirectionality> getDirectionalities()
  45.     {
  46.         return this.directions;
  47.     }

  48.     /**
  49.      * This method looks if this directionality "contains" the provided other directionality. The logic table looks as follows:
  50.      * <table border="1">
  51.      * <caption>&nbsp;</caption>
  52.      * <tr>
  53.      * <td><b>THIS &darr; &nbsp; OTHER &rarr;</b></td>
  54.      * <td><b>DIR_BOTH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
  55.      * <td><b>DIR_PLUS&nbsp;&nbsp;&nbsp;</b></td>
  56.      * <td><b>DIR_MINUS</b></td>
  57.      * <td><b>DIR_NONE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
  58.      * </tr>
  59.      * <tr>
  60.      * <td><b>DIR_BOTH</b></td>
  61.      * <td>true</td>
  62.      * <td>true</td>
  63.      * <td>true</td>
  64.      * <td>true</td>
  65.      * </tr>
  66.      * <tr>
  67.      * <td><b>DIR_PLUS</b></td>
  68.      * <td>false</td>
  69.      * <td>true</td>
  70.      * <td>false</td>
  71.      * <td>true</td>
  72.      * </tr>
  73.      * <tr>
  74.      * <td><b>DIR_MINUS</b></td>
  75.      * <td>false</td>
  76.      * <td>false</td>
  77.      * <td>true</td>
  78.      * <td>true</td>
  79.      * </tr>
  80.      * <tr>
  81.      * <td><b>DIR_NONE</b></td>
  82.      * <td>false</td>
  83.      * <td>false</td>
  84.      * <td>false</td>
  85.      * <td>true</td>
  86.      * </tr>
  87.      * </table>
  88.      * @param directionality LongitudinalDirectionality; the directionality to compare with
  89.      * @return whether this directionality "contains" the provided other directionality
  90.      */
  91.     public final boolean contains(final LongitudinalDirectionality directionality)
  92.     {
  93.         return (this.equals(directionality) || this.equals(DIR_BOTH) || directionality.equals(DIR_NONE)) ? true : false;
  94.     }

  95.     /**
  96.      * Easy access method to test if the directionality is FORWARD or BOTH.
  97.      * @return whether the directionality is FORWARD or BOTH
  98.      */
  99.     public final boolean isForwardOrBoth()
  100.     {
  101.         return this.equals(DIR_PLUS) || this.equals(DIR_BOTH);
  102.     }

  103.     /**
  104.      * Easy access method to test if the directionality is BACKWARD or BOTH.
  105.      * @return whether the directionality is BACKWARD or BOTH
  106.      */
  107.     public final boolean isBackwardOrBoth()
  108.     {
  109.         return this.equals(DIR_MINUS) || this.equals(DIR_BOTH);
  110.     }

  111.     /**
  112.      * Easy access method to test if the directionality is FORWARD.
  113.      * @return whether the directionality is FORWARD
  114.      */
  115.     public final boolean isForward()
  116.     {
  117.         return this.equals(DIR_PLUS);
  118.     }

  119.     /**
  120.      * Easy access method to test if the directionality is BACKWARD.
  121.      * @return whether the directionality is BACKWARD
  122.      */
  123.     public final boolean isBackward()
  124.     {
  125.         return this.equals(DIR_MINUS);
  126.     }

  127.     /**
  128.      * Easy access method to test if the directionality is BACKWARD or BOTH.
  129.      * @return whether the directionality is BACKWARD or BOTH
  130.      */
  131.     public final boolean isBoth()
  132.     {
  133.         return this.equals(DIR_BOTH);
  134.     }

  135.     /**
  136.      * Easy access method to test if the directionality is NONE.
  137.      * @return whether the directionality is NONE
  138.      */
  139.     public final boolean isNone()
  140.     {
  141.         return this.equals(DIR_NONE);
  142.     }

  143.     /**
  144.      * Compute the intersection of this LongitudinalDirectionality with another LongitudinalDirectionality.
  145.      * @param other LongitudinalDirectionality; the other LongitudinalDirectionality
  146.      * @return LongitudinalDirectionality; the intersection of <code>this</code> and <code>other</code>
  147.      */
  148.     public final LongitudinalDirectionality intersect(final LongitudinalDirectionality other)
  149.     {
  150.         if (null == other)
  151.         {
  152.             System.err.println("other LongitudinalDirectionality should not be null; returning DIR_NONE");
  153.             return DIR_NONE;
  154.         }
  155.         switch (other)
  156.         {
  157.             case DIR_BOTH:
  158.                 return this;
  159.             case DIR_MINUS:
  160.                 if (this.equals(other))
  161.                 {
  162.                     return this;
  163.                 }
  164.                 if (this.equals(DIR_BOTH))
  165.                 {
  166.                     return other;
  167.                 }
  168.                 return DIR_NONE;
  169.             case DIR_NONE:
  170.                 return other;
  171.             case DIR_PLUS:
  172.                 if (this.equals(other))
  173.                 {
  174.                     return this;
  175.                 }
  176.                 if (this.equals(DIR_BOTH))
  177.                 {
  178.                     return other;
  179.                 }
  180.                 return DIR_NONE;
  181.             default:
  182.                 // Cannot happen (unless someone manages to change this enum).
  183.                 System.err.println("intersect with ???? (returns DIR_NONE)");
  184.                 return DIR_NONE;
  185.         }
  186.     }

  187.     /**
  188.      * Check if a direction is permitted by this LongitudinalDirectionality.
  189.      * @param direction GTUDirectionality; the direction of motion in which a GTU moves, or wants to move
  190.      * @return boolean; true if the direction is permitted by this LongitudinalDirectionality; false if it is not permitted
  191.      */
  192.     public final boolean permits(final GTUDirectionality direction)
  193.     {
  194.         switch (direction)
  195.         {
  196.             case DIR_MINUS:
  197.                 return isBackwardOrBoth();
  198.             case DIR_PLUS:
  199.                 return isForwardOrBoth();
  200.             default:
  201.                 System.out.println("Bad direction: " + direction);
  202.                 return false;
  203.         }
  204.     }

  205.     /**
  206.      * Return the inverse if this LongitudinalDirectionality.
  207.      * @return LongitudinalDirectionality; the directional inverse of this LongitudinalDirectionality
  208.      */
  209.     public LongitudinalDirectionality invert()
  210.     {
  211.         switch (this)
  212.         {
  213.             case DIR_BOTH:
  214.                 return this;
  215.             case DIR_MINUS:
  216.                 return DIR_PLUS;
  217.             case DIR_NONE:
  218.                 return this;
  219.             case DIR_PLUS:
  220.                 return DIR_MINUS;
  221.             default:
  222.                 return this; // cannot happen
  223.         }
  224.     }

  225. }