public class ShapeCollection<S extends Shape> extends AbstractList<S> implements Shape
relate(Shape) should return the same result no matter what
the shape order is, although the default implementation can be order
dependent when the shapes overlap; see relateContainsShortCircuits().
To improve performance slightly, the caller could order the shapes by
largest first so that relate() will have a greater chance of
short-circuit'ing sooner. As the Shape contract states; it may return
intersects when the best answer is actually contains or within. If any shape
intersects the provided shape then that is the answer.
This implementation is not optimized for a large number of shapes; relate is
O(N). A more sophisticated implementation might do an R-Tree based on
bbox'es, for example.| Modifier and Type | Field and Description |
|---|---|
protected Rectangle |
bbox |
protected SpatialContext |
ctx |
protected List<S> |
shapes |
modCount| Constructor and Description |
|---|
ShapeCollection(List<S> shapes,
SpatialContext ctx)
WARNING:
shapes is copied by reference. |
| Modifier and Type | Method and Description |
|---|---|
protected Rectangle |
computeBoundingBox(Collection<? extends Shape> shapes,
SpatialContext ctx) |
protected static boolean |
computeMutualDisjoint(List<? extends Shape> shapes)
Computes whether the shapes are mutually disjoint.
|
boolean |
equals(Object o)
The sub-classes of Shape generally implement the
same contract for
Object.equals(Object) and Object.hashCode()
amongst the same sub-interface type. |
S |
get(int index) |
double |
getArea(SpatialContext ctx)
Calculates the area of the shape, in square-degrees.
|
Rectangle |
getBoundingBox()
Get the bounding box for this Shape.
|
ShapeCollection |
getBuffered(double distance,
SpatialContext ctx)
Returns a buffered version of this shape.
|
Point |
getCenter()
Returns the center point of this shape.
|
SpatialContext |
getContext()
Get the SpatialContext that created the Shape
|
List<S> |
getShapes() |
boolean |
hasArea()
Does the shape have area? This will be false for points and lines.
|
int |
hashCode() |
SpatialRelation |
relate(Shape other)
Describe the relationship between the two objects.
|
protected boolean |
relateContainsShortCircuits()
Called by relate() to determine whether to return early if it finds
CONTAINS, instead of checking the remaining shapes.
|
int |
size() |
String |
toString() |
add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subListaddAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayclone, finalize, getClass, notify, notifyAll, wait, wait, waitaddAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArrayparallelStream, removeIf, streamprotected final SpatialContext ctx
protected final Rectangle bbox
public ShapeCollection(List<S> shapes, SpatialContext ctx)
shapes is copied by reference.shapes - Copied by reference! (make a defensive copy if caller modifies)ctx - protected Rectangle computeBoundingBox(Collection<? extends Shape> shapes, SpatialContext ctx)
public S get(int index)
public int size()
public Rectangle getBoundingBox()
Shapethis.getBoundingBox().relate(this) == CONTAINSgetBoundingBox in interface Shapepublic Point getCenter()
ShapegetBoundingBox().getCenter() but it doesn't have to be.
Postcondition: this.relate(this.getCenter()) == CONTAINSpublic boolean hasArea()
Shapepublic ShapeCollection getBuffered(double distance, SpatialContext ctx)
ShapegetBuffered in interface Shapepublic SpatialRelation relate(Shape other)
Shapeprotected boolean relateContainsShortCircuits()
.protected static boolean computeMutualDisjoint(List<? extends Shape> shapes)
relateContainsShortCircuits().
Beware: this is an O(N^2) algorithm.. Consequently, consider safely
assuming non-disjoint if shapes.size() > 10 or something. And if all shapes
are a Point then the result of this method doesn't ultimately matter.public double getArea(SpatialContext ctx)
Shapepublic String toString()
toString in class AbstractCollection<S extends Shape>public boolean equals(Object o)
ShapeObject.equals(Object) and Object.hashCode()
amongst the same sub-interface type. This means, for example, that multiple
Point implementations of different classes are equal if they share the same x
& y.public int hashCode()
public SpatialContext getContext()
ShapegetContext in interface ShapeCopyright © 2025. All rights reserved.