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

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

//
// Copyright 2021 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 java.util.Objects;
import net.sf.eBus.util.Validator;

/**
 * Contains a single GeoJSON {@link Position position}.
 *
 * @author Charles W. Rapp
 */

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

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

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

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

    /**
     * Single position containing the latitude, longitude, and
     * (optionally) elevation.
     */
    public final Position position;

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

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

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

        this.position = builder.mPosition;
    } // end of GeoPoint(Builder)

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

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

    /**
     * Returns text containing the single position.
     * @return GeoJSON point represented as human-readable text.
     */
    @Override
    public String toString()
    {
        final StringBuilder retval = new StringBuilder();

        return (retval.append('[').append(super.toString())
                      .append(", pos=").append(position)
                      .append(']')
                      .toString());
    } // end of toString()

    /**
     * Returns {@code true} if {@code o} is the same instance as
     * {@code this GeoPoint} or if {@code o} is a
     * non-{@code null GeoPoint} instance whose position equals
     * {@code this GeoPoint} position.
     * @param o comparison object.
     * @return {@code true} if {@code o} is a
     * non-{@code null GeoPoint} instance whose position equals
     * {@code this GeoPoint} position.
     */
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (!retcode && o instanceof GeoPoint)
        {
            retcode = position.equals(((GeoPoint) o).position);
        }

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

    /**
     * Returns position hash code.
     * @return GeoJSOM position as a hash value.
     */
    @Override
    public int hashCode()
    {
        return (position.hashCode());
    } // end of hashCode()

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

    /**
     * Returns a new instance of an {@code GeoPoint} builder.
     * @return GeoJSON point builder.
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

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

    /**
     * Builder class used to create {@code GeoPoint} instances.
     * Since {@code Builder} constructor is private the only
     * was to obtain a {@code Builder} instance is by calling
     * {@link GeoPoint#builder()}.
     */
    public static final class Builder
        extends GeoObject.GeoBuilder
    {
    //-----------------------------------------------------------
    // Member data.
    //

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

        private Position mPosition;

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

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

        private Builder()
        {
            super (GeoPoint.class, GeoType.POINT);
        } // end of Builder()

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

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

        /**
         * Checks if position has been set.
         * @param problems add each detected problem to this
         * validator.
         * @return {@code problems}.
         */
        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mPosition, "position"));
        } // end of validate(Validator)

        /**
         * Returns a new {@code GeoPoint} instance created from
         * this builder's settings.
         * @return OpenStreetMap node instance.
         */
        @Override
        protected GeoPoint buildImpl()
        {
            return (new GeoPoint(this));
        } // end of buildImpl()

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

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

        /**
         * Sets GeoJSON position. Returns
         * {@code this Builder} instance so that builder method
         * calls can be chained.
         * @param position single position.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code position} is {@code null}.
         */
        public Builder position(final Position position)
        {
            mPosition =
                Objects.requireNonNull(
                    position, "position is null");

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

        //
        // end of Set Methods.
        //-------------------------------------------------------
    } // end of class Builder
} // end of class GeoPoint




© 2015 - 2024 Weber Informatics LLC | Privacy Policy