View Javadoc
1   package org.opentrafficsim.draw.graphs;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djutils.immutablecollections.Immutable;
9   import org.djutils.immutablecollections.ImmutableArrayList;
10  import org.opentrafficsim.draw.graphs.GraphPath.Section;
11  
12  /**
13   * A {@code GraphCrossSection} defines the location of graphs. It has one section having one or more source objects depending on
14   * the number of series. For example, a 3-lane road may result in a section with 3 series. Graphs can aggregate the series, or
15   * show multiple series.
16   * <p>
17   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   * @param <S> underlying type of path sections
24   */
25  public class GraphCrossSection<S> extends AbstractGraphSpace<S>
26  {
27  
28      /** Section. */
29      private final Section<S> section;
30  
31      /** Position on the section. */
32      private final List<Length> positions;
33  
34      /**
35       * Constructor for a one-series cross section.
36       * @param seriesName String; name of series
37       * @param section Section&lt;S&gt;; section
38       * @param position Length; position on the section
39       */
40      public GraphCrossSection(final String seriesName, final Section<S> section, final Length position)
41      {
42          this(new ArrayList<String>()
43          {
44              /** */
45              private static final long serialVersionUID = 20181022L;
46              {
47                  add(seriesName);
48              }
49          }, section, new ArrayList<Length>()
50          {
51              /** */
52              private static final long serialVersionUID = 20181022L;
53              {
54                  add(position);
55              }
56          });
57      }
58  
59      /**
60       * Constructor.
61       * @param seriesNames List&lt;String&gt;; names of series
62       * @param section Section&lt;S&gt;; section
63       * @param positions List&lt;Length&gt;; position on the section
64       */
65      public GraphCrossSection(final List<String> seriesNames, final Section<S> section, final List<Length> positions)
66      {
67          super(seriesNames);
68          this.section = section;
69          this.positions = positions;
70      }
71  
72      /**
73       * Returns the underlying source of the series.
74       * @param series int; series number
75       * @return S; underlying source of the series
76       */
77      public S getSource(final int series)
78      {
79          return this.section.getSource(series);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public Iterator<S> iterator(final int series)
85      {
86          List<S> list = new ArrayList<>();
87          list.add(this.section.getSource(series));
88          return new ImmutableArrayList<>(list, Immutable.WRAP).iterator();
89      }
90  
91      /**
92       * Returns the position on the underlying source of the series.
93       * @param series int; series number
94       * @return Length; position on the underlying source of the series
95       */
96      public Length position(final int series)
97      {
98          return this.positions.get(series);
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public Iterator<S> iterator()
104     {
105         return this.section.iterator();
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public String toString()
111     {
112         return "GraphCrossSection [section=" + this.section + ", positions=" + this.positions + "]";
113     }
114 
115 }