net.sf.eBusx.geo.GeoMultiPoint 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;
import net.sf.eBusx.geo.GeoObject.GeoType;
/**
* Contains {@link Position} array which may be empty.
*
* @author Charles W. Rapp
*/
public final class GeoMultiPoint
extends GeoObject
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x500700L;
//-----------------------------------------------------------
// Locals.
//
/**
* Positions contained in this multi-point GeoJSON object.
*/
public final Position[] positions;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a new instance of GeoMultiPoint.
*/
private GeoMultiPoint(final Builder builder)
{
super (builder);
this.positions = builder.positions();
} // end of GeoMultiPoint(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns a single text line containing all the positions.
* @return text listing positions.
*/
@Override
public String toString()
{
final int numPos = positions.length;
String sep = "";
final StringBuilder retval = new StringBuilder();
retval.append("pos={");
for (int i = 0; i < numPos; ++i, sep = ", ")
{
retval.append(sep).append(positions[i]);
}
return (retval.append("}]").toString());
} // end of toString()
/**
* Returns {@code true} if {@code o} is a
* non-{@code null GeoMultiPoint} with a position array
* equaling {@code this GeoMultiPoint} position array.
* Otherwise returns {@code false}.
* @param o comparison object.
* @return {@code true} if {@code o} is a
* non-{@code null GeoMultiPoint} with a position array
* equaling {@code this GeoMultiPoint} position array.
*/
@Override
public boolean equals(final Object o)
{
boolean retcode = (this == o);
if (!retcode && o instanceof GeoMultiPoint)
{
retcode =
Arrays.equals(
positions, ((GeoMultiPoint) o).positions);
}
return (retcode);
} // end of equals(Object)
/**
* Returns hash code of contained positions.
* @return contained positions hash code.
*/
@Override
public int hashCode()
{
return (Objects.hash((Object[]) positions));
} // end of hashCode()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
/**
* Returns a new GeoJSON multi-point builder instance.
* @return {@code GeoMultiPoint} builder instance.
*/
public static Builder builder()
{
return (new Builder());
} // end of builder()
//---------------------------------------------------------------
// Inner classes.
//
/**
* Builder class used to create {@code GeoMultiPoint}
* instances. It is suggested that a new builder instance
* be created for each new multi-point instance rather than
* re-using builder instances.
*/
public static final class Builder
extends GeoObject.GeoBuilder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Constants.
//
private static final String POSITIONS_ERROR =
"positions is null";
//-------------------------------------------------------
// Locals.
//
/**
* Place positions into this list which is used to create
* array.
*/
private final List mPositions;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
private Builder()
{
super (GeoMultiPoint.class, GeoType.MULTIPOINT);
mPositions = new ArrayList<>();
} // end of Builder()
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
/**
* Returns a new {@code GeoMultiPoint} instance
* configured from {@code this Builder}'s validated
* settings.
* @return new multi-point GeoJSON instance.
*/
@Override
protected GeoMultiPoint buildImpl()
{
return (new GeoMultiPoint(this));
} // end of buildImpl()
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Get Methods.
//
/**
* Returns positions list as an array.
* @return positions array.
*/
private Position[] positions()
{
return (
mPositions.toArray(
new Position[mPositions.size()]));
} // end of positions()
//
// end of Get Methods.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
/**
* Appends position to positions list.
* @param pos append this position.
* @return {@code this Builder} instance.
* @throws NullPointerException
* if {@code pos} is {@code null}.
*
* @see #addAll(Position[])
* @see #addAll(Collection)
* @see #positions(Position[])
*/
public Builder add(final Position pos)
{
mPositions.add(
Objects.requireNonNull(pos, "pos is null"));
return (this);
} // end of add(Position)
/**
* Appends position array to positions list.
* @param positions append all positions to list.
* @return {@code this Builder} instance.
* @throws NullPointerException
* if {@code positions} is {@code null}.
*
* @see #add(Position)
* @see #addAll(Collection)
* @see #positions(Position[])
*/
public Builder addAll(final Position[] positions)
{
Collections.addAll(
mPositions,
Objects.requireNonNull(
positions, POSITIONS_ERROR));
return (this);
} // end of addAll(Position[])
/**
* Appends position collection to positions list.
* @param positions append all positions to list.
* @return {@code this Builder} instance.
* @throws NullPointerException
* if {@code positions} is {@code null}.
*
* @see #add(Position)
* @see #addAll(Position[])
* @see #positions(Position[])
*/
public Builder addAll(final Collection positions)
{
mPositions.addAll(
Objects.requireNonNull(
positions, POSITIONS_ERROR));
return (this);
} // end of addAll(Collection<>)
/**
* Sets positions list to the given position values. Note
* that all previously a-dded positions are removed from
* the list prior to adding these positions but
* after verifying that positions is not
* {@code null}.
* @param positions set position list
* @return {@code this Builder} instance.
* @throws NullPointerException
* if {@code positions} is {@code null}. If this
* exception is thrown then positions list is unchanged.
*
* @see #add(Position)
* @see #addAll(Position[])
* @see #addAll(Collection)
*/
public Builder positions(final Position[] positions)
{
Objects.requireNonNull(positions, POSITIONS_ERROR);
mPositions.clear();
Collections.addAll(mPositions, positions);
return (this);
} // end of positions(Position[])
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class GeoMultiPoint