GraphCrossSection.java

  1. package org.opentrafficsim.draw.graphs;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;

  5. import org.djunits.value.vdouble.scalar.Length;
  6. import org.djutils.immutablecollections.Immutable;
  7. import org.djutils.immutablecollections.ImmutableArrayList;
  8. import org.opentrafficsim.draw.graphs.GraphPath.Section;

  9. /**
  10.  * A {@code GraphCrossSection} defines the location of graphs. It has one section having one or more source objects depending on
  11.  * the number of series. For example, a 3-lane road may result in a section with 3 series. Graphs can aggregate the series, or
  12.  * show multiple series.
  13.  * <p>
  14.  * Copyright (c) 2013-2020 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 22 okt. 2018 <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.  * @param <S> underlying type of path sections
  22.  */
  23. public class GraphCrossSection<S> extends AbstractGraphSpace<S>
  24. {

  25.     /** Section. */
  26.     private final Section<S> section;

  27.     /** Position on the section. */
  28.     private final List<Length> positions;

  29.     /**
  30.      * Constructor for a one-series cross section.
  31.      * @param seriesName String; name of series
  32.      * @param section Section&lt;S&gt;; section
  33.      * @param position Length; position on the section
  34.      */
  35.     public GraphCrossSection(final String seriesName, final Section<S> section, final Length position)
  36.     {
  37.         this(new ArrayList<String>()
  38.         {
  39.             /** */
  40.             private static final long serialVersionUID = 20181022L;
  41.             {
  42.                 add(seriesName);
  43.             }
  44.         }, section, new ArrayList<Length>()
  45.         {
  46.             /** */
  47.             private static final long serialVersionUID = 20181022L;
  48.             {
  49.                 add(position);
  50.             }
  51.         });
  52.     }

  53.     /**
  54.      * Constructor.
  55.      * @param seriesNames List&lt;String&gt;; names of series
  56.      * @param section Section&lt;S&gt;; section
  57.      * @param positions List&lt;Length&gt;; position on the section
  58.      */
  59.     public GraphCrossSection(final List<String> seriesNames, final Section<S> section, final List<Length> positions)
  60.     {
  61.         super(seriesNames);
  62.         this.section = section;
  63.         this.positions = positions;
  64.     }

  65.     /**
  66.      * Returns the underlying source of the series.
  67.      * @param series int; series number
  68.      * @return S; underlying source of the series
  69.      */
  70.     public S getSource(final int series)
  71.     {
  72.         return this.section.getSource(series);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public Iterator<S> iterator(final int series)
  77.     {
  78.         List<S> list = new ArrayList<>();
  79.         list.add(this.section.getSource(series));
  80.         return new ImmutableArrayList<>(list, Immutable.WRAP).iterator();
  81.     }

  82.     /**
  83.      * Returns the position on the underlying source of the series.
  84.      * @param series int; series number
  85.      * @return Length; position on the underlying source of the series
  86.      */
  87.     public Length position(final int series)
  88.     {
  89.         return this.positions.get(series);
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public Iterator<S> iterator()
  94.     {
  95.         return this.section.iterator();
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     public String toString()
  100.     {
  101.         return "GraphCrossSection [section=" + this.section + ", positions=" + this.positions + "]";
  102.     }

  103. }