1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.Bounds;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * A gpx track segment consisting of multiple waypoints, that cannot be changed.
|
---|
13 | */
|
---|
14 | public class ImmutableGpxTrackSegment implements GpxTrackSegment {
|
---|
15 |
|
---|
16 | private final List<WayPoint> wayPoints;
|
---|
17 | private final Bounds bounds;
|
---|
18 | private final double length;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constructs a new {@code ImmutableGpxTrackSegment}.
|
---|
22 | * @param wayPoints list of waypoints
|
---|
23 | */
|
---|
24 | public ImmutableGpxTrackSegment(Collection<WayPoint> wayPoints) {
|
---|
25 | this.wayPoints = Collections.unmodifiableList(new ArrayList<>(wayPoints));
|
---|
26 | this.bounds = calculateBounds();
|
---|
27 | this.length = calculateLength();
|
---|
28 | }
|
---|
29 |
|
---|
30 | private Bounds calculateBounds() {
|
---|
31 | Bounds result = null;
|
---|
32 | for (WayPoint wpt: wayPoints) {
|
---|
33 | if (result == null) {
|
---|
34 | result = new Bounds(wpt.getCoor());
|
---|
35 | } else {
|
---|
36 | result.extend(wpt.getCoor());
|
---|
37 | }
|
---|
38 | }
|
---|
39 | return result;
|
---|
40 | }
|
---|
41 |
|
---|
42 | private double calculateLength() {
|
---|
43 | double result = 0.0; // in meters
|
---|
44 | WayPoint last = null;
|
---|
45 | for (WayPoint tpt : wayPoints) {
|
---|
46 | if (last != null) {
|
---|
47 | Double d = last.getCoor().greatCircleDistance(tpt.getCoor());
|
---|
48 | if (!d.isNaN() && !d.isInfinite()) {
|
---|
49 | result += d;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | last = tpt;
|
---|
53 | }
|
---|
54 | return result;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Bounds getBounds() {
|
---|
59 | return bounds == null ? null : new Bounds(bounds);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public Collection<WayPoint> getWayPoints() {
|
---|
64 | return Collections.unmodifiableList(wayPoints);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public double length() {
|
---|
69 | return length;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public int getUpdateCount() {
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public int hashCode() {
|
---|
79 | return 31 + ((wayPoints == null) ? 0 : wayPoints.hashCode());
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public boolean equals(Object obj) {
|
---|
84 | if (this == obj)
|
---|
85 | return true;
|
---|
86 | if (obj == null)
|
---|
87 | return false;
|
---|
88 | if (getClass() != obj.getClass())
|
---|
89 | return false;
|
---|
90 | ImmutableGpxTrackSegment other = (ImmutableGpxTrackSegment) obj;
|
---|
91 | if (wayPoints == null) {
|
---|
92 | if (other.wayPoints != null)
|
---|
93 | return false;
|
---|
94 | } else if (!wayPoints.equals(other.wayPoints))
|
---|
95 | return false;
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 | }
|
---|