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

net.sf.eBusx.geo.GeoEllipse 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 ellipse by the center point, major and
 * minor axes, and degrees of rotation with axis units and
 * rotation units.
 *
 * @author Charles W. Rapp
 */

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

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

    /**
     * Default units defining ellipse axis is kilometers
     * ({@value}).
     */
    public static final String DEFAULT_AXIS_UNITS = "km";

    /**
     * Default units defining ellipse rotation is "{@value}".
     */
    public static final String DEFAULT_ROTATION_UNITS =
        "decimal degrees";

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

    //-----------------------------------------------------------
    // Statics.
    //

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

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

    /**
     * Ellipse major axis.
     */
    public final BigDecimal majorAxis;

    /**
     * Ellipse minor axis.
     */
    public final BigDecimal minorAxis;

    /**
     * Ellipse degrees of rotation.
     */
    public final BigDecimal rotation;

    /**
     * Major, minor axes are defined in this unit. Defaults
     * to {@link #DEFAULT_AXIS_UNITS}.
     */
    public final String axisUnits;

    /**
     * Rotation is defined in this unit. Defaults to
     * {@link #DEFAULT_ROTATION_UNITS}.
     */
    public final String rotationUnits;

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

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

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

        this.center = builder.mCenter;
        this.majorAxis = builder.mMajorAxis;
        this.minorAxis = builder.mMinorAxis;
        this.rotation = builder.mRotation;
        this.axisUnits = builder.mAxisUnits;
        this.rotationUnits = builder.mRotationUnits;
    } // end of GeoEllipse(Builder)

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

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

    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (!retcode && o instanceof GeoEllipse)
        {
            final GeoEllipse ge = (GeoEllipse) o;

            retcode = (center.equals(ge.center) &&
                       majorAxis.equals(ge.majorAxis) &&
                       minorAxis.equals(ge.minorAxis) &&
                       axisUnits.equals(ge.axisUnits) &&
                       rotation.equals(ge.rotation) &&
                       rotationUnits.equals(ge.rotationUnits));
        }

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

    @Override
    public int hashCode()
    {
        return (Objects.hash(center,
                             majorAxis,
                             minorAxis,
                             rotation,
                             axisUnits,
                             rotationUnits));
    } // end of hashCode()

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

        return (retval.append('[')
                      .append(super.toString())
                      .append(", center=").append(center)
                      .append(", major axis=").append(majorAxis)
                      .append(' ').append(axisUnits)
                      .append(", minor axis=").append(minorAxis)
                      .append(' ').append(axisUnits)
                      .append(", rotation=").append(rotation)
                      .append(' ').append(rotationUnits)
                      .append(']')
                      .toString());
    } // end of toString()

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

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

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

    /**
     * Builder class used to create GeoJSON ellipse 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 mMajorAxis;
        private BigDecimal mMinorAxis;
        private BigDecimal mRotation;
        private String mAxisUnits;
        private String mRotationUnits;

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

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

        private Builder()
        {
            super (GeoEllipse.class, GeoType.ELLIPSE);

            mAxisUnits = DEFAULT_AXIS_UNITS;
            mRotationUnits = DEFAULT_ROTATION_UNITS;
        } // end of Builder()

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

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

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

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

        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mCenter, "center")
                         .requireNotNull(mMajorAxis, "majorAxis")
                         .requireNotNull(mMinorAxis, "minorAxis")
                         .requireNotNull(mRotation, "rotation"));
        } // end of validate(Validator)

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

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

        /**
         * Sets GeoJSON ellipse's center coordinate.
         * @param center ellipse'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 ellipse's major axis. Must be > zero.
         * @param axis ellipse major axis.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code axis} is {@code null}.
         * @throws IllegalArgumentException
         * if {@code axis} ≤ zero.
         */
        public Builder majorAxis(final BigDecimal axis)
        {
            Objects.requireNonNull(axis, "axis is null");

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

            mMajorAxis = axis;

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

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

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

            mMinorAxis = axis;

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

        /**
         * Sets GeoJSON ellipse's rotation. Must by ≥ zero.
         * @param rotation ellipse rotation.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code rotation} is {@code null}.
         * @throws IllegalArgumentException
         * if {@code rotation} < zero.
         */
        public Builder rotation(final BigDecimal rotation)
        {
            Objects.requireNonNull(rotation, "rotation is null");

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

            mRotation = rotation;

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

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

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

* @param units axis units. * @return {@code this Builder} instance. * @throws IllegalArgumentException * if {@code units} is either {@code null} or an empty * string. */ public Builder axisUnits(final String units) { if (Strings.isNullOrEmpty(units)) { throw ( new IllegalArgumentException( "units is either null or an empty string")); } mAxisUnits = units; return (this); } // end of axisUnits(String) /** * Sets GeoJSON ellipse's rotation units. Please note * that {@code units} may be set to any * non-{@code null}, non-empty text value. *

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy