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