Revision 23675
branches/v2_0_0_prep/libraries/libFMap_data/src-test/org/gvsig/fmap/data/feature/FeatureCollectionTestParent.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {DiSiD Technologies} {{Test indexed iterator retrieval}} |
|
26 |
*/ |
|
27 |
package org.gvsig.fmap.data.feature; |
|
28 |
|
|
29 |
import java.util.Iterator; |
|
30 |
|
|
31 |
import junit.framework.TestCase; |
|
32 |
|
|
33 |
import org.gvsig.fmap.data.DataManager; |
|
34 |
import org.gvsig.fmap.data.DataStoreParameters; |
|
35 |
import org.gvsig.fmap.data.ReadException; |
|
36 |
|
|
37 |
/** |
|
38 |
* Unit tests for the {@link AbstractFeatureCollection} class. |
|
39 |
* |
|
40 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
41 |
*/ |
|
42 |
public abstract class FeatureCollectionTestParent extends TestCase { |
|
43 |
|
|
44 |
|
|
45 |
protected void setUp() throws Exception { |
|
46 |
super.setUp(); |
|
47 |
register(); |
|
48 |
} |
|
49 |
|
|
50 |
protected void tearDown() throws Exception { |
|
51 |
super.tearDown(); |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* Test method for |
|
56 |
* {@link org.gvsig.fmap.data.feature.AbstractFeatureCollection#iterator(int)} |
|
57 |
* . |
|
58 |
* |
|
59 |
* @throws Exception |
|
60 |
*/ |
|
61 |
public void testIteratorInt() throws Exception { |
|
62 |
FeatureCollection featureCollection = createFeatureCollection(); |
|
63 |
testIteratorInt(featureCollection); |
|
64 |
featureCollection.dispose(); |
|
65 |
} |
|
66 |
|
|
67 |
public void testIteratorInt(FeatureCollection featureCollection) { |
|
68 |
|
|
69 |
if (featureCollection.size() < 3) { |
|
70 |
fail("The Collection to create for the test must contain " |
|
71 |
+ "at least 3 values"); |
|
72 |
} |
|
73 |
|
|
74 |
try { |
|
75 |
featureCollection.iterator(-5); |
|
76 |
fail("Iterator index accepted with a value < 0"); |
|
77 |
} catch (Exception ex) { |
|
78 |
// Good |
|
79 |
} |
|
80 |
|
|
81 |
try { |
|
82 |
featureCollection.iterator(featureCollection.size() + 2); |
|
83 |
fail("Iterator index accepted with a value > collection size"); |
|
84 |
} catch (Exception ex) { |
|
85 |
// Good |
|
86 |
} |
|
87 |
|
|
88 |
int index = featureCollection.size() - 3; |
|
89 |
Iterator iter = featureCollection.iterator(index); |
|
90 |
int iterations = 0; |
|
91 |
while (iter.hasNext()) { |
|
92 |
iter.next(); |
|
93 |
iterations++; |
|
94 |
} |
|
95 |
assertEquals("The number of iterations remaining is not correct", 3, |
|
96 |
iterations); |
|
97 |
|
|
98 |
iter = featureCollection.iterator(0); |
|
99 |
iterations = 0; |
|
100 |
while (iter.hasNext()) { |
|
101 |
iter.next(); |
|
102 |
iterations++; |
|
103 |
} |
|
104 |
assertEquals("The number of iterations is not the " |
|
105 |
+ "total size of the collection", featureCollection.size(), |
|
106 |
iterations); |
|
107 |
} |
|
108 |
|
|
109 |
protected FeatureCollection createFeatureCollection() throws Exception { |
|
110 |
FeatureStore store = createFeatureStore(); |
|
111 |
FeatureType ft = store.getDefaultFeatureType(); |
|
112 |
return createFeatureCollection(store, ft); |
|
113 |
} |
|
114 |
|
|
115 |
protected FeatureStore createFeatureStore() throws Exception { |
|
116 |
DataManager manager = DataManager.getManager(); |
|
117 |
return (FeatureStore) manager |
|
118 |
.createDataStore(createStoreParameters(manager)); |
|
119 |
} |
|
120 |
|
|
121 |
protected abstract void register(); |
|
122 |
|
|
123 |
protected abstract DataStoreParameters createStoreParameters( |
|
124 |
DataManager manager) |
|
125 |
throws Exception; |
|
126 |
|
|
127 |
/** |
|
128 |
* If this method is rewritten, the returned FeatureCollection must have at |
|
129 |
* least 3 values. |
|
130 |
*/ |
|
131 |
protected abstract FeatureCollection createFeatureCollection( |
|
132 |
FeatureStore store, FeatureType ft) throws ReadException; |
|
133 |
} |
branches/v2_0_0_prep/libraries/libFMap_data/src-test/org/gvsig/fmap/data/feature/paging/FeatureCollectionPagingHelperTest.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {DiSiD Technologies} {{Task}} |
|
26 |
*/ |
|
27 |
package org.gvsig.fmap.data.feature.paging; |
|
28 |
|
|
29 |
import java.util.Iterator; |
|
30 |
|
|
31 |
import junit.framework.TestCase; |
|
32 |
|
|
33 |
import org.easymock.MockControl; |
|
34 |
import org.gvsig.fmap.data.feature.*; |
|
35 |
|
|
36 |
/** |
|
37 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
38 |
*/ |
|
39 |
public class FeatureCollectionPagingHelperTest extends TestCase { |
|
40 |
|
|
41 |
private FeatureCollectionPagingHelper helper; |
|
42 |
private MockControl collectionControl; |
|
43 |
private FeatureCollection collectionMock; |
|
44 |
private MockControl storeControl; |
|
45 |
private FeatureStore storeMock; |
|
46 |
|
|
47 |
protected void setUp() throws Exception { |
|
48 |
super.setUp(); |
|
49 |
collectionControl = MockControl |
|
50 |
.createNiceControl(FeatureCollection.class); |
|
51 |
collectionMock = (FeatureCollection) collectionControl.getMock(); |
|
52 |
storeControl = MockControl.createNiceControl(FeatureStore.class); |
|
53 |
storeMock = (FeatureStore) storeControl.getMock(); |
|
54 |
} |
|
55 |
|
|
56 |
protected void tearDown() throws Exception { |
|
57 |
super.tearDown(); |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* Test method for |
|
62 |
* {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getMaxPageSize()} |
|
63 |
*/ |
|
64 |
public void testGetMaxPageSize() throws Exception { |
|
65 |
storeControl.expectAndReturn(storeMock.getFeatureCollection(), |
|
66 |
collectionMock, 2); |
|
67 |
storeControl.replay(); |
|
68 |
collectionControl.expectAndReturn(collectionMock.size(), 10, 2); |
|
69 |
collectionControl.replay(); |
|
70 |
|
|
71 |
helper = new FeatureCollectionPagingHelperImpl(storeMock); |
|
72 |
assertEquals( |
|
73 |
"El tama?o m?ximo de p?gina no es el establecido en el helper", |
|
74 |
FeatureCollectionPagingHelper.DEFAULT_PAGE_SIZE, helper |
|
75 |
.getMaxPageSize()); |
|
76 |
|
|
77 |
helper = new FeatureCollectionPagingHelperImpl(storeMock, 5); |
|
78 |
assertEquals( |
|
79 |
"El tama?o m?ximo de p?gina no es el establecido en el helper", |
|
80 |
5, helper.getMaxPageSize()); |
|
81 |
|
|
82 |
storeControl.verify(); |
|
83 |
collectionControl.verify(); |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Test method for |
|
88 |
* {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getNumPages()} |
|
89 |
* . |
|
90 |
*/ |
|
91 |
public void testGetNumPages() throws Exception { |
|
92 |
storeControl.expectAndReturn(storeMock.getFeatureCollection(), |
|
93 |
collectionMock); |
|
94 |
storeControl.replay(); |
|
95 |
collectionControl.expectAndReturn(collectionMock.size(), 10); |
|
96 |
collectionControl.replay(); |
|
97 |
|
|
98 |
helper = new FeatureCollectionPagingHelperImpl(storeMock); |
|
99 |
|
|
100 |
assertEquals("El n? de p?ginas calculadas no es correcto", 1, helper |
|
101 |
.getNumPages()); |
|
102 |
|
|
103 |
storeControl.verify(); |
|
104 |
collectionControl.verify(); |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Test method for |
|
109 |
* {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getTotalSize()} |
|
110 |
* . |
|
111 |
*/ |
|
112 |
public void testGetTotalSize() throws Exception { |
|
113 |
storeControl.expectAndReturn(storeMock.getFeatureCollection(), |
|
114 |
collectionMock); |
|
115 |
storeControl.replay(); |
|
116 |
collectionControl.expectAndReturn(collectionMock.size(), 10, 2); |
|
117 |
collectionControl.replay(); |
|
118 |
|
|
119 |
helper = new FeatureCollectionPagingHelperImpl(storeMock); |
|
120 |
|
|
121 |
assertEquals("El n? total de features no es el de la Colecci?n", 10, |
|
122 |
helper.getTotalSize()); |
|
123 |
|
|
124 |
storeControl.verify(); |
|
125 |
collectionControl.verify(); |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Test method for |
|
130 |
* {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getFeatureAt(int)} |
|
131 |
* . |
|
132 |
*/ |
|
133 |
public void testGetFeatureAt() throws Exception { |
|
134 |
// Create two mock features for the test. |
|
135 |
MockControl featureControl = MockControl.createControl(Feature.class); |
|
136 |
Feature mockFeature1 = (Feature) featureControl.getMock(); |
|
137 |
Feature mockFeature2 = (Feature) featureControl.getMock(); |
|
138 |
|
|
139 |
// Create a mock Iterator to return the Features. |
|
140 |
MockControl iteratorControl = MockControl |
|
141 |
.createControl(FeatureIterator.class); |
|
142 |
Iterator mockIterator = (Iterator) iteratorControl.getMock(); |
|
143 |
|
|
144 |
// Values 0 and 1 |
|
145 |
mockIterator.hasNext(); |
|
146 |
iteratorControl.setReturnValue(true); |
|
147 |
mockIterator.next(); |
|
148 |
iteratorControl.setReturnValue(mockFeature1); |
|
149 |
mockIterator.hasNext(); |
|
150 |
iteratorControl.setReturnValue(true); |
|
151 |
mockIterator.next(); |
|
152 |
iteratorControl.setReturnValue(mockFeature2); |
|
153 |
mockIterator.hasNext(); |
|
154 |
iteratorControl.setReturnValue(false); |
|
155 |
|
|
156 |
// Values 4 and 5 |
|
157 |
mockIterator.hasNext(); |
|
158 |
iteratorControl.setReturnValue(true); |
|
159 |
mockIterator.next(); |
|
160 |
iteratorControl.setReturnValue(mockFeature2); |
|
161 |
mockIterator.hasNext(); |
|
162 |
iteratorControl.setReturnValue(true); |
|
163 |
mockIterator.next(); |
|
164 |
iteratorControl.setReturnValue(mockFeature1); |
|
165 |
mockIterator.hasNext(); |
|
166 |
iteratorControl.setReturnValue(false); |
|
167 |
|
|
168 |
iteratorControl.replay(); |
|
169 |
|
|
170 |
// Define the mock Collection operations. |
|
171 |
storeControl.expectAndReturn(storeMock.getFeatureCollection(), |
|
172 |
collectionMock); |
|
173 |
storeControl.replay(); |
|
174 |
|
|
175 |
collectionControl.expectAndReturn(collectionMock.size(), 11); |
|
176 |
collectionControl.expectAndReturn(collectionMock.iterator(0), |
|
177 |
mockIterator); |
|
178 |
collectionControl.expectAndReturn(collectionMock.iterator(4), |
|
179 |
mockIterator); |
|
180 |
collectionControl.replay(); |
|
181 |
|
|
182 |
helper = new FeatureCollectionPagingHelperImpl(storeMock, 2); |
|
183 |
|
|
184 |
// Check the returned Features are the correct ones |
|
185 |
Feature feature = helper.getFeatureAt(0); |
|
186 |
assertEquals("La Feature devuelta no es la que corresponde " |
|
187 |
+ "al ?ndice solicitado: 0", mockFeature1, feature); |
|
188 |
|
|
189 |
feature = helper.getFeatureAt(1); |
|
190 |
assertEquals("La Feature devuelta no es la que corresponde " |
|
191 |
+ "al ?ndice solicitado: 1", mockFeature2, feature); |
|
192 |
|
|
193 |
feature = helper.getFeatureAt(5); |
|
194 |
assertEquals("La Feature devuelta no es la que corresponde " |
|
195 |
+ "al ?ndice solicitado: 5", mockFeature1, feature); |
|
196 |
|
|
197 |
try { |
|
198 |
feature = helper.getFeatureAt(9999); |
|
199 |
fail("Returned a value for a non existent Feature position"); |
|
200 |
} catch (Exception ex) { |
|
201 |
// Good |
|
202 |
} |
|
203 |
|
|
204 |
storeControl.verify(); |
|
205 |
collectionControl.verify(); |
|
206 |
iteratorControl.verify(); |
|
207 |
} |
|
208 |
|
|
209 |
/** |
|
210 |
* Test method for |
|
211 |
* {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#setCurrentPage(int)} |
|
212 |
* . |
|
213 |
*/ |
|
214 |
public void testSetCurrentPage() throws Exception { |
|
215 |
storeControl.expectAndReturn(storeMock.getFeatureCollection(), |
|
216 |
collectionMock); |
|
217 |
storeControl.replay(); |
|
218 |
collectionControl.expectAndReturn(collectionMock.size(), 10); |
|
219 |
collectionControl.replay(); |
|
220 |
|
|
221 |
helper = new FeatureCollectionPagingHelperImpl(storeMock); |
|
222 |
|
|
223 |
try { |
|
224 |
helper.setCurrentPage(-4); |
|
225 |
fail("Allowed to set a negative number as the current page"); |
|
226 |
} catch (Exception ex) { |
|
227 |
// Good |
|
228 |
} |
|
229 |
|
|
230 |
try { |
|
231 |
helper.setCurrentPage(99999); |
|
232 |
fail("Allowed to set a current page greater than the number " |
|
233 |
+ "of available pages"); |
|
234 |
} catch (Exception ex) { |
|
235 |
// Good |
|
236 |
} |
|
237 |
|
|
238 |
storeControl.verify(); |
|
239 |
collectionControl.verify(); |
|
240 |
} |
|
241 |
|
|
242 |
} |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/paging/FeatureCollectionPagingHelper.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {DiSiD Technologies} {Add pagination to a FeatureCollection} |
|
26 |
*/ |
|
27 |
package org.gvsig.fmap.data.feature.paging; |
|
28 |
|
|
29 |
import org.gvsig.fmap.data.feature.Feature; |
|
30 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
|
31 |
import org.gvsig.fmap.data.feature.FeatureStore; |
|
32 |
|
|
33 |
/** |
|
34 |
* Helper interface to access the values of a FeatureCollection by position. |
|
35 |
* |
|
36 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
37 |
*/ |
|
38 |
public interface FeatureCollectionPagingHelper { |
|
39 |
|
|
40 |
static final int DEFAULT_PAGE_SIZE = 100; |
|
41 |
|
|
42 |
/** |
|
43 |
* Returns the current maximum number of Features per page. |
|
44 |
* |
|
45 |
* @return the current maximum page size |
|
46 |
*/ |
|
47 |
int getMaxPageSize(); |
|
48 |
|
|
49 |
/** |
|
50 |
* Sets the current maximum number of Features per page. As the page size |
|
51 |
* changes, the current page of data is reloaded. |
|
52 |
* |
|
53 |
* @param pageSize |
|
54 |
* the maximum number of Feature per page |
|
55 |
*/ |
|
56 |
void setMaxPageSize(int pageSize); |
|
57 |
|
|
58 |
/** |
|
59 |
* Returns the number of pages available in the collection, calculated with |
|
60 |
* the total number of elements and the maximum number of elements per page. |
|
61 |
* |
|
62 |
* @return the number of pages available |
|
63 |
*/ |
|
64 |
int getNumPages(); |
|
65 |
|
|
66 |
/** |
|
67 |
* Returns the number of the currently loaded page of Features (zero based). |
|
68 |
* |
|
69 |
* @return the current page number |
|
70 |
*/ |
|
71 |
int getCurrentPage(); |
|
72 |
|
|
73 |
/** |
|
74 |
* Sets the current page number, and loads the Features for that page. |
|
75 |
* |
|
76 |
* @param page |
|
77 |
* the page to load |
|
78 |
*/ |
|
79 |
void setCurrentPage(int page); |
|
80 |
|
|
81 |
/** |
|
82 |
* Returns the number of elements of the entire Collection. |
|
83 |
* |
|
84 |
* @return the number of elements |
|
85 |
*/ |
|
86 |
int getTotalSize(); |
|
87 |
|
|
88 |
/** |
|
89 |
* Returns the Feature located at the index position. |
|
90 |
* |
|
91 |
* @param index |
|
92 |
* to locate the Feature in the Collection |
|
93 |
* @return the Feature located at the index position |
|
94 |
*/ |
|
95 |
Feature getFeatureAt(int index); |
|
96 |
|
|
97 |
/** |
|
98 |
* Returns all the values of the current loaded page. |
|
99 |
* |
|
100 |
* @return all the values of the current loaded page |
|
101 |
*/ |
|
102 |
Feature[] getCurrentPageFeatures(); |
|
103 |
|
|
104 |
/** |
|
105 |
* Returns the FeatureCollection used to fetch the data. |
|
106 |
* |
|
107 |
* @return the FeatureCollection |
|
108 |
*/ |
|
109 |
FeatureCollection getFeatureCollection(); |
|
110 |
|
|
111 |
/** |
|
112 |
* Sets the FeatureCollection to extract data from. |
|
113 |
* |
|
114 |
* @param featureCollection |
|
115 |
* the FeatureCollection |
|
116 |
*/ |
|
117 |
void setFeatureCollection(FeatureCollection featureCollection); |
|
118 |
|
|
119 |
/** |
|
120 |
* Returns the FeatureStore used to fetch the data. |
|
121 |
* |
|
122 |
* @return the FeatureStore |
|
123 |
*/ |
|
124 |
FeatureStore getFeatureStore(); |
|
125 |
|
|
126 |
/** |
|
127 |
* Reloads the data from the collection. |
|
128 |
*/ |
|
129 |
void reload(); |
|
130 |
} |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/paging/package.html | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8" ?> |
|
2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
3 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
4 |
<head> |
|
5 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
|
6 |
<title>org.gvsig.fmap.data.feature.paging package documentation</title> |
|
7 |
</head> |
|
8 |
<body> |
|
9 |
|
|
10 |
<p>Utilities to perform Features paged loading of a FeatureCollection</p> |
|
11 |
|
|
12 |
</body> |
|
13 |
</html> |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/paging/FeatureCollectionPagingHelperImpl.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Gobernment (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {DiSiD Technologies} {Create a JTable TableModel for a FeatureCollection} |
|
26 |
*/ |
|
27 |
package org.gvsig.fmap.data.feature.paging; |
|
28 |
|
|
29 |
import java.util.Iterator; |
|
30 |
|
|
31 |
import org.gvsig.fmap.data.ReadException; |
|
32 |
import org.gvsig.fmap.data.feature.*; |
|
33 |
|
|
34 |
/** |
|
35 |
* Helper class to access the values of a FeatureCollection by position. Handles |
|
36 |
* pagination automatically to avoid filling the memory in case of big |
|
37 |
* collections. |
|
38 |
* |
|
39 |
* TODO: read values in the background when the returned value is near the end |
|
40 |
* of the page, instead of loading a page on demand. |
|
41 |
* |
|
42 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
43 |
*/ |
|
44 |
public class FeatureCollectionPagingHelperImpl implements |
|
45 |
FeatureCollectionPagingHelper { |
|
46 |
|
|
47 |
private FeatureQuery query; |
|
48 |
|
|
49 |
private FeatureCollection featureCollection; |
|
50 |
|
|
51 |
private FeatureStore featureStore; |
|
52 |
|
|
53 |
private int maxPageSize; |
|
54 |
|
|
55 |
private int numPages; |
|
56 |
|
|
57 |
private int currentPage = -1; |
|
58 |
|
|
59 |
private Feature[] values; |
|
60 |
|
|
61 |
/** |
|
62 |
* Constructs a FeatureCollectionPagingHelper from a FeatureCollection. |
|
63 |
* |
|
64 |
* @param featureCollection |
|
65 |
* to extract data from |
|
66 |
* @throws ReadException |
|
67 |
*/ |
|
68 |
public FeatureCollectionPagingHelperImpl(FeatureStore featureStore) |
|
69 |
throws ReadException { |
|
70 |
this(featureStore, DEFAULT_PAGE_SIZE); |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* Constructs a FeatureCollectionPagingHelper from a FeatureCollection. |
|
75 |
* |
|
76 |
* @param featureCollection |
|
77 |
* to extract data from |
|
78 |
* @param pageSize |
|
79 |
* the number of elements per page data |
|
80 |
* @throws ReadException |
|
81 |
*/ |
|
82 |
public FeatureCollectionPagingHelperImpl(FeatureStore featureStore, |
|
83 |
int pageSize) throws ReadException { |
|
84 |
this(featureStore, null, pageSize); |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Constructs a FeatureCollectionPagingHelper from a FeatureCollection. |
|
89 |
* |
|
90 |
* @param featureCollection |
|
91 |
* to extract data from |
|
92 |
* @throws ReadException |
|
93 |
*/ |
|
94 |
public FeatureCollectionPagingHelperImpl(FeatureStore featureStore, |
|
95 |
FeatureQuery featureQuery) throws ReadException { |
|
96 |
this(featureStore, featureQuery, DEFAULT_PAGE_SIZE); |
|
97 |
} |
|
98 |
|
|
99 |
/** |
|
100 |
* Constructs a FeatureCollectionPagingHelper from a FeatureCollection. |
|
101 |
* |
|
102 |
* @param featureCollection |
|
103 |
* to extract data from |
|
104 |
* @param pageSize |
|
105 |
* the number of elements per page data |
|
106 |
* @throws ReadException |
|
107 |
*/ |
|
108 |
public FeatureCollectionPagingHelperImpl(FeatureStore featureStore, |
|
109 |
FeatureQuery featureQuery, |
|
110 |
int pageSize) throws ReadException { |
|
111 |
this.maxPageSize = pageSize; |
|
112 |
this.featureStore = featureStore; |
|
113 |
this.query = featureQuery; |
|
114 |
loadFeatureCollection(); |
|
115 |
setNumPages(calculateNumPages()); |
|
116 |
} |
|
117 |
|
|
118 |
public int getMaxPageSize() { |
|
119 |
return maxPageSize; |
|
120 |
} |
|
121 |
|
|
122 |
public void setMaxPageSize(int pageSize) { |
|
123 |
this.maxPageSize = pageSize; |
|
124 |
reload(); |
|
125 |
} |
|
126 |
|
|
127 |
public int getNumPages() { |
|
128 |
return numPages; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Sets the number of pages. |
|
133 |
*/ |
|
134 |
private void setNumPages(int numPages) { |
|
135 |
this.numPages = numPages; |
|
136 |
} |
|
137 |
|
|
138 |
public int getCurrentPage() { |
|
139 |
return currentPage; |
|
140 |
} |
|
141 |
|
|
142 |
public void setCurrentPage(int page) { |
|
143 |
System.out.println("Current page: " + page); |
|
144 |
|
|
145 |
if (page < 0) { |
|
146 |
throw new IndexOutOfBoundsException( |
|
147 |
"Error, unable to set helper current page to a " |
|
148 |
+ "negative value: " + page); |
|
149 |
} |
|
150 |
if (page >= getNumPages()) { |
|
151 |
throw new IndexOutOfBoundsException( |
|
152 |
"Error, unable to set helper current page to the page num. " |
|
153 |
+ page |
|
154 |
+ ", as the Collection only has " + getNumPages() |
|
155 |
+ " pages of Features"); |
|
156 |
} |
|
157 |
currentPage = page; |
|
158 |
loadCurrentPageData(); |
|
159 |
} |
|
160 |
|
|
161 |
public int getTotalSize() { |
|
162 |
return getFeatureCollection().size(); |
|
163 |
} |
|
164 |
|
|
165 |
public Feature getFeatureAt(int index) { |
|
166 |
// Check if we have currently loaded the viewed page data, |
|
167 |
// or we need to load a new one |
|
168 |
int pageForIndex = (int) Math.floor(index / getMaxPageSize()); |
|
169 |
|
|
170 |
if (pageForIndex != getCurrentPage()) { |
|
171 |
setCurrentPage(pageForIndex); |
|
172 |
} |
|
173 |
|
|
174 |
int positionForIndex = index - (getCurrentPage() * getMaxPageSize()); |
|
175 |
|
|
176 |
return values[positionForIndex]; |
|
177 |
} |
|
178 |
|
|
179 |
public Feature[] getCurrentPageFeatures() { |
|
180 |
return values; |
|
181 |
} |
|
182 |
|
|
183 |
public FeatureCollection getFeatureCollection() { |
|
184 |
return featureCollection; |
|
185 |
} |
|
186 |
|
|
187 |
public void reload() { |
|
188 |
setNumPages(calculateNumPages()); |
|
189 |
loadCurrentPageData(); |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* Sets the FeatureCollection to extract data from. |
|
194 |
*/ |
|
195 |
public void setFeatureCollection(FeatureCollection featureCollection) { |
|
196 |
this.featureCollection = featureCollection; |
|
197 |
reload(); |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* Returns the FeatureStore of the Collection. |
|
202 |
* |
|
203 |
* @return the featureStore |
|
204 |
*/ |
|
205 |
public FeatureStore getFeatureStore() { |
|
206 |
return featureStore; |
|
207 |
} |
|
208 |
|
|
209 |
/** |
|
210 |
* Calculates the number of pages. |
|
211 |
*/ |
|
212 |
private int calculateNumPages() { |
|
213 |
return ((int) Math.floor(getTotalSize() / getMaxPageSize())) + 1; |
|
214 |
} |
|
215 |
|
|
216 |
/** |
|
217 |
* Loads all the Features of the current page. |
|
218 |
*/ |
|
219 |
private void loadCurrentPageData() { |
|
220 |
int currentPage = getCurrentPage(); |
|
221 |
int currentPageSize; |
|
222 |
if (currentPage < (numPages - 1)) { |
|
223 |
currentPageSize = getMaxPageSize(); |
|
224 |
} else { |
|
225 |
currentPageSize = getTotalSize() - (currentPage * getMaxPageSize()); |
|
226 |
} |
|
227 |
|
|
228 |
values = new Feature[currentPageSize]; |
|
229 |
|
|
230 |
int firstPosition = currentPage * getMaxPageSize(); |
|
231 |
|
|
232 |
Iterator iter = getFeatureCollection().iterator(firstPosition); |
|
233 |
int i = 0; |
|
234 |
while (iter.hasNext() && i < currentPageSize) { |
|
235 |
values[i] = (Feature) iter.next(); |
|
236 |
i++; |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 |
private void loadFeatureCollection() throws ReadException { |
|
241 |
if (query == null) { |
|
242 |
featureCollection = getFeatureStore().getFeatureCollection(); |
|
243 |
} else { |
|
244 |
featureCollection = getFeatureStore().getFeatureCollection(query); |
|
245 |
} |
|
246 |
} |
|
247 |
} |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/joinstore/JoinFeatureCollection.java | ||
---|---|---|
5 | 5 |
|
6 | 6 |
import org.gvsig.fmap.data.DataStore; |
7 | 7 |
import org.gvsig.fmap.data.ReadException; |
8 |
import org.gvsig.fmap.data.feature.AbstractFeatureCollection; |
|
9 |
import org.gvsig.fmap.data.feature.Feature; |
|
10 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
|
11 |
import org.gvsig.fmap.data.feature.FeatureType; |
|
8 |
import org.gvsig.fmap.data.feature.*; |
|
12 | 9 |
|
13 | 10 |
public class JoinFeatureCollection extends AbstractFeatureCollection{ |
14 | 11 |
private FeatureType featureType; |
... | ... | |
39 | 36 |
return complexIter; |
40 | 37 |
} |
41 | 38 |
|
39 |
/** |
|
40 |
* TODO: test it |
|
41 |
*/ |
|
42 |
protected Iterator internalIterator(int index) { |
|
43 |
this.checkModified(); |
|
44 |
JoinIterator complexIter = new JoinIterator(this.joinStore, |
|
45 |
this.featureType, this.collection.iterator(index)); |
|
46 |
|
|
47 |
return complexIter; |
|
48 |
} |
|
49 |
|
|
42 | 50 |
protected class JoinIterator implements Iterator{ |
43 | 51 |
|
44 | 52 |
private FeatureType featureType; |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/joinstore/JoinFeatureCollectionWithFeatureID.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.data.feature.joinstore; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.Collection; |
|
5 |
import java.util.Comparator; |
|
6 |
import java.util.Iterator; |
|
7 |
import java.util.TreeSet; |
|
3 |
import java.util.*; |
|
8 | 4 |
|
9 | 5 |
import org.gvsig.fmap.data.DataManager; |
10 | 6 |
import org.gvsig.fmap.data.DataStore; |
11 | 7 |
import org.gvsig.fmap.data.ReadException; |
12 |
import org.gvsig.fmap.data.feature.AbstractFeatureCollection; |
|
13 |
import org.gvsig.fmap.data.feature.AbstractFeatureIDIterator; |
|
14 |
import org.gvsig.fmap.data.feature.Feature; |
|
15 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
|
16 |
import org.gvsig.fmap.data.feature.FeatureStore; |
|
17 |
import org.gvsig.fmap.data.feature.FeatureType; |
|
8 |
import org.gvsig.fmap.data.feature.*; |
|
18 | 9 |
import org.gvsig.fmap.data.feature.expressionevaluator.Filter; |
19 | 10 |
import org.gvsig.tools.observer.DefaultObservable; |
20 | 11 |
|
... | ... | |
100 | 91 |
return dbfIter; |
101 | 92 |
} |
102 | 93 |
|
94 |
protected Iterator internalIterator(int index) { |
|
95 |
checkModified(); |
|
96 |
FIDIterator fidIter; |
|
97 |
|
|
98 |
if (featureIDs instanceof List) { |
|
99 |
fidIter = new FIDIterator(((List) featureIDs).listIterator(index), |
|
100 |
featureType); |
|
101 |
} else { |
|
102 |
// TODO: If featureIDs is not a Collection of type List (actually, |
|
103 |
// only when ordering is applied), we are not able |
|
104 |
// to get a listIterator with an index, so we have to move the |
|
105 |
// iterator to the required position. This option will be probably |
|
106 |
// a lot slower, so an option would be to order with a List |
|
107 |
// instead of a TreeSet. |
|
108 |
Iterator iter = featureIDs.iterator(); |
|
109 |
for (int i = 0; i < index; i++) { |
|
110 |
iter.next(); |
|
111 |
} |
|
112 |
|
|
113 |
fidIter = new FIDIterator(iter, featureType); |
|
114 |
} |
|
115 |
|
|
116 |
return fidIter; |
|
117 |
} |
|
118 |
|
|
103 | 119 |
public Object[] toArray() { |
104 | 120 |
checkModified(); |
105 | 121 |
ArrayList features= new ArrayList(); |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/MemoryFeatureCollection.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.data.feature; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.Collection; |
|
5 |
import java.util.Iterator; |
|
6 |
import java.util.List; |
|
3 |
import java.util.*; |
|
7 | 4 |
|
8 | 5 |
import org.gvsig.fmap.data.DataStore; |
9 | 6 |
import org.gvsig.fmap.data.ReadException; |
... | ... | |
14 | 11 |
public class MemoryFeatureCollection extends AbstractFeatureCollection |
15 | 12 |
implements Observable { |
16 | 13 |
|
17 |
protected Collection features = new ArrayList();//<FeatureID>
|
|
14 |
protected List features = new ArrayList();// <FeatureID>
|
|
18 | 15 |
protected boolean allowmodify = true; |
19 | 16 |
|
20 | 17 |
protected DefaultObservable observable = new DefaultObservable(); |
... | ... | |
251 | 248 |
return features.isEmpty(); |
252 | 249 |
} |
253 | 250 |
|
254 |
public Iterator iterator() { |
|
255 |
checkModified(); |
|
256 |
return new FeatureIteratorById(features.iterator(), this.featureType); |
|
257 |
} |
|
251 |
protected Iterator internalIterator(int index) { |
|
252 |
checkModified(); |
|
253 |
return new FeatureIteratorById(features.listIterator(index), |
|
254 |
this.featureType); |
|
255 |
} |
|
258 | 256 |
|
259 |
public Feature getFeature(int index) throws ReadException {
|
|
257 |
public Feature getFeature(int index) throws ReadException {
|
|
260 | 258 |
if (!(this.features instanceof List)) { |
261 | 259 |
ArrayList tmp = new ArrayList(); |
262 | 260 |
Iterator iter = this.features.iterator(); |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/AbstractFeatureCollection.java | ||
---|---|---|
53 | 53 |
// return null; |
54 | 54 |
} |
55 | 55 |
|
56 |
public Iterator iterator() { |
|
57 |
return internalIterator(0); |
|
58 |
} |
|
56 | 59 |
|
60 |
public Iterator iterator(int index) { |
|
61 |
if (index < 0 || index > size()) { |
|
62 |
throw new IndexOutOfBoundsException("The index (" + index |
|
63 |
+ ") is less than 0 or bigger than " |
|
64 |
+ "the size of the collection: " + size()); |
|
65 |
} |
|
66 |
|
|
67 |
return internalIterator(index); |
|
68 |
} |
|
57 | 69 |
|
70 |
/** |
|
71 |
* Child classes must implement the method to return an iterator starting at |
|
72 |
* the given index, but without the validation of the index. |
|
73 |
* |
|
74 |
* @see DataCollection#iterator(int). |
|
75 |
*/ |
|
76 |
protected abstract Iterator internalIterator(int index); |
|
77 |
|
|
58 | 78 |
} |
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/DataCollection.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.data; |
2 | 2 |
|
3 | 3 |
import java.util.Collection; |
4 |
import java.util.Iterator; |
|
4 | 5 |
|
5 | 6 |
import org.gvsig.tools.observer.Observer; |
6 | 7 |
|
... | ... | |
15 | 16 |
* |
16 | 17 |
*/ |
17 | 18 |
public interface DataCollection extends Collection, Observer{ |
18 |
public void dispose(); |
|
19 |
|
|
19 |
public void dispose(); |
|
20 | 20 |
|
21 |
public boolean isFromStore(DataStore store);
|
|
21 |
public boolean isFromStore(DataStore store);
|
|
22 | 22 |
|
23 |
/** |
|
24 |
* Returns an iterator over the elements in this collection, in the order |
|
25 |
* (if any) defined when the collection was obtained. |
|
26 |
* |
|
27 |
* The iterator starts at the specified position in this collection. The |
|
28 |
* specified index indicates the first element that would be returned by an |
|
29 |
* initial call to the <tt>next</tt> method. An initial call to the |
|
30 |
* <tt>previous</tt> method would return the element with the specified |
|
31 |
* index minus one. |
|
32 |
* |
|
33 |
* @param index |
|
34 |
* index of first element to be returned from the iterator (by a |
|
35 |
* call to the <tt>next</tt> method). |
|
36 |
* @return an iterator of the elements in this collection (in proper |
|
37 |
* sequence), starting at the specified position in the collection. |
|
38 |
* @throws IndexOutOfBoundsException |
|
39 |
* if the index is out of range (index < 0 || index > |
|
40 |
* size()). |
|
41 |
*/ |
|
42 |
Iterator iterator(int index); |
|
23 | 43 |
} |
branches/v2_0_0_prep/libraries/libFMap_data/pom.xml | ||
---|---|---|
35 | 35 |
<artifactId>libTools</artifactId> |
36 | 36 |
<version>2.0-SNAPSHOT</version> |
37 | 37 |
</dependency> |
38 |
<dependency> |
|
39 |
<groupId>easymock</groupId> |
|
40 |
<artifactId>easymock</artifactId> |
|
41 |
<version>1.2_Java1.3</version> |
|
42 |
</dependency> |
|
38 | 43 |
</dependencies> |
39 | 44 |
</project> |
branches/v2_0_0_prep/libraries/libFMap_data/.classpath | ||
---|---|---|
12 | 12 |
<classpathentry kind="lib" path="lib/org.cresques.cts.jar"/> |
13 | 13 |
<classpathentry combineaccessrules="false" kind="src" path="/libIverUtiles"/> |
14 | 14 |
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> |
15 |
<classpathentry kind="lib" path="lib/easymock-1.2_Java1.3.jar"/> |
|
15 | 16 |
<classpathentry kind="output" path="bin"/> |
16 | 17 |
</classpath> |
Also available in: Unified diff