<commit_before>/*
 * Copyright 2014 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplannerdelirium.sss.domain.location;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("SphereLocation")
public class SphereLocation extends Location {

    private static final double RADIAN_CONVERSION = Math.PI / 180.0;
    private static final int EARTH_R_IN_KM = 6371;
    private static final int TWICE_EARTH_R_IN_KM = 2 * EARTH_R_IN_KM;

    private double cartesianX;
    private double cartesianY;
    private double cartesianZ;

    public SphereLocation() {
        super();
    }

    public SphereLocation(long id, double latitude, double longitude) {
        super(id, latitude, longitude);
        updateCache();
    }

    public void updateCache() {
        final double latitudeInRads  = RADIAN_CONVERSION * latitude;
        final double longitudeInRads = RADIAN_CONVERSION * longitude;
        // Cartesian coordinates, normalized for a sphere of diameter 1.0
        this.cartesianX = 0.5 * Math.cos(latitudeInRads) * Math.sin(longitudeInRads);
        this.cartesianY = 0.5 * Math.cos(latitudeInRads) * Math.cos(longitudeInRads);
        this.cartesianZ = 0.5 * Math.sin(latitudeInRads);
    }

    @Override
    public double getDistanceTo(final Location o) {
        if (o == this) {
            return 0.0;
        }
        SphereLocation other = (SphereLocation) o;
        double dX = cartesianX - other.cartesianX;
        double dY = cartesianY - other.cartesianY;
        double dZ = cartesianZ - other.cartesianZ;
        double r = Math.sqrt(dX * dX + dY * dY + dZ * dZ);
        return TWICE_EARTH_R_IN_KM * Math.asin(r);
    }

    @Override
    public double getEuclideanDistanceSquaredTo(Location o) {
        if (o == this) {
            return 0.0;
        }
        SphereLocation other = (SphereLocation) o;
        double dX = cartesianX - other.cartesianX;
        double dY = cartesianY - other.cartesianY;
        double dZ = cartesianZ - other.cartesianZ;
        return dX * dX + dY * dY + dZ * dZ;
    }

}
<commit_msg>Reuse code from the recently added method<commit_after>/*
 * Copyright 2014 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplannerdelirium.sss.domain.location;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("SphereLocation")
public class SphereLocation extends Location {

    private static final double RADIAN_CONVERSION = Math.PI / 180.0;
    private static final int EARTH_R_IN_KM = 6371;
    private static final int TWICE_EARTH_R_IN_KM = 2 * EARTH_R_IN_KM;

    private double cartesianX;
    private double cartesianY;
    private double cartesianZ;

    public SphereLocation() {
        super();
    }

    public SphereLocation(long id, double latitude, double longitude) {
        super(id, latitude, longitude);
        updateCache();
    }

    public void updateCache() {
        final double latitudeInRads  = RADIAN_CONVERSION * latitude;
        final double longitudeInRads = RADIAN_CONVERSION * longitude;
        // Cartesian coordinates, normalized for a sphere of diameter 1.0
        this.cartesianX = 0.5 * Math.cos(latitudeInRads) * Math.sin(longitudeInRads);
        this.cartesianY = 0.5 * Math.cos(latitudeInRads) * Math.cos(longitudeInRads);
        this.cartesianZ = 0.5 * Math.sin(latitudeInRads);
    }

    @Override
    public double getDistanceTo(final Location o) {
        if (o == this) {
            return 0.0;
        }
        double r = Math.sqrt(getEuclideanDistanceSquaredTo(o));
        return TWICE_EARTH_R_IN_KM * Math.asin(r);
    }

    @Override
    public double getEuclideanDistanceSquaredTo(Location o) {
        if (o == this) {
            return 0.0;
        }
        SphereLocation other = (SphereLocation) o;
        double dX = cartesianX - other.cartesianX;
        double dY = cartesianY - other.cartesianY;
        double dZ = cartesianZ - other.cartesianZ;
        return dX * dX + dY * dY + dZ * dZ;
    }

}

{'fraction_non_alphanumeric': 0.08318514127642758, 'fraction_numerical': 0.011460185734044654, 'mean_word_length': 2.846504559270517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '985294', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1499, 'n_words': 465}