| 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 | public class ImmutableGpxTrack implements GpxTrack { |
|---|
| 14 | |
|---|
| 15 | private final Map<String, Object> attributes; |
|---|
| 16 | private final Collection<GpxTrackSegment> segments; |
|---|
| 17 | private final double length; |
|---|
| 18 | private final Bounds bounds; |
|---|
| 19 | |
|---|
| 20 | public ImmutableGpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) { |
|---|
| 21 | List<GpxTrackSegment> newSegments = new ArrayList<GpxTrackSegment>(); |
|---|
| 22 | for (Collection<WayPoint> trackSeg: trackSegs) { |
|---|
| 23 | if (trackSeg != null && !trackSeg.isEmpty()) { |
|---|
| 24 | newSegments.add(new ImmutableGpxTrackSegment(trackSeg)); |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | this.attributes = Collections.unmodifiableMap(new HashMap<String, Object>(attributes)); |
|---|
| 28 | this.segments = Collections.unmodifiableCollection(newSegments); |
|---|
| 29 | this.length = calculateLength(); |
|---|
| 30 | this.bounds = calculateBounds(); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | private double calculateLength(){ |
|---|
| 34 | double result = 0.0; // in meters |
|---|
| 35 | |
|---|
| 36 | for (GpxTrackSegment trkseg : segments) { |
|---|
| 37 | result += trkseg.length(); |
|---|
| 38 | } |
|---|
| 39 | return result; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | private Bounds calculateBounds() { |
|---|
| 43 | Bounds result = null; |
|---|
| 44 | for (GpxTrackSegment segment: segments) { |
|---|
| 45 | Bounds segBounds = segment.getBounds(); |
|---|
| 46 | if (segBounds != null) { |
|---|
| 47 | if (result == null) { |
|---|
| 48 | result = new Bounds(segBounds); |
|---|
| 49 | } else { |
|---|
| 50 | result.extend(segBounds); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | return result; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public Map<String, Object> getAttributes() { |
|---|
| 58 | return attributes; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | public Bounds getBounds() { |
|---|
| 62 | if (bounds == null) |
|---|
| 63 | return null; |
|---|
| 64 | else |
|---|
| 65 | return new Bounds(bounds); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public double length() { |
|---|
| 69 | return length; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public Collection<GpxTrackSegment> getSegments() { |
|---|
| 73 | return segments; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | public int getUpdateCount() { |
|---|
| 77 | return 0; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|