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 AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionShapeCollection(List<S> shapes, SpatialContext ctx) WARNING:shapesis copied by reference. -
Method Summary
Modifier and TypeMethodDescriptionprotected RectanglecomputeBoundingBox(Collection<? extends Shape> shapes, SpatialContext ctx) protected static booleancomputeMutualDisjoint(List<? extends Shape> shapes) Computes whether the shapes are mutually disjoint.booleanThe sub-classes of Shape generally implement the same contract forObject.equals(Object)andObject.hashCode()amongst the same sub-interface type.get(int index) doublegetArea(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 ShapebooleanhasArea()Does the shape have area? This will be false for points and lines.inthashCode()Describe the relationship between the two objects.protected booleanCalled by relate() to determine whether to return early if it finds CONTAINS, instead of checking the remaining shapes.intsize()toString()Methods inherited from class AbstractList
add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subListMethods inherited from class AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface 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:shapesis copied by reference.- Parameters:
shapes- Copied by reference! (make a defensive copy if caller modifies)
-
-
Method Details
-
computeBoundingBox
-
getShapes
-
get
-
size
-
getBoundingBox
Description copied from interface:ShapeGet 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:
getBoundingBoxin interfaceShape
-
getCenter
-
hasArea
public boolean hasArea()Description copied from interface:ShapeDoes 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:ShapeReturns 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:
getBufferedin interfaceShape- Returns:
- Not null, and the returned shape should contain the current shape.
-
relate
Description copied from interface:ShapeDescribe 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:ShapeCalculates 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:
toStringin classAbstractCollection<S extends Shape>
-
equals
Description copied from interface:ShapeThe 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
-
getContext
Description copied from interface:ShapeGet the SpatialContext that created the Shape- Specified by:
getContextin interfaceShape
-