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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19 * <p>
20 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 22 okt. 2018 <br>
21 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24 * @param <S> underlying type of path sections
25 */
26 public class GraphCrossSection<S> extends AbstractGraphSpace<S>
27 {
28
29 /** Section. */
30 private final Section<S> section;
31
32 /** Position on the section. */
33 private final List<Length> positions;
34
35 /**
36 * Constructor for a one-series cross section.
37 * @param seriesName String; name of series
38 * @param section Section<S>; section
39 * @param position Length; position on the section
40 */
41 public GraphCrossSection(final String seriesName, final Section<S> section, final Length position)
42 {
43 this(new ArrayList<String>()
44 {
45 /** */
46 private static final long serialVersionUID = 20181022L;
47 {
48 add(seriesName);
49 }
50 }, section, new ArrayList<Length>()
51 {
52 /** */
53 private static final long serialVersionUID = 20181022L;
54 {
55 add(position);
56 }
57 });
58 }
59
60 /**
61 * Constructor.
62 * @param seriesNames List<String>; names of series
63 * @param section Section<S>; section
64 * @param positions List<Length>; position on the section
65 */
66 public GraphCrossSection(final List<String> seriesNames, final Section<S> section, final List<Length> positions)
67 {
68 super(seriesNames);
69 this.section = section;
70 this.positions = positions;
71 }
72
73 /**
74 * Returns the underlying source of the series.
75 * @param series int; series number
76 * @return S; underlying source of the series
77 */
78 public S getSource(final int series)
79 {
80 return this.section.getSource(series);
81 }
82
83 /** {@inheritDoc} */
84 @Override
85 public Iterator<S> iterator(final int series)
86 {
87 List<S> list = new ArrayList<>();
88 list.add(this.section.getSource(series));
89 return new ImmutableArrayList<>(list, Immutable.WRAP).iterator();
90 }
91
92 /**
93 * Returns the position on the underlying source of the series.
94 * @param series int; series number
95 * @return Length; position on the underlying source of the series
96 */
97 public Length position(final int series)
98 {
99 return this.positions.get(series);
100 }
101
102 /** {@inheritDoc} */
103 @Override
104 public Iterator<S> iterator()
105 {
106 return this.section.iterator();
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 public String toString()
112 {
113 return "GraphCrossSection [section=" + this.section + ", positions=" + this.positions + "]";
114 }
115
116 }