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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  16.  * </p>
  17.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  18.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  19.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  20.  * @param <S> underlying type of path sections
  21.  */
  22. public class GraphCrossSection<S> extends AbstractGraphSpace<S>
  23. {

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

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

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

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

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

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

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

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

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

  102. }