
net.sf.eBusx.geo.GeoMultiPolygon 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* Contains zero or more GeoJSON polygons.
*
* @author Charles W. Rapp
*/
public final class GeoMultiPolygon
extends GeoObject
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x070500L;
//-----------------------------------------------------------
// Locals.
//
/**
* GeoJSON polygon array. May be empty but not {@code null}.
*/
public final GeoPolygon[] polygons;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a new multi-polygon instance based on builder
* settings.
* @param builder contains GeoJSON polygons.
*/
private GeoMultiPolygon(final Builder builder)
{
super (builder);
this.polygons = builder.polygons();
} // end of GeoMultiPolygon(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns {@code true} if {@code o} is the same instance as
* {@code this GeoMultiPolygon} or if {@code o} is a
* non-{@code null GeoMultiPolygon} instance whose polygons
* array equals {@code this GeoMultiPolygon}'s array.
* @param o comparison object.
* @return {@code true} if {@code o} equals
* {@code this GeoMultiPolygon} instance.
*/
@Override
public boolean equals(final Object o)
{
boolean retcode = (this == o);
if (!retcode && o instanceof GeoMultiPolygon)
{
retcode =
Arrays.equals(
polygons,
((GeoMultiPolygon) o).polygons);
}
return (retcode);
} // end of equals(Object)
/**
* Returns hash of polygons.
* @return polygons hash.
*/
@Override
public int hashCode()
{
return (Objects.hash((Object[]) polygons));
} // end of hashCode()
@Override
public String toString()
{
final int numPolygons = polygons.length;
String sep;
int i;
final StringBuilder retval = new StringBuilder();
retval.append('[').append(super.toString())
.append(", polygons={");
for (i = 0, sep = ""; i < numPolygons; ++i, sep = ", ")
{
retval.append(sep).append(polygons[i]);
}
return (retval.append("}]").toString());
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Get Methods.
//
/**
* Returns {@code true} if this multi-polygon elements
* has no line polygons; {@code false} otherwise.
* @return {@code true} if this element contains no polygons.
*/
public boolean isEmpty()
{
return (polygons.length == 0);
} // end of isEmpty()
//
// end of Get Methods.
//-----------------------------------------------------------
/**
* Returns a new instance of an {@code GeoMultiPolygon}
* builder.
* @return GeoJSON multi-polygon builder.
*
* @see Builder
*/
public static Builder builder()
{
return (new Builder());
} // end of builder()
//---------------------------------------------------------------
// Inner classes.
//
/**
* Builder class used to create GeoJSON multi-polygon
* instances. 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 final List mPolygons;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
private Builder()
{
super (GeoMultiPolygon.class, GeoType.MULTI_POLYGON);
mPolygons = new ArrayList<>();
} // end of Builder()
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
@Override
protected Builder self()
{
return (this);
} // end of self()
/**
* Returns a new {@code GeoMultiPolygon} instance created
* from this builder's settings.
* @return {@code GeoMultiPolygon} instance.
*/
@Override
protected GeoMultiPolygon buildImpl()
{
return (new GeoMultiPolygon(this));
} // end of buildImpl()
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Get Methods.
//
/**
* Returns polygons list as an array.
* @return polygons array.
*/
private GeoPolygon[] polygons()
{
return (
mPolygons.toArray(
new GeoPolygon[mPolygons.size()]));
} // end of polygons()
//
// end of Get Methods.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
/**
* Appends polygon to polygons list.
* @param polygon append this polygon.
* @return {@code this Builder} instance.
* @throws NullPointerException
* if {@code polygon} is {@code null}.
*/
public Builder add(final GeoPolygon polygon)
{
mPolygons.add(
Objects.requireNonNull(
polygon, "polygon is null"));
return (this);
} // end of add(GeoPolygon)
/**
* Appends polygon array to polygons list. Polygons array
* may be {@code null} or empty. If this case nothing is
* done.
* @param polygons append all polygons to list.
* @return {@code this Builder} instance.
*/
public Builder addAll(@Nullable final GeoPolygon[] polygons)
{
if (polygons != null && polygons.length > 0)
{
Collections.addAll(mPolygons, polygons);
}
return (this);
} // end of addAll(GeoPolygon[])
/**
* Appends polygons collection to polygons list. Polygons
* collection may be {@code null} or empty.
* @param polygons append all polygons to list.
* @return {@code this Builder} instance.
*/
public Builder addAll(@Nullable final Collection polygons)
{
if (polygons != null && !polygons.isEmpty())
{
mPolygons.addAll(polygons);
}
return (this);
} // end of addAll(Collection<>)
/**
* Sets polygons list to given array. Note that all
* previously added polygons are removed from the list
* prior to adding these polygons but after
* determining that the array is not {@code null}.
* @param polygons GeoJSON polygons.
* @return {@code this Builder} instance.
* @throws NullPointerException
* if {@code polygons} is {@code null}. If this exception
* is thrown then polygons list is unchanged.
*/
public Builder polygons(final GeoPolygon[] polygons)
{
Objects.requireNonNull(polygons, "polygons is null");
mPolygons.clear();
Collections.addAll(mPolygons, polygons);
return (this);
} // end of polygons(GeoPolygon[])
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class GeoMultiPolygon
© 2015 - 2025 Weber Informatics LLC | Privacy Policy