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

net.sf.eBusx.geo.GeoMultiLineString 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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Contains zero or more GeoJSON line strings.
 *
 * @author Charles W. Rapp
 */

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

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

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

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

    /**
     * GeoJSON line string array. May be empty but not
     * {@code null}.
     */
    public final LineString[] lineStrings;

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

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

    /**
     * Creates a new multi-string instance based on the builder
     * settings.
     * @param builder contains GeoJSON strings.
     */
    private GeoMultiLineString(final Builder builder)
    {
        super (builder);

        this.lineStrings = builder.lineStrings();
    } // end of GeoMultiLineString(Builder)

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

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

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

        if (!retcode && o instanceof GeoMultiLineString)
        {
            retcode =
                Arrays.equals(lineStrings,
                    ((GeoMultiLineString) o).lineStrings);
        }

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

    /**
     * Returns hash of line strings.
     * @return line strings hash.
     */
    @Override
    public int hashCode()
    {
        return (Objects.hash((Object[]) lineStrings));
    } // end of hashCode()

    /**
     * Returns human-readable text containing GeoJSON line
     * strings.
     * @return GeoJSON line strings as text.
     */
    @Override
    public String toString()
    {
        final int numRings = lineStrings.length;
        String sep;
        int i;
        final StringBuilder retval = new StringBuilder();

        retval.append('[').append(super.toString())
              .append(", rings={");

        for (i = 0, sep = ""; i < numRings; ++i, sep = ", ")
        {
            retval.append(sep).append(lineStrings[i]);
        }

        retval.append("}]");

        return (retval.toString());
    } // end of toString()

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

    /**
     * Returns a new instance of an {@code GeoMultiLineString}
     * builder.
     * @return GeoJSON multi-string builder.
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

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

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

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

        private final List mLineStrings;

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

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

        private Builder()
        {
            super (GeoMultiLineString.class,
                   GeoType.MULTI_LINE_STRING);

            mLineStrings = new ArrayList<>();
        } // end of Builder()

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

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

        /**
         * Returns a new {@code GeoMultiLineString} instance
         * created from this builders settings.
         * @return {@code GeoMultiLineString} instance.
         */
        @Override
        protected GeoMultiLineString buildImpl()
        {
            return (new GeoMultiLineString(this));
        } // end of buildImpl()

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

        //-------------------------------------------------------
        // Get Methods.
        //

        /**
         * Returns line strings list as an array.
         * @return line strings array.
         */
        private LineString[] lineStrings()
        {
            return (
                mLineStrings.toArray(
                    new LineString[mLineStrings.size()]));
        } // end of lineStrings()

        //
        // end of Get Methods.
        //-------------------------------------------------------

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

        /**
         * Appends line string to line strings list.
         * @param ls append this line string.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code ls} is {@code null}.
         */
        public Builder add(final LineString ls)
        {
            mLineStrings.add(
                Objects.requireNonNull(ls, "ls is null"));

            return (this);
        } // end of add(LineString)

        /**
         * Appends line string array to line strings list. Line
         * string array may be {@code null} or empty.
         * @param ls append all line strings to list.
         * @return {@code this Builder} instance.
         */
        public Builder addAll(final LineString[] ls)
        {
            if (ls != null && ls.length > 0)
            {
                Collections.addAll(mLineStrings, ls);
            }

            return (this);
        } // end of addAll(LineString[])

        /**
         * Appends line string collection to line strings list.
         * Line string collection may be {@code null} or empty.
         * @param ls append all line strings to list.
         * @return {@code this Builder} instance.
         */
        public Builder addAll(final Collection ls)
        {
            if (ls != null && !ls.isEmpty())
            {
                mLineStrings.addAll(ls);
            }

            return (this);
        } // end of addAll(Collection<>)

        /**
         * Sets line strings list to the given array. Note that
         * all previously added line strings are removed from
         * the list prior to adding these line strings but
         * after that the array is not {@code null}.
         * @param ls line strings.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code ls} is {@code null}. If this exception is
         * thrown then line strings list is unchanged.
         */
        public Builder lineStrings(final LineString[] ls)
        {
            Objects.requireNonNull(ls, "lineStrings is null");

            mLineStrings.clear();
            Collections.addAll(mLineStrings, ls);

            return (this);
        } // end of lineStrings(LineString[])

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy