1 package org.opentrafficsim.kpi.sampling;
2
3 import java.io.Serializable;
4 import java.util.Iterator;
5 import java.util.LinkedHashSet;
6 import java.util.Set;
7
8 import org.djutils.exceptions.Throw;
9 import org.djutils.immutablecollections.ImmutableIterator;
10 import org.opentrafficsim.kpi.interfaces.LaneData;
11 import org.opentrafficsim.kpi.interfaces.LinkData;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class CrossSection implements Serializable
25 {
26
27
28 private static final long serialVersionUID = 20160929L;
29
30
31 private final Set<LanePosition> lanePositions;
32
33
34
35
36
37 public CrossSection(final Set<LanePosition> lanePositions)
38 {
39 Throw.whenNull(lanePositions, "Lane positions may not be null.");
40 this.lanePositions = new LinkedHashSet<>(lanePositions);
41 }
42
43
44
45
46
47
48
49 public CrossSection(final LinkData<?> link, final double fraction) throws SamplingException
50 {
51 Throw.whenNull(link, "Link lane positions may not be null.");
52 this.lanePositions = new LinkedHashSet<>();
53 for (LaneData<?> lane : link.getLaneDatas())
54 {
55 LanePosition lanePosition = new LanePosition(lane, lane.getLength().times(fraction));
56 this.lanePositions.add(lanePosition);
57 }
58 }
59
60
61
62
63
64 public final int size()
65 {
66 return this.lanePositions.size();
67 }
68
69
70
71
72
73 public final Set<LanePosition> getLanePositions()
74 {
75 return new LinkedHashSet<>(this.lanePositions);
76 }
77
78
79
80
81
82 public final Iterator<LanePosition> getIterator()
83 {
84 return new ImmutableIterator<>(this.lanePositions.iterator());
85 }
86
87
88 @Override
89 @SuppressWarnings("checkstyle:designforextension")
90 public String toString()
91 {
92 return "CrossSection [lanePositions=" + this.lanePositions + "]";
93 }
94
95 }