source: josm/trunk/src/org/openstreetmap/josm/data/gpx/ImmutableGpxTrackSegment.java@ 13138

Last change on this file since 13138 was 12186, checked in by michael2402, 7 years ago

See #14794: Add javadoc for all gpx classes.

  • Property svn:eol-style set to native
File size: 2.6 KB
RevLine 
[2907]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.gpx;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
[10906]7import java.util.List;
[2907]8
9import org.openstreetmap.josm.data.Bounds;
10
[12186]11/**
12 * A gpx track segment consisting of multiple waypoints, that cannot be changed.
13 */
[2907]14public class ImmutableGpxTrackSegment implements GpxTrackSegment {
15
[10906]16 private final List<WayPoint> wayPoints;
[2907]17 private final Bounds bounds;
18 private final double length;
19
[10906]20 /**
21 * Constructs a new {@code ImmutableGpxTrackSegment}.
22 * @param wayPoints list of waypoints
23 */
[2907]24 public ImmutableGpxTrackSegment(Collection<WayPoint> wayPoints) {
[10906]25 this.wayPoints = Collections.unmodifiableList(new ArrayList<>(wayPoints));
[2907]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) {
[8510]46 if (last != null) {
[2907]47 Double d = last.getCoor().greatCircleDistance(tpt.getCoor());
[8510]48 if (!d.isNaN() && !d.isInfinite()) {
[2907]49 result += d;
50 }
51 }
52 last = tpt;
53 }
54 return result;
55 }
56
[6084]57 @Override
[2907]58 public Bounds getBounds() {
[11553]59 return bounds == null ? null : new Bounds(bounds);
[2907]60 }
61
[6084]62 @Override
[2907]63 public Collection<WayPoint> getWayPoints() {
[12186]64 return Collections.unmodifiableList(wayPoints);
[2907]65 }
66
[6084]67 @Override
[2907]68 public double length() {
69 return length;
70 }
71
[6084]72 @Override
[3119]73 public int getUpdateCount() {
74 return 0;
75 }
76
[10906]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 }
[2907]98}
Note: See TracBrowser for help on using the repository browser.