<commit_before>package cgeo.geocaching.maps.routing;

import cgeo.geocaching.R;
import cgeo.geocaching.enumerations.CoordinatesType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.models.Geocache;
import cgeo.geocaching.models.Waypoint;
import cgeo.geocaching.storage.DataStore;
import cgeo.geocaching.utils.AndroidRxUtils;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.Toast;

import java.util.ArrayList;

import io.reactivex.rxjava3.schedulers.Schedulers;

public class Route implements Parcelable {

    private enum ToggleItemState {
        ADDED,
        REMOVED,
        ERROR_NO_POINT
    }

    private ArrayList<RouteSegment> segments = null;
    private boolean loadingRoute = false;

    public interface RouteUpdater {
        void updateRoute(ArrayList<Geopoint> route, float distance);
    }

    private static class RouteSegment implements Parcelable {
        private RouteItem item = null;
        private Geopoint point = null;
        private ArrayList<Geopoint> points = null;
        private float distance = 0.0f;

        RouteSegment(final RouteItem item) {
            this.item = item;

            point = null;
            if (item.getType() == CoordinatesType.CACHE) {
                final Geocache cache = DataStore.loadCache(item.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB);
                if (cache != null) {
                    point = cache.getCoords();
                }
            } else if (item.getType() == CoordinatesType.WAYPOINT && item.getId() >= 0) {
                final Waypoint waypoint = DataStore.loadWaypoint(item.getId());
                if (waypoint != null) {
                    point = waypoint.getCoords();
                }
            }

            points = null;
            distance = 0.0f;
        }

        public boolean hasPoint() {
            return point != null;
        }

        // Parcelable methods

        public static final Parcelable.Creator<RouteSegment> CREATOR = new Parcelable.Creator<RouteSegment>() {

            @Override
            public RouteSegment createFromParcel(final Parcel source) {
                return new RouteSegment(source);
            }

            @Override
            public RouteSegment[] newArray(final int size) {
                return new RouteSegment[size];
            }

        };

        private RouteSegment (final Parcel parcel) {
            item = parcel.readParcelable(RouteItem.class.getClassLoader());
            point = parcel.readParcelable(Geopoint.class.getClassLoader());
            points = parcel.readArrayList(Geopoint.class.getClassLoader());
            distance = parcel.readFloat();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(final Parcel dest, final int flags) {
            dest.writeParcelable(item, flags);
            dest.writeParcelable(point, flags);
            dest.writeList(points);
            dest.writeFloat(distance);
        }

    }

    public Route() {
    }

