Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STRtree & co value generics #797

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*
* @version 1.7
*/
public class ArrayListVisitor
implements ItemVisitor
public class ArrayListVisitor<T>
implements ItemVisitor<T>
{

private ArrayList items = new ArrayList();
private ArrayList<T> items = new ArrayList<>();

/**
* Creates a new instance.
Expand All @@ -36,7 +36,7 @@ public ArrayListVisitor() {
*
* @param item the item to visit
*/
public void visitItem(Object item)
public void visitItem(T item)
{
items.add(item);
}
Expand All @@ -46,6 +46,6 @@ public void visitItem(Object item)
*
* @return the array of items
*/
public ArrayList getItems() { return items; }
public ArrayList<T> getItems() { return items; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
* @version 1.7
*/

public interface ItemVisitor
public interface ItemVisitor<T>
{
/**
* Visits an item in the index.
*
* @param item the index item to be visited
*/
void visitItem(Object item);
void visitItem(T item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
*
* @version 1.7
*/
public interface SpatialIndex
public interface SpatialIndex<T>
{
/**
* Adds a spatial item with an extent specified by the given {@link Envelope} to the index
*/
void insert(Envelope itemEnv, Object item);
void insert(Envelope itemEnv, T item);

/**
* Queries the index for all items whose extents intersect the given search {@link Envelope}
Expand All @@ -41,7 +41,7 @@ public interface SpatialIndex
* @param searchEnv the envelope to query for
* @return a list of the items found by the query
*/
List query(Envelope searchEnv);
List<T> query(Envelope searchEnv);

/**
* Queries the index for all items whose extents intersect the given search {@link Envelope},
Expand All @@ -52,7 +52,7 @@ public interface SpatialIndex
* @param searchEnv the envelope to query for
* @param visitor a visitor object to apply to the items found
*/
void query(Envelope searchEnv, ItemVisitor visitor);
void query(Envelope searchEnv, ItemVisitor<T> visitor);

/**
* Removes a single item from the tree.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@
* @author Martin Davis
*
*/
public class HPRtree
implements SpatialIndex
public class HPRtree<T>
implements SpatialIndex<T>
{
private static final int ENV_SIZE = 4;

private static final int HILBERT_LEVEL = 12;

private static int DEFAULT_NODE_CAPACITY = 16;

private List<Item> items = new ArrayList<Item>();
private List<Item<T>> items = new ArrayList<>();

private int nodeCapacity = DEFAULT_NODE_CAPACITY;

Expand Down Expand Up @@ -108,28 +108,28 @@ public int size() {
}

@Override
public void insert(Envelope itemEnv, Object item) {
public void insert(Envelope itemEnv, T item) {
if (isBuilt) {
throw new IllegalStateException("Cannot insert items after tree is built.");
}
items.add( new Item(itemEnv, item) );
items.add( new Item<>(itemEnv, item) );
totalExtent.expandToInclude(itemEnv);
}

@Override
public List query(Envelope searchEnv) {
public List<T> query(Envelope searchEnv) {
build();

if (! totalExtent.intersects(searchEnv))
return new ArrayList();
return new ArrayList<>();

ArrayListVisitor visitor = new ArrayListVisitor();
ArrayListVisitor<T> visitor = new ArrayListVisitor<>();
query(searchEnv, visitor);
return visitor.getItems();
}

@Override
public void query(Envelope searchEnv, ItemVisitor visitor) {
public void query(Envelope searchEnv, ItemVisitor<T> visitor) {
build();
if (! totalExtent.intersects(searchEnv))
return;
Expand All @@ -141,7 +141,7 @@ public void query(Envelope searchEnv, ItemVisitor visitor) {
}
}

private void queryTopLayer(Envelope searchEnv, ItemVisitor visitor) {
private void queryTopLayer(Envelope searchEnv, ItemVisitor<T> visitor) {
int layerIndex = layerStartIndex.length - 2;
int layerSize = layerSize(layerIndex);
// query each node in layer
Expand All @@ -150,7 +150,7 @@ private void queryTopLayer(Envelope searchEnv, ItemVisitor visitor) {
}
}

private void queryNode(int layerIndex, int nodeOffset, Envelope searchEnv, ItemVisitor visitor) {
private void queryNode(int layerIndex, int nodeOffset, Envelope searchEnv, ItemVisitor<T> visitor) {
int layerStart = layerStartIndex[layerIndex];
int nodeIndex = layerStart + nodeOffset;
if (! intersects(nodeIndex, searchEnv)) return;
Expand All @@ -173,7 +173,7 @@ private boolean intersects(int nodeIndex, Envelope env) {
return ! isBeyond;
}

private void queryNodeChildren(int layerIndex, int blockOffset, Envelope searchEnv, ItemVisitor visitor) {
private void queryNodeChildren(int layerIndex, int blockOffset, Envelope searchEnv, ItemVisitor<T> visitor) {
int layerStart = layerStartIndex[layerIndex];
int layerEnd = layerStartIndex[layerIndex + 1];
for (int i = 0; i < nodeCapacity; i++) {
Expand All @@ -185,14 +185,14 @@ private void queryNodeChildren(int layerIndex, int blockOffset, Envelope searchE
}
}

private void queryItems(int blockStart, Envelope searchEnv, ItemVisitor visitor) {
private void queryItems(int blockStart, Envelope searchEnv, ItemVisitor<T> visitor) {
for (int i = 0; i < nodeCapacity; i++) {
int itemIndex = blockStart + i;
// don't query past end of items
if (itemIndex >= items.size()) break;

// visit the item if its envelope intersects search env
Item item = items.get(itemIndex);
Item<T> item = items.get(itemIndex);
//nodeIntersectsCount++;
if (intersects( item.getEnvelope(), searchEnv) ) {
//if (item.getEnvelope().intersects(searchEnv)) {
Expand Down Expand Up @@ -387,7 +387,7 @@ private void sortItems() {
Collections.sort(items, comp);
}

static class ItemComparator implements Comparator<Item> {
static class ItemComparator implements Comparator<Item<?>> {

private HilbertEncoder encoder;

Expand All @@ -396,7 +396,7 @@ public ItemComparator(HilbertEncoder encoder) {
}

@Override
public int compare(Item item1, Item item2) {
public int compare(Item<?> item1, Item<?> item2) {
int hcode1 = encoder.encode(item1.getEnvelope());
int hcode2 = encoder.encode(item2.getEnvelope());
return Integer.compare(hcode1, hcode2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

import org.locationtech.jts.geom.Envelope;

public class Item {
public class Item<T> {

private Envelope env;
private Object item;
private T item;

public Item(Envelope env, Object item) {
public Item(Envelope env, T item) {
this.env = env;
this.item = item;
}
Expand All @@ -27,7 +27,7 @@ public Envelope getEnvelope() {
return env;
}

public Object getItem() {
public T getItem() {
return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
*
* @version 1.7
*/
public class Quadtree
implements SpatialIndex, Serializable
public class Quadtree<T>
implements SpatialIndex<T>, Serializable
{
private static final long serialVersionUID = -7461163625812743604L;

Expand Down Expand Up @@ -184,13 +184,13 @@ public List OLDquery(Envelope searchEnv)
* @param searchEnv the envelope of the desired query area.
* @return a List of items which may intersect the search envelope
*/
public List query(Envelope searchEnv)
public List<T> query(Envelope searchEnv)
{
/**
* the items that are matched are the items in quads which
* overlap the search envelope
*/
ArrayListVisitor visitor = new ArrayListVisitor();
ArrayListVisitor<T> visitor = new ArrayListVisitor<>();
query(searchEnv, visitor);
return visitor.getItems();
}
Expand All @@ -208,7 +208,7 @@ public List query(Envelope searchEnv)
* @param searchEnv the envelope of the desired query area.
* @param visitor a visitor object which is passed the visited items
*/
public void query(Envelope searchEnv, ItemVisitor visitor)
public void query(Envelope searchEnv, ItemVisitor<T> visitor)
{
/**
* the items that are matched are the items in quads which
Expand All @@ -220,9 +220,9 @@ public void query(Envelope searchEnv, ItemVisitor visitor)
/**
* Return a list of all items in the Quadtree
*/
public List queryAll()
public List<T> queryAll()
{
List foundItems = new ArrayList();
List<T> foundItems = new ArrayList<>();
root.addAllItems(foundItems);
return foundItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class AbstractNode implements Boundable, Serializable {
*/
private static final long serialVersionUID = 6493722185909573708L;

private ArrayList childBoundables = new ArrayList();
private ArrayList<Boundable> childBoundables = new ArrayList<>();
private Object bounds = null;
private int level;

Expand All @@ -59,7 +59,7 @@ public AbstractNode(int level) {
*
* @return a list of the children
*/
public List getChildBoundables() {
public List<Boundable> getChildBoundables() {
return childBoundables;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public void addChildBoundable(Boundable childBoundable) {
childBoundables.add(childBoundable);
}

void setChildBoundables(ArrayList childBoundables)
void setChildBoundables(ArrayList<Boundable> childBoundables)
{
this.childBoundables = childBoundables;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*
* @version 1.7
*/
public abstract class AbstractSTRtree implements Serializable {
public abstract class AbstractSTRtree<T> implements Serializable {

/**
*
Expand Down Expand Up @@ -68,7 +68,7 @@ protected static interface IntersectsOp {
/**
* Set to <tt>null</tt> when index is built, to avoid retaining memory.
*/
private ArrayList itemBoundables = new ArrayList();
private ArrayList<ItemBoundable<T>> itemBoundables = new ArrayList<>();

private int nodeCapacity;

Expand Down Expand Up @@ -112,7 +112,7 @@ public AbstractSTRtree(int nodeCapacity, AbstractNode root) {
* @param nodeCapacity the maximum number of child nodes in a node
* @param itemBoundables the list of leaf nodes in the tree
*/
public AbstractSTRtree(int nodeCapacity, ArrayList itemBoundables) {
public AbstractSTRtree(int nodeCapacity, ArrayList<ItemBoundable<T>> itemBoundables) {
this(nodeCapacity);
this.itemBoundables = itemBoundables;
}
Expand Down Expand Up @@ -260,17 +260,18 @@ protected int depth(AbstractNode node)
}


protected void insert(Object bounds, Object item) {
protected void insert(Object bounds, T item) {
Assert.isTrue(!built, "Cannot insert items into an STR packed R-tree after it has been built.");
itemBoundables.add(new ItemBoundable(bounds, item));
itemBoundables.add(new ItemBoundable<>(bounds, item));
}

/**
* Also builds the tree, if necessary.
* @param <T>
*/
protected List query(Object searchBounds) {
protected List<T> query(Object searchBounds) {
build();
ArrayList matches = new ArrayList();
ArrayList<T> matches = new ArrayList<>();
if (isEmpty()) {
//Assert.isTrue(root.getBounds() == null);
return matches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*
* @version 1.7
*/
public class ItemBoundable implements Boundable, Serializable {
public class ItemBoundable<T> implements Boundable, Serializable {
private Object bounds;
private Object item;
private T item;

public ItemBoundable(Object bounds, Object item) {
public ItemBoundable(Object bounds, T item) {
this.bounds = bounds;
this.item = item;
}
Expand All @@ -32,5 +32,5 @@ public Object getBounds() {
return bounds;
}

public Object getItem() { return item; }
public T getItem() { return item; }
}