Class ShapeCollection<S extends Shape>
- All Implemented Interfaces:
Iterable<S>
,Collection<S>
,List<S>
,SequencedCollection<S>
,Shape
Ideally, 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.
-
Field Summary
FieldsFields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorsConstructorDescriptionShapeCollection
(List<S> shapes, SpatialContext ctx) WARNING:shapes
is copied by reference. -
Method Summary
Modifier and TypeMethodDescriptionprotected Rectangle
computeBoundingBox
(Collection<? extends Shape> shapes, SpatialContext ctx) protected static boolean
computeMutualDisjoint
(List<? extends Shape> shapes) Computes whether the shapes are mutually disjoint.boolean
The sub-classes of Shape generally implement the same contract forObject.equals(Object)
andObject.hashCode()
amongst the same sub-interface type.get
(int index) double
getArea
(SpatialContext ctx) Calculates the area of the shape, in square-degrees.Get the bounding box for this Shape.getBuffered
(double distance, SpatialContext ctx) Returns a buffered version of this shape.Returns the center point of this shape.Get the SpatialContext that created the Shapeboolean
hasArea()
Does the shape have area? This will be false for points and lines.int
hashCode()
Describe the relationship between the two objects.protected boolean
Called by relate() to determine whether to return early if it finds CONTAINS, instead of checking the remaining shapes.int
size()
toString()
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, isEmpty, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator, toArray, toArray
-
Field Details
-
ctx
-
shapes
-
bbox
-
-
Constructor Details
-
ShapeCollection
WARNING:shapes
is copied by reference.- Parameters:
shapes
- Copied by reference! (make a defensive copy if caller modifies)
-
-
Method Details
-
computeBoundingBox
-
getShapes
-
get
-
size
public int size() -
getBoundingBox
Description copied from interface:Shape
Get the bounding box for this Shape. This means the shape is within the bounding box and that it touches each side of the rectangle.Postcondition:
this.getBoundingBox().relate(this) == CONTAINS
- Specified by:
getBoundingBox
in interfaceShape
-
getCenter
Description copied from interface:Shape
Returns the center point of this shape. This is usually the same asgetBoundingBox().getCenter()
but it doesn't have to be.Postcondition:
this.relate(this.getCenter()) == CONTAINS
-
hasArea
public boolean hasArea()Description copied from interface:Shape
Does the shape have area? This will be false for points and lines. It will also be false for shapes that normally have area but are constructed in a degenerate case as to not have area (e.g. a circle with 0 radius or rectangle with no height or no width). -
getBuffered
Description copied from interface:Shape
Returns a buffered version of this shape. The buffer is usually a rounded-corner buffer, although some shapes might buffer differently. This is an optional operation.- Specified by:
getBuffered
in interfaceShape
- Returns:
- Not null, and the returned shape should contain the current shape.
-
relate
Description copied from interface:Shape
Describe the relationship between the two objects. For example- this is WITHIN other
- this CONTAINS other
- this is DISJOINT other
- this INTERSECTS other
If the shapes are equal then the result is CONTAINS (preferred) or WITHIN.
-
relateContainsShortCircuits
protected boolean relateContainsShortCircuits()Called by relate() to determine whether to return early if it finds CONTAINS, instead of checking the remaining shapes. It will do so without calling this method if the "other" shape is a Point. If a remaining shape finds INTERSECTS, then INTERSECTS will be returned. The only problem with this returning true is that if some of the shapes overlap, it's possible that the result of relate() could be dependent on the order of the shapes, which could be unexpected / wrong depending on the application. The default implementation returns true because it probably doesn't matter. If it does, a subclass could add a boolean flag that this method could return. That flag could be initialized to true only if the shapes are mutually disjoint.- See Also:
-
computeMutualDisjoint
Computes whether the shapes are mutually disjoint. This is a utility method offered for use by a subclass implementingrelateContainsShortCircuits()
. 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. -
getArea
Description copied from interface:Shape
Calculates the area of the shape, in square-degrees. If ctx is null then simple Euclidean calculations will be used. This figure can be an estimate. -
toString
- Overrides:
toString
in classAbstractCollection<S extends Shape>
-
equals
Description copied from interface:Shape
The sub-classes of Shape generally implement the same contract forObject.equals(Object)
andObject.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. -
hashCode
public int hashCode() -
getContext
Description copied from interface:Shape
Get the SpatialContext that created the Shape- Specified by:
getContext
in interfaceShape
-