    public void toggleItem(final Context context, final RouteItem item, final RouteUpdater routeUpdater) {
        if (loadingRoute) {
            return;
        }

        if (item.getType() == CoordinatesType.WAYPOINT && item.getId() == -1) {
            Toast.makeText(context, R.string.individual_route_error_single_waypoint_mode, Toast.LENGTH_SHORT).show();
            return;
        }

        switch (toggleItemInternal(item)) {
            case ADDED:
                Toast.makeText(context, R.string.individual_route_added, Toast.LENGTH_SHORT).show();
                break;
            case REMOVED:
                Toast.makeText(context, R.string.individual_route_removed, Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(context, R.string.individual_route_error_toggling_waypoint, Toast.LENGTH_SHORT).show();
                break;
        }
        updateRoute(routeUpdater);
        saveRoute();
    }

    public void updateRoute(final RouteUpdater routeUpdater) {
        if (loadingRoute) {
            return;
        }

        final ArrayList<Geopoint> route = new ArrayList<>();
        float distance = 0.0f;
        if (segments != null) {
            for (int i = 0; i < segments.size(); i++) {
                final RouteSegment segment = segments.get(i);
                if (segment.points != null) {
                    for (int j = 0; j < segment.points.size(); j++) {
                        route.add(segment.points.get(j));
                    }
                }
                distance += segment.distance;
            }
        }
        routeUpdater.updateRoute(route, distance);
    }

    public void clearRoute(final RouteUpdater routeUpdater) {
        clearRouteInternal(routeUpdater, true);
    }
<commit_msg>fix crash on them options change (fix #8349) (#8353)<commit_after>package cgeo.geocaching.maps.routing;

import cgeo.geocaching.R;
import cgeo.geocaching.enumerations.CoordinatesType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.models.Geocache;
import cgeo.geocaching.models.Waypoint;
import cgeo.geocaching.storage.DataStore;
import cgeo.geocaching.utils.AndroidRxUtils;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.Toast;

import java.util.ArrayList;

import io.reactivex.rxjava3.schedulers.Schedulers;

public class Route implements Parcelable {

    private enum ToggleItemState {
        ADDED,
        REMOVED,
        ERROR_NO_POINT
    }

    private ArrayList<RouteSegment> segments = null;
    private boolean loadingRoute = false;

    public interface RouteUpdater {
        void updateRoute(ArrayList<Geopoint> route, float distance);
    }

    private static class RouteSegment implements Parcelable {
        private RouteItem item = null;
        private Geopoint point = null;
        private ArrayList<Geopoint> points = null;
        private float distance = 0.0f;

        RouteSegment(final RouteItem item) {
            this.item = item;

            point = null;
            if (item.getType() == CoordinatesType.CACHE) {
                final Geocache cache = DataStore.loadCache(item.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB);
                if (cache != null) {
                    point = cache.getCoords();
                }
            } else if (item.getType() == CoordinatesType.WAYPOINT && item.getId() >= 0) {
                final Waypoint waypoint = DataStore.loadWaypoint(item.getId());
                if (waypoint != null) {
                    point = waypoint.getCoords();
                }
            }

            points = null;
            distance = 0.0f;
        }

        public boolean hasPoint() {
            return point != null;
        }

        // Parcelable methods

        public static final Parcelable.Creator<RouteSegment> CREATOR = new Parcelable.Creator<RouteSegment>() {

            @Override
            public RouteSegment createFromParcel(final Parcel source) {
                return new RouteSegment(source);
            }

            @Override
            public RouteSegment[] newArray(final int size) {
                return new RouteSegment[size];
            }

        };

        private RouteSegment (final Parcel parcel) {
            item = parcel.readParcelable(RouteItem.class.getClassLoader());
            point = parcel.readParcelable(Geopoint.class.getClassLoader());
            points = parcel.readArrayList(Geopoint.class.getClassLoader());
            distance = parcel.readFloat();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(final Parcel dest, final int flags) {
            dest.writeParcelable(item, flags);
            dest.writeParcelable(point, flags);
            dest.writeList(points);
            dest.writeFloat(distance);
        }

    }

    public Route() {
    }

    public void toggleItem(final Context context, final RouteItem item, final RouteUpdater routeUpdater) {
        if (loadingRoute) {
            return;
        }

        if (item.getType() == CoordinatesType.WAYPOINT && item.getId() == -1) {
            Toast.makeText(context, R.string.individual_route_error_single_waypoint_mode, Toast.LENGTH_SHORT).show();
            return;
        }

        switch (toggleItemInternal(item)) {
            case ADDED:
                Toast.makeText(context, R.string.individual_route_added, Toast.LENGTH_SHORT).show();
                break;
            case REMOVED:
                Toast.makeText(context, R.string.individual_route_removed, Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(context, R.string.individual_route_error_toggling_waypoint, Toast.LENGTH_SHORT).show();
                break;
        }
        updateRoute(routeUpdater);
        saveRoute();
    }

    public void updateRoute(final RouteUpdater routeUpdater) {
        if (loadingRoute) {
            return;
        }

        final ArrayList<Geopoint> route = new ArrayList<>();
        float distance = 0.0f;
        if (segments != null) {
            for (int i = 0; i < segments.size(); i++) {
                final RouteSegment segment = segments.get(i);
                if (segment.points != null) {
                    for (int j = 0; j < segment.points.size(); j++) {
                        route.add(segment.points.get(j));
                    }
                }
                distance += segment.distance;
            }
        }
        if (null != routeUpdater) {
            routeUpdater.updateRoute(route, distance);
        }
    }

    public void clearRoute(final RouteUpdater routeUpdater) {
        clearRouteInternal(routeUpdater, true);
    }

{'fraction_non_alphanumeric': 0.09393969549208876, 'fraction_numerical': 0.003184396457358941, 'mean_word_length': 1.9830810329474622, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2395216', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2681, 'n_words': 577}