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 |
|
---|
13 | /**
|
---|
14 | * Immutable GPX track.
|
---|
15 | * @since 2907
|
---|
16 | */
|
---|
17 | public class ImmutableGpxTrack extends WithAttributes implements GpxTrack {
|
---|
18 |
|
---|
19 | private final List<GpxTrackSegment> segments;
|
---|
20 | private final double length;
|
---|
21 | private final Bounds bounds;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructs a new {@code ImmutableGpxTrack}.
|
---|
25 | * @param trackSegs track segments
|
---|
26 | * @param attributes track attributes
|
---|
27 | */
|
---|
28 | public ImmutableGpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) {
|
---|
29 | List<GpxTrackSegment> newSegments = new ArrayList<>();
|
---|
30 | for (Collection<WayPoint> trackSeg: trackSegs) {
|
---|
31 | if (trackSeg != null && !trackSeg.isEmpty()) {
|
---|
32 | newSegments.add(new ImmutableGpxTrackSegment(trackSeg));
|
---|
33 | }
|
---|
34 | }
|
---|
35 | this.attr = Collections.unmodifiableMap(new HashMap<>(attributes));
|
---|
36 | this.segments = Collections.unmodifiableList(newSegments);
|
---|
37 | this.length = calculateLength();
|
---|
38 | this.bounds = calculateBounds();
|
---|
39 | }
|
---|
40 |
|
---|
41 | private double calculateLength() {
|
---|
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 |
|
---|
65 | @Override
|
---|
66 | public Map<String, Object> getAttributes() {
|
---|
67 | return attr;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Bounds getBounds() {
|
---|
72 | return bounds == null ? null : new Bounds(bounds);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public double length() {
|
---|
77 | return length;
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public Collection<GpxTrackSegment> getSegments() {
|
---|
82 | return segments;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public int hashCode() {
|
---|
87 | return 31 * super.hashCode() + ((segments == null) ? 0 : segments.hashCode());
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public boolean equals(Object obj) {
|
---|
92 | if (this == obj)
|
---|
93 | return true;
|
---|
94 | if (!super.equals(obj))
|
---|
95 | return false;
|
---|
96 | if (getClass() != obj.getClass())
|
---|
97 | return false;
|
---|
98 | ImmutableGpxTrack other = (ImmutableGpxTrack) obj;
|
---|
99 | if (segments == null) {
|
---|
100 | if (other.segments != null)
|
---|
101 | return false;
|
---|
102 | } else if (!segments.equals(other.segments))
|
---|
103 | return false;
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | }
|
---|