01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.index.quadtree;
17:
18: import java.io.IOException;
19: import java.util.AbstractCollection;
20: import java.util.Collection;
21: import java.util.Iterator;
22: import java.util.logging.Logger;
23:
24: import com.vividsolutions.jts.geom.Envelope;
25:
26: /**
27: * A collection that will open and close the QuadTree and find the next id in
28: * the index.
29: *
30: * @author Jesse
31: *
32: */
33: public class LazySearchCollection extends AbstractCollection implements
34: Collection {
35:
36: private QuadTree tree;
37:
38: private Envelope bounds;
39:
40: public LazySearchCollection(QuadTree tree, Envelope bounds) {
41: this .tree = tree;
42: this .bounds = bounds;
43: }
44:
45: /*
46: * (non-Javadoc)
47: *
48: * @see java.util.AbstractCollection#iterator()
49: */
50: public Iterator iterator() {
51: LazySearchIterator object;
52: try {
53: object = new LazySearchIterator(tree.getRoot().copy(), tree
54: .getIndexfile(), bounds);
55: } catch (IOException e) {
56: throw new RuntimeException(e);
57: }
58: tree.registerIterator(object);
59: return object;
60: }
61:
62: public int size() {
63: Iterator iter = iterator();
64: try {
65: int count = 0;
66: while (iter.hasNext()) {
67: iter.next();
68: count++;
69: }
70: return count;
71: } finally {
72: try {
73: tree.close(iter);
74: } catch (StoreException e) {
75: org.geotools.util.logging.Logging.getLogger(
76: "org.geotools.index.quadtree").severe(
77: "Couldn't close iterator");
78: }
79: }
80: }
81:
82: public boolean isEmpty() {
83: Iterator iter = iterator();
84: boolean isEmtpy = true;
85: try {
86: isEmtpy = !iter.hasNext();
87: } finally {
88: try {
89: tree.close(iter);
90: } catch (StoreException e) {
91: org.geotools.util.logging.Logging.getLogger(
92: "org.geotools.index.quadtree").severe(
93: "Couldn't close iterator");
94: }
95: }
96: return isEmtpy;
97: }
98:
99: }
|