Changeset 13136 in josm


Ignore:
Timestamp:
2017-11-20T01:25:31+01:00 (4 weeks ago)
Author:
Don-vip
Message:

fix #15573 - add synchronization to GpxData

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java

    r12725 r13136  
    107107     * @param other existing GPX data
    108108     */
    109     public void mergeFrom(GpxData other) {
     109    public synchronized void mergeFrom(GpxData other) {
    110110        if (storageFile == null && other.storageFile != null) {
    111111            storageFile = other.storageFile;
     
    125125            }
    126126        }
    127         other.getTracks().forEach(this::addTrack);
    128         other.getRoutes().forEach(this::addRoute);
    129         other.getWaypoints().forEach(this::addWaypoint);
     127        other.privateTracks.forEach(this::addTrack);
     128        other.privateRoutes.forEach(this::addRoute);
     129        other.privateWaypoints.forEach(this::addWaypoint);
    130130        dataSources.addAll(other.dataSources);
    131131        fireInvalidate();
     
    136136     * @return The tracks.
    137137     */
    138     public Collection<GpxTrack> getTracks() {
     138    public synchronized Collection<GpxTrack> getTracks() {
    139139        return Collections.unmodifiableCollection(privateTracks);
    140140    }
     
    145145     * @since 12156
    146146     */
    147     public void addTrack(GpxTrack track) {
     147    public synchronized void addTrack(GpxTrack track) {
    148148        if (privateTracks.stream().anyMatch(t -> t == track)) {
    149149            throw new IllegalArgumentException(MessageFormat.format("The track was already added to this data: {0}", track));
     
    159159     * @since 12156
    160160     */
    161     public void removeTrack(GpxTrack track) {
     161    public synchronized void removeTrack(GpxTrack track) {
    162162        if (!privateTracks.removeIf(t -> t == track)) {
    163163            throw new IllegalArgumentException(MessageFormat.format("The track was not in this data: {0}", track));
     
    172172     * @since 12156
    173173     */
    174     public Collection<GpxRoute> getRoutes() {
     174    public synchronized Collection<GpxRoute> getRoutes() {
    175175        return Collections.unmodifiableCollection(privateRoutes);
    176176    }
     
    181181     * @since 12156
    182182     */
    183     public void addRoute(GpxRoute route) {
     183    public synchronized void addRoute(GpxRoute route) {
    184184        if (privateRoutes.stream().anyMatch(r -> r == route)) {
    185185            throw new IllegalArgumentException(MessageFormat.format("The route was already added to this data: {0}", route));
     
    194194     * @since 12156
    195195     */
    196     public void removeRoute(GpxRoute route) {
     196    public synchronized void removeRoute(GpxRoute route) {
    197197        if (!privateRoutes.removeIf(r -> r == route)) {
    198198            throw new IllegalArgumentException(MessageFormat.format("The route was not in this data: {0}", route));
     
    206206     * @since 12156
    207207     */
    208     public Collection<WayPoint> getWaypoints() {
     208    public synchronized Collection<WayPoint> getWaypoints() {
    209209        return Collections.unmodifiableCollection(privateWaypoints);
    210210    }
     
    215215     * @since 12156
    216216     */
    217     public void addWaypoint(WayPoint waypoint) {
     217    public synchronized void addWaypoint(WayPoint waypoint) {
    218218        if (privateWaypoints.stream().anyMatch(w -> w == waypoint)) {
    219219            throw new IllegalArgumentException(MessageFormat.format("The route was already added to this data: {0}", waypoint));
     
    228228     * @since 12156
    229229     */
    230     public void removeWaypoint(WayPoint waypoint) {
     230    public synchronized void removeWaypoint(WayPoint waypoint) {
    231231        if (!privateWaypoints.removeIf(w -> w == waypoint)) {
    232232            throw new IllegalArgumentException(MessageFormat.format("The route was not in this data: {0}", waypoint));
     
    239239     * @return {@code true} if this GPX data has track points, {@code false} otherwise
    240240     */
    241     public boolean hasTrackPoints() {
     241    public synchronized boolean hasTrackPoints() {
    242242        return getTrackPoints().findAny().isPresent();
    243243    }
     
    251251     * @since 12156
    252252     */
    253     public Stream<WayPoint> getTrackPoints() {
     253    public synchronized Stream<WayPoint> getTrackPoints() {
    254254        return getTracks().stream().flatMap(trk -> trk.getSegments().stream()).flatMap(trkseg -> trkseg.getWayPoints().stream());
    255255    }
     
    259259     * @return {@code true} if this GPX data has route points, {@code false} otherwise
    260260     */
    261     public boolean hasRoutePoints() {
    262         return getRoutes().stream().anyMatch(rte -> !rte.routePoints.isEmpty());
     261    public synchronized boolean hasRoutePoints() {
     262        return privateRoutes.stream().anyMatch(rte -> !rte.routePoints.isEmpty());
    263263    }
    264264
     
    267267     * @return {@code true} if this GPX data is empty, {@code false} otherwise
    268268     */
    269     public boolean isEmpty() {
     269    public synchronized boolean isEmpty() {
    270270        return !hasRoutePoints() && !hasTrackPoints() && waypoints.isEmpty();
    271271    }
     
    301301     * @see #dataSources
    302302     */
    303     public Bounds recalculateBounds() {
     303    public synchronized Bounds recalculateBounds() {
    304304        Bounds bounds = null;
    305         for (WayPoint wpt : getWaypoints()) {
     305        for (WayPoint wpt : privateWaypoints) {
    306306            if (bounds == null) {
    307307                bounds = new Bounds(wpt.getCoor());
     
    310310            }
    311311        }
    312         for (GpxRoute rte : getRoutes()) {
     312        for (GpxRoute rte : privateRoutes) {
    313313            for (WayPoint wpt : rte.routePoints) {
    314314                if (bounds == null) {
     
    319319            }
    320320        }
    321         for (GpxTrack trk : getTracks()) {
     321        for (GpxTrack trk : privateTracks) {
    322322            Bounds trkBounds = trk.getBounds();
    323323            if (trkBounds != null) {
     
    336336     * @return the length in meters
    337337     */
    338     public double length() {
    339         return getTracks().stream().mapToDouble(GpxTrack::length).sum();
     338    public synchronized double length() {
     339        return privateTracks.stream().mapToDouble(GpxTrack::length).sum();
    340340    }
    341341
     
    362362     * @return minimum and maximum dates in array of 2 elements
    363363    */
    364     public Date[] getMinMaxTimeForAllTracks() {
     364    public synchronized Date[] getMinMaxTimeForAllTracks() {
    365365        double now = System.currentTimeMillis() / 1000.0;
    366366        final DoubleSummaryStatistics statistics = tracks.stream()
     
    384384     * end of a segment, or may be null if nothing close enough
    385385     */
    386     public WayPoint nearestPointOnTrack(EastNorth p, double tolerance) {
     386    public synchronized WayPoint nearestPointOnTrack(EastNorth p, double tolerance) {
    387387        /*
    388388         * assume the coordinates of P are xp,yp, and those of a section of track between two
     
    414414        double py = p.north();
    415415        double rx = 0.0, ry = 0.0, sx, sy, x, y;
    416         for (GpxTrack track : getTracks()) {
     416        for (GpxTrack track : privateTracks) {
    417417            for (GpxTrackSegment seg : track.getSegments()) {
    418418                WayPoint r = null;
     
    502502     * Resets the internal caches of east/north coordinates.
    503503     */
    504     public void resetEastNorthCache() {
    505         getWaypoints().forEach(WayPoint::invalidateEastNorthCache);
     504    public synchronized void resetEastNorthCache() {
     505        privateWaypoints.forEach(WayPoint::invalidateEastNorthCache);
    506506        getTrackPoints().forEach(WayPoint::invalidateEastNorthCache);
    507507        for (GpxRoute route: getRoutes()) {
     
    594594
    595595    @Override
    596     public int hashCode() {
     596    public synchronized int hashCode() {
    597597        final int prime = 31;
    598598        int result = 1;
     
    605605
    606606    @Override
    607     public boolean equals(Object obj) {
     607    public synchronized boolean equals(Object obj) {
    608608        if (this == obj)
    609609            return true;
Note: See TracChangeset for help on using the changeset viewer.