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 | public class ImmutableGpxTrackSegment implements GpxTrackSegment {
|
---|
12 |
|
---|
13 | private final List<WayPoint> wayPoints;
|
---|
14 | private final Bounds bounds;
|
---|
15 | private final double length;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Constructs a new {@code ImmutableGpxTrackSegment}.
|
---|
19 | * @param wayPoints list of waypoints
|
---|
20 | */
|
---|
21 | public ImmutableGpxTrackSegment(Collection<WayPoint> wayPoints) {
|
---|
22 | this.wayPoints = Collections.unmodifiableList(new ArrayList<>(wayPoints));
|
---|
23 | this.bounds = calculateBounds();
|
---|
24 | this.length = calculateLength();
|
---|
25 | }
|
---|
26 |
|
---|
27 | private Bounds calculateBounds() {
|
---|
28 | Bounds result = null;
|
---|
29 | for (WayPoint wpt: wayPoints) {
|
---|
30 | if (result == null) {
|
---|
31 | result = new Bounds(wpt.getCoor());
|
---|
32 | } else {
|
---|
33 | result.extend(wpt.getCoor());
|
---|
34 | }
|
---|
35 | }
|
---|
36 | return result;
|
---|
37 | }
|
---|
38 |
|
---|
39 | private double calculateLength() {
|
---|
40 | double result = 0.0; // in meters
|
---|
41 | WayPoint last = null;
|
---|
42 | for (WayPoint tpt : wayPoints) {
|
---|
43 | if (last != null) {
|
---|
44 | Double d = last.getCoor().greatCircleDistance(tpt.getCoor());
|
---|
45 | if (!d.isNaN() && !d.isInfinite()) {
|
---|
46 | result += d;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | last = tpt;
|
---|
50 | }
|
---|
51 | return result;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public Bounds getBounds() {
|
---|
56 | return bounds == null ? null : new Bounds(bounds);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public Collection<WayPoint> getWayPoints() {
|
---|
61 | return wayPoints;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public double length() {
|
---|
66 | return length;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public int getUpdateCount() {
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public int hashCode() {
|
---|
76 | return 31 + ((wayPoints == null) ? 0 : wayPoints.hashCode());
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public boolean equals(Object obj) {
|
---|
81 | if (this == obj)
|
---|
82 | return true;
|
---|
83 | if (obj == null)
|
---|
84 | return false;
|
---|
85 | if (getClass() != obj.getClass())
|
---|
86 | return false;
|
---|
87 | ImmutableGpxTrackSegment other = (ImmutableGpxTrackSegment) obj;
|
---|
88 | if (wayPoints == null) {
|
---|
89 | if (other.wayPoints != null)
|
---|
90 | return false;
|
---|
91 | } else if (!wayPoints.equals(other.wayPoints))
|
---|
92 | return false;
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|