[2907] | 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.HashMap;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 |
|
---|
| 11 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 12 |
|
---|
[9949] | 13 | /**
|
---|
| 14 | * Immutable GPX track.
|
---|
| 15 | * @since 2907
|
---|
| 16 | */
|
---|
[5681] | 17 | public class ImmutableGpxTrack extends WithAttributes implements GpxTrack {
|
---|
[2907] | 18 |
|
---|
[10906] | 19 | private final List<GpxTrackSegment> segments;
|
---|
[2907] | 20 | private final double length;
|
---|
| 21 | private final Bounds bounds;
|
---|
| 22 |
|
---|
[9949] | 23 | /**
|
---|
| 24 | * Constructs a new {@code ImmutableGpxTrack}.
|
---|
| 25 | * @param trackSegs track segments
|
---|
| 26 | * @param attributes track attributes
|
---|
| 27 | */
|
---|
[2907] | 28 | public ImmutableGpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) {
|
---|
[7005] | 29 | List<GpxTrackSegment> newSegments = new ArrayList<>();
|
---|
[2907] | 30 | for (Collection<WayPoint> trackSeg: trackSegs) {
|
---|
| 31 | if (trackSeg != null && !trackSeg.isEmpty()) {
|
---|
| 32 | newSegments.add(new ImmutableGpxTrackSegment(trackSeg));
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
[7005] | 35 | this.attr = Collections.unmodifiableMap(new HashMap<>(attributes));
|
---|
[10906] | 36 | this.segments = Collections.unmodifiableList(newSegments);
|
---|
[2907] | 37 | this.length = calculateLength();
|
---|
| 38 | this.bounds = calculateBounds();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[8510] | 41 | private double calculateLength() {
|
---|
[2907] | 42 | double result = 0.0; // in meters
|
---|
| 43 |
|
---|
| 44 | for (GpxTrackSegment trkseg : segments) {
|
---|
| 45 | result += trkseg.length();
|
---|
| 46 | }
|
---|
| 47 | return result;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | private Bounds calculateBounds() {
|
---|
| 51 | Bounds result = null;
|
---|
| 52 | for (GpxTrackSegment segment: segments) {
|
---|
| 53 | Bounds segBounds = segment.getBounds();
|
---|
| 54 | if (segBounds != null) {
|
---|
| 55 | if (result == null) {
|
---|
| 56 | result = new Bounds(segBounds);
|
---|
| 57 | } else {
|
---|
| 58 | result.extend(segBounds);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | return result;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[5681] | 65 | @Override
|
---|
[2907] | 66 | public Map<String, Object> getAttributes() {
|
---|
[5681] | 67 | return attr;
|
---|
[2907] | 68 | }
|
---|
| 69 |
|
---|
[5681] | 70 | @Override
|
---|
[2907] | 71 | public Bounds getBounds() {
|
---|
[11553] | 72 | return bounds == null ? null : new Bounds(bounds);
|
---|
[2907] | 73 | }
|
---|
| 74 |
|
---|
[5681] | 75 | @Override
|
---|
[2907] | 76 | public double length() {
|
---|
| 77 | return length;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5681] | 80 | @Override
|
---|
[2907] | 81 | public Collection<GpxTrackSegment> getSegments() {
|
---|
| 82 | return segments;
|
---|
| 83 | }
|
---|
[3119] | 84 |
|
---|
[5681] | 85 | @Override
|
---|
[3119] | 86 | public int getUpdateCount() {
|
---|
| 87 | return 0;
|
---|
| 88 | }
|
---|
[10906] | 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public int hashCode() {
|
---|
| 92 | return 31 * super.hashCode() + ((segments == null) ? 0 : segments.hashCode());
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Override
|
---|
| 96 | public boolean equals(Object obj) {
|
---|
| 97 | if (this == obj)
|
---|
| 98 | return true;
|
---|
| 99 | if (!super.equals(obj))
|
---|
| 100 | return false;
|
---|
| 101 | if (getClass() != obj.getClass())
|
---|
| 102 | return false;
|
---|
| 103 | ImmutableGpxTrack other = (ImmutableGpxTrack) obj;
|
---|
| 104 | if (segments == null) {
|
---|
| 105 | if (other.segments != null)
|
---|
| 106 | return false;
|
---|
| 107 | } else if (!segments.equals(other.segments))
|
---|
| 108 | return false;
|
---|
| 109 | return true;
|
---|
| 110 | }
|
---|
[2907] | 111 | }
|
---|