All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBusx.geo.GeoCircle Maven / Gradle / Ivy

The newest version!
//
// Copyright 2024 Charles W. Rapp
//
// 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 net.sf.eBusx.geo;

import com.google.common.base.Strings;
import java.math.BigDecimal;
import java.util.Objects;
import net.sf.eBus.util.Validator;

/**
 * Defines a GeoJSON circle by the center point, radius, and
 * radius units.
 *
 * @author Charles W. Rapp
 */

public final class GeoCircle
    extends GeoObject
{
//---------------------------------------------------------------
// Member data.
//

    //-----------------------------------------------------------
    // Constants.
    //

    /**
     * Default units defining circle radius is kilometers
     * ({@value}).
     */
    public static final String DEFAULT_RADIUS_UNITS = "km";

    /**
     * Serialization version identifier.
     */
    private static final long serialVersionUID = 0x070500L;

    //-----------------------------------------------------------
    // Locals.
    //

    /**
     * Circle center coordinate.
     */
    public final Position center;

    /**
     * Circle radius.
     */
    public final BigDecimal radius;

    /**
     * Radius is defined in these units. Default to
     * {@link #DEFAULT_RADIUS_UNITS}.
     */
    public final String radiusUnits;

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    /**
     * Creates a new GeoJSON circle instance based on builder's
     * settings.
     * @param builder contains circle settings.
     */
    private GeoCircle(final Builder builder)
    {
        super (builder);

        this.center = builder.mCenter;
        this.radius = builder.mRadius;
        this.radiusUnits = builder.mRadiusUnits;
    } // end of GeoCircle(Builder)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    //-----------------------------------------------------------
    // Object Method Overrides.
    //

    /**
     * Returns {@code true} if {@code o} is the same instance as
     * {@code this GeoCircle} or if {@code o} is a
     * non-{@code null GeoCircle} instance whose center, radius,
     * and radius units equals {@code this GeoCircle}'s values.
     * @param o comparison object.
     * @return {@code true} if {@code o} equals
     * {@code this GeoCircle} instance.
     */
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (!retcode && o instanceof GeoCircle)
        {
            final GeoCircle gc = (GeoCircle) o;

            retcode = (center.equals(gc.center) &&
                       radius.equals(gc.radius) &&
                       radiusUnits.equals(gc.radiusUnits));
        }

        return (retcode);
    } // end of equals(Object)

    /**
     * Returns hash code of center, radius, and radius units.
     * @return GeoJSON circle hash code.
     */
    @Override
    public int hashCode()
    {
        return (Objects.hash(center, radius, radiusUnits));
    } // end of hashCode()

    @Override
    public String toString()
    {
        final StringBuilder retval = new StringBuilder();

        return (retval.append('[')
                      .append(super.toString())
                      .append(", center=").append(center)
                      .append(", radius=").append(radius)
                      .append(' ').append(radiusUnits)
                      .append(']')
                      .toString());
    } // end of toString()

    //
    // end of Object Method Overrides.
    //-----------------------------------------------------------

    /**
     * Returns a new instance of a {@code GeoCircle} builder.
     * @return GeoJSON circle builder.
     *
     * @see Builder
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

//---------------------------------------------------------------
// Inner classes.
//

    /**
     * Builder class used to create GeoJSON circle instance. A
     * {@code Builder} instance is obtained by calling
     * {@link #builder()} method.
     *
     * @see #builder()
     */
    public static final class Builder
        extends GeoObject.GeoBuilder
    {
    //-----------------------------------------------------------
    // Member data.
    //

        //-------------------------------------------------------
        // Locals.
        //

        private Position mCenter;
        private BigDecimal mRadius;
        private String mRadiusUnits;

    //-----------------------------------------------------------
    // Member methods.
    //

        //-------------------------------------------------------
        // Constructors.
        //

        private Builder()
        {
            super (GeoCircle.class, GeoType.CIRCLE);

            mRadiusUnits = DEFAULT_RADIUS_UNITS;
        } // end of Builder()

        //
        // end of Constructors.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Builder Method Overrides.
        //

        @Override
        protected Builder self()
        {
            return (this);
        } // end of self()

        @Override
        protected GeoCircle buildImpl()
        {
            return (new GeoCircle(this));
        } // end of buildImpl()

        @Override
        protected Validator validate(Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mCenter, "center")
                         .requireNotNull(mRadius, "radius"));
        } // end of validate(Validator)

        //
        // end of Builder Method Overrides.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Set Methods.
        //

        /**
         * Sets GeoJSON circle's center coordinate.
         * @param center circle's center.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code center} is {@code null}.
         */
        public Builder center(final Position center)
        {
            mCenter =
                Objects.requireNonNull(center, "center is null");

            return (this);
        } // end of center(Position)

        /**
         * Sets GeoJSON circle's radius. Must be > zero.
         * @param radius circle's radius.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code radius} is {@code null}.
         * @throws IllegalArgumentException
         * if {@code radius} ≤ zero.
         */
        public Builder radius(final BigDecimal radius)
        {
            Objects.requireNonNull(radius, "radius is null");

            if (radius.compareTo(BigDecimal.ZERO) <= 0)
            {
                throw (
                    new IllegalArgumentException(
                        "radious <= zero"));
            }

            mRadius = radius;

            return (this);
        } // end of radius(BigDecimal)

        /**
         * Sets GeoJSON circle's radius units. Please note that
         * {@code units} may be set to any
         * non-{@code null}, non-empty text value.
         * 

* Defaults to {@link #DEFAULT_RADIUS_UNITS} if not set. *

* @param units radius units. * @return {@code this Builder} instance. * @throws IllegalArgumentException * if {@code units} is either {@code null} or an empty * string. */ public Builder radiusUnits(final String units) { if (Strings.isNullOrEmpty(units)) { throw ( new IllegalArgumentException( "units is either null or an empty string")); } mRadiusUnits = units; return (this); } // end of radiusUnits(String) // // end of Set Methods. //------------------------------------------------------- } // end of class Builder } // end of class GeoCircle




© 2015 - 2025 Weber Informatics LLC | Privacy Policy