Revision 44158
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/JSIRSpatialIndexProviderFactory.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.feature.impl; |
|
2 |
|
|
3 |
import org.gvsig.fmap.dal.DataFactory; |
|
4 |
import org.gvsig.fmap.dal.DataFactoryUnit; |
|
5 |
import org.gvsig.fmap.dal.spi.AbstractDataFactory; |
|
6 |
import org.gvsig.tools.dynobject.DynObject; |
|
7 |
import org.gvsig.tools.service.spi.Services; |
|
8 |
|
|
9 |
public class JSIRSpatialIndexProviderFactory |
|
10 |
extends AbstractDataFactory |
|
11 |
implements DataFactory |
|
12 |
{ |
|
13 |
|
|
14 |
public JSIRSpatialIndexProviderFactory() { |
|
15 |
super(JSIRSpatialIndexProvider.NAME, ""); |
|
16 |
} |
|
17 |
|
|
18 |
@Override |
|
19 |
public DataFactoryUnit create(DynObject parameters, Services services) { |
|
20 |
return new JSIRSpatialIndexProvider(); |
|
21 |
} |
|
22 |
|
|
23 |
@Override |
|
24 |
public DynObject createParameters() { |
|
25 |
return null; |
|
26 |
} |
|
27 |
|
|
28 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/GeometryIndexProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl; |
|
25 |
|
|
26 |
import java.util.List; |
|
27 |
|
|
28 |
import org.gvsig.fmap.dal.exception.DataException; |
|
29 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
30 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
31 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
|
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
34 |
import java.util.Iterator; |
|
35 |
import org.apache.commons.collections4.IteratorUtils; |
|
36 |
import org.gvsig.fmap.geom.SpatialIndex; |
|
37 |
|
|
38 |
public class GeometryIndexProvider extends AbstractFeatureIndexProvider { |
|
39 |
|
|
40 |
private SpatialIndex index = null; |
|
41 |
|
|
42 |
public GeometryIndexProvider() { |
|
43 |
|
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public void initialize() { |
|
48 |
} |
|
49 |
|
|
50 |
protected Envelope getEnvelope(Object value) { |
|
51 |
Envelope env = null; |
|
52 |
|
|
53 |
if (value instanceof Envelope) { |
|
54 |
env = (Envelope) value; |
|
55 |
} else |
|
56 |
if (value instanceof Geometry) { |
|
57 |
env = ((Geometry) value).getEnvelope(); |
|
58 |
} |
|
59 |
return env; |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
64 |
this.index.insert(getEnvelope(value), fref); |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
69 |
this.index.remove(getEnvelope(value), fref); |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public List match(Object value) throws FeatureIndexException { |
|
74 |
return this.index.queryAsList(getEnvelope(value)); |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public List nearest(int count, Object value) { |
|
79 |
if( !this.index.getFactory().isNearestQuerySupported() ) { |
|
80 |
throw new UnsupportedOperationException(); |
|
81 |
} |
|
82 |
if (value == null) { |
|
83 |
throw new IllegalArgumentException("value is null"); |
|
84 |
} |
|
85 |
|
|
86 |
Iterator x = this.index.queryNearest(getEnvelope(value), count); |
|
87 |
return IteratorUtils.toList(x); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public boolean isMatchSupported() { |
|
92 |
return true; |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public boolean isNearestSupported() { |
|
97 |
return this.index.getFactory().isNearestQuerySupported(); |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public boolean isNearestToleranceSupported() { |
|
102 |
return false; |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public boolean isRangeSupported() { |
|
107 |
return false; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public List nearest(int count, Object value, Object tolerance) |
|
112 |
throws FeatureIndexException { |
|
113 |
throw new UnsupportedOperationException(); |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public List range(Object value1, Object value2) |
|
118 |
throws FeatureIndexException { |
|
119 |
throw new UnsupportedOperationException(); |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public void clear() throws DataException { |
|
124 |
this.index.removeAll(); |
|
125 |
} |
|
126 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/MemorySpatialIndexProviderFactory.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.fmap.dal.feature.impl; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.DataFactory; |
|
5 |
import org.gvsig.fmap.dal.DataFactoryUnit; |
|
6 |
import org.gvsig.fmap.dal.spi.AbstractDataFactory; |
|
7 |
import org.gvsig.tools.dynobject.DynObject; |
|
8 |
import org.gvsig.tools.service.spi.Services; |
|
9 |
|
|
10 |
|
|
11 |
public class MemorySpatialIndexProviderFactory |
|
12 |
extends AbstractDataFactory |
|
13 |
implements DataFactory |
|
14 |
{ |
|
15 |
|
|
16 |
public MemorySpatialIndexProviderFactory() { |
|
17 |
super(MemorySpatialIndexProvider.NAME, ""); |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
public DataFactoryUnit create(DynObject parameters, Services services) { |
|
22 |
return new MemorySpatialIndexProvider(); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public DynObject createParameters() { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/GeometryIndexProviderFactory.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.feature.impl; |
|
2 |
|
|
3 |
import org.gvsig.fmap.dal.DataFactory; |
|
4 |
import org.gvsig.fmap.dal.DataFactoryUnit; |
|
5 |
import org.gvsig.fmap.dal.spi.AbstractDataFactory; |
|
6 |
import org.gvsig.tools.dynobject.DynObject; |
|
7 |
import org.gvsig.tools.service.spi.Services; |
|
8 |
|
|
9 |
public class GeometryIndexProviderFactory |
|
10 |
extends AbstractDataFactory |
|
11 |
implements DataFactory |
|
12 |
{ |
|
13 |
|
|
14 |
private static final String NAME = "GeometryIndex"; |
|
15 |
|
|
16 |
public GeometryIndexProviderFactory() { |
|
17 |
super(NAME, ""); |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
public DataFactoryUnit create(DynObject parameters, Services services) { |
|
22 |
return new GeometryIndexProvider(); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public DynObject createParameters() { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/JSIRSpatialIndexProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl; |
|
25 |
|
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Properties; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.exception.DataException; |
|
31 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
32 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
33 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
34 |
import org.gvsig.fmap.geom.Geometry; |
|
35 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
36 |
import org.gvsig.fmap.geom.primitive.Point; |
|
37 |
|
|
38 |
import com.infomatiq.jsi.IntProcedure; |
|
39 |
import com.infomatiq.jsi.Rectangle; |
|
40 |
import com.infomatiq.jsi.rtree.RTree; |
|
41 |
|
|
42 |
public class JSIRSpatialIndexProvider extends AbstractFeatureIndexProvider { |
|
43 |
|
|
44 |
public static final String NAME = "JSIRSpatialIndexProvider"; |
|
45 |
|
|
46 |
private RTree rtree = null; |
|
47 |
|
|
48 |
class ListIntProcedure implements IntProcedure { |
|
49 |
|
|
50 |
List solution = new ArrayList(); |
|
51 |
|
|
52 |
public boolean execute(int arg0) { |
|
53 |
solution.add(new Integer(arg0)); |
|
54 |
return true; |
|
55 |
} |
|
56 |
|
|
57 |
public List getSolution() { |
|
58 |
return solution; |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
public JSIRSpatialIndexProvider() { |
|
63 |
|
|
64 |
} |
|
65 |
|
|
66 |
public void initialize() { |
|
67 |
try { |
|
68 |
this.rtree = createRTree(); |
|
69 |
} catch (Exception e) { |
|
70 |
throw new RuntimeException(); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
private RTree createRTree() { |
|
75 |
RTree rtree = new RTree(); |
|
76 |
Properties props = new Properties(); |
|
77 |
// props.setProperty("MaxNodeEntries", "500"); |
|
78 |
// props.setProperty("MinNodeEntries", "200"); |
|
79 |
rtree.init(props); |
|
80 |
return rtree; |
|
81 |
} |
|
82 |
|
|
83 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
84 |
Envelope env = getEnvelope(value); |
|
85 |
|
|
86 |
if (env == null) { |
|
87 |
throw new IllegalArgumentException( |
|
88 |
"value is neither Geometry or Envelope"); |
|
89 |
} |
|
90 |
|
|
91 |
Object oid = fref.getOID(); |
|
92 |
if (!isCompatibleOID(oid)) { |
|
93 |
throw new IllegalArgumentException("OID type not compatible: " |
|
94 |
+ oid.getClass().getName()); |
|
95 |
} |
|
96 |
|
|
97 |
rtree.add(toJsiRect(env), ((Number) oid).intValue()); |
|
98 |
} |
|
99 |
|
|
100 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
101 |
Envelope env = getEnvelope(value); |
|
102 |
|
|
103 |
if (env == null) { |
|
104 |
throw new IllegalArgumentException( |
|
105 |
"value is neither Geometry or Envelope"); |
|
106 |
} |
|
107 |
|
|
108 |
Object oid = fref.getOID(); |
|
109 |
if (!isCompatibleOID(oid)) { |
|
110 |
throw new IllegalArgumentException("OID type not compatible: " |
|
111 |
+ oid.getClass().getName()); |
|
112 |
} |
|
113 |
|
|
114 |
rtree.delete(toJsiRect(env), ((Number) oid).intValue()); |
|
115 |
} |
|
116 |
|
|
117 |
public List match(Object value) throws FeatureIndexException { |
|
118 |
Envelope env = getEnvelope(value); |
|
119 |
|
|
120 |
if (env == null) { |
|
121 |
throw new IllegalArgumentException( |
|
122 |
"value is neither Geometry or Envelope"); |
|
123 |
} |
|
124 |
ListIntProcedure solution = new ListIntProcedure(); |
|
125 |
rtree.intersects(toJsiRect(env), solution); |
|
126 |
return new LongList(solution.getSolution()); |
|
127 |
} |
|
128 |
|
|
129 |
public List match(Object min, Object max) { |
|
130 |
throw new UnsupportedOperationException( |
|
131 |
"Can't perform this kind of search."); |
|
132 |
} |
|
133 |
|
|
134 |
public List nearest(int count, Object value) { |
|
135 |
if (value == null) { |
|
136 |
throw new IllegalArgumentException("value is null"); |
|
137 |
} |
|
138 |
|
|
139 |
if (value instanceof Point) { |
|
140 |
Point p = (Point) value; |
|
141 |
com.infomatiq.jsi.Point jsiPoint = |
|
142 |
new com.infomatiq.jsi.Point((float) p.getDirectPosition() |
|
143 |
.getOrdinate(0), (float) p.getDirectPosition().getOrdinate( |
|
144 |
1)); |
|
145 |
return (List) rtree.nearest(jsiPoint, count); |
|
146 |
} else { |
|
147 |
Envelope env = getEnvelope(value); |
|
148 |
|
|
149 |
if (env == null) { |
|
150 |
throw new IllegalArgumentException( |
|
151 |
"value is neither Geometry or Envelope"); |
|
152 |
} |
|
153 |
return new LongList((List) rtree.nearest(toJsiRect(env), count)); |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
public boolean isMatchSupported() { |
|
158 |
return true; |
|
159 |
} |
|
160 |
|
|
161 |
public boolean isNearestSupported() { |
|
162 |
return true; |
|
163 |
} |
|
164 |
|
|
165 |
public boolean isNearestToleranceSupported() { |
|
166 |
return false; |
|
167 |
} |
|
168 |
|
|
169 |
public boolean isRangeSupported() { |
|
170 |
return false; |
|
171 |
} |
|
172 |
|
|
173 |
public List nearest(int count, Object value, Object tolerance) |
|
174 |
throws FeatureIndexException { |
|
175 |
throw new UnsupportedOperationException(); |
|
176 |
} |
|
177 |
|
|
178 |
public List range(Object value1, Object value2) |
|
179 |
throws FeatureIndexException { |
|
180 |
throw new UnsupportedOperationException(); |
|
181 |
} |
|
182 |
|
|
183 |
/** |
|
184 |
* Indicates whether the given OID's type is compatible |
|
185 |
* with this provider |
|
186 |
* |
|
187 |
* @param oid |
|
188 |
* |
|
189 |
* @return |
|
190 |
* true if this index provider supports the given oid type |
|
191 |
*/ |
|
192 |
private boolean isCompatibleOID(Object oid) { |
|
193 |
if (!(oid instanceof Number)) { |
|
194 |
return false; |
|
195 |
} |
|
196 |
|
|
197 |
long num = ((Number) oid).longValue(); |
|
198 |
|
|
199 |
if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) { |
|
200 |
return false; |
|
201 |
} |
|
202 |
|
|
203 |
return true; |
|
204 |
} |
|
205 |
|
|
206 |
protected Envelope getEnvelope(Object value) { |
|
207 |
Envelope env = null; |
|
208 |
|
|
209 |
if (value instanceof Envelope) { |
|
210 |
env = (Envelope) value; |
|
211 |
} else |
|
212 |
if (value instanceof Geometry) { |
|
213 |
env = ((Geometry) value).getEnvelope(); |
|
214 |
} |
|
215 |
return env; |
|
216 |
} |
|
217 |
|
|
218 |
protected Rectangle toJsiRect(Envelope env) { |
|
219 |
Point min = env.getLowerCorner(); |
|
220 |
Point max = env.getUpperCorner(); |
|
221 |
|
|
222 |
Rectangle jsiRect = |
|
223 |
new Rectangle((float) min.getX(), (float) min.getY(), |
|
224 |
(float) max.getX(), (float) max.getY()); |
|
225 |
return jsiRect; |
|
226 |
} |
|
227 |
|
|
228 |
public void clear() throws DataException { |
|
229 |
rtree = createRTree(); |
|
230 |
} |
|
231 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/MemorySpatialIndexProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl; |
|
25 |
|
|
26 |
import java.util.List; |
|
27 |
|
|
28 |
import org.gvsig.fmap.dal.exception.DataException; |
|
29 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
30 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
31 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
|
33 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
34 |
import org.gvsig.fmap.geom.SpatialIndex; |
|
35 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
36 |
|
|
37 |
public class MemorySpatialIndexProvider extends AbstractFeatureIndexProvider { |
|
38 |
|
|
39 |
public static final String NAME = "MemorySpatialIndexProvider"; |
|
40 |
|
|
41 |
private SpatialIndex index = null; |
|
42 |
|
|
43 |
public MemorySpatialIndexProvider() { |
|
44 |
|
|
45 |
} |
|
46 |
|
|
47 |
public void initialize() { |
|
48 |
try { |
|
49 |
this.index = GeometryLocator.getGeometryManager().createDefaultMemorySpatialIndex(); |
|
50 |
} catch (Exception e) { |
|
51 |
throw new RuntimeException(); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
public void delete(Object o, FeatureReferenceProviderServices fref) { |
|
56 |
Geometry geom = (Geometry) o; |
|
57 |
this.index.remove(geom, fref.getOID()); |
|
58 |
} |
|
59 |
|
|
60 |
public void insert(Object o, FeatureReferenceProviderServices fref) { |
|
61 |
if (o == null ) { |
|
62 |
return; |
|
63 |
} |
|
64 |
Geometry geom = (Geometry) o; |
|
65 |
this.index.insert(geom, fref.getOID()); |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
public List match(Object value) throws FeatureIndexException { |
|
70 |
Envelope env = null; |
|
71 |
if (value instanceof Envelope) { |
|
72 |
env = (Envelope) value; |
|
73 |
} else { |
|
74 |
if (value instanceof Geometry) { |
|
75 |
env = ((Geometry) value).getEnvelope(); |
|
76 |
} |
|
77 |
} |
|
78 |
return new LongList(this.index.queryAsList(env)); |
|
79 |
|
|
80 |
} |
|
81 |
|
|
82 |
public List match(Object min, Object max) { |
|
83 |
throw new UnsupportedOperationException( |
|
84 |
"Can't perform this kind of search."); |
|
85 |
} |
|
86 |
|
|
87 |
public List nearest(int count, Object value) throws FeatureIndexException { |
|
88 |
throw new UnsupportedOperationException( |
|
89 |
"Can't perform this kind of search."); |
|
90 |
} |
|
91 |
|
|
92 |
public boolean isMatchSupported() { |
|
93 |
return true; |
|
94 |
} |
|
95 |
|
|
96 |
public boolean isNearestSupported() { |
|
97 |
return false; |
|
98 |
} |
|
99 |
|
|
100 |
public boolean isNearestToleranceSupported() { |
|
101 |
return false; |
|
102 |
} |
|
103 |
|
|
104 |
public boolean isRangeSupported() { |
|
105 |
return false; |
|
106 |
} |
|
107 |
|
|
108 |
public List nearest(int count, Object value, Object tolerance) |
|
109 |
throws FeatureIndexException { |
|
110 |
throw new UnsupportedOperationException(); |
|
111 |
} |
|
112 |
|
|
113 |
public List range(Object value1, Object value2) |
|
114 |
throws FeatureIndexException { |
|
115 |
throw new UnsupportedOperationException(); |
|
116 |
} |
|
117 |
|
|
118 |
public void clear() throws DataException { |
|
119 |
this.index.removeAll(); |
|
120 |
} |
|
121 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DefaultFeatureAttributeDescriptor.java | ||
---|---|---|
1069 | 1069 |
} |
1070 | 1070 |
} |
1071 | 1071 |
|
1072 |
@Override |
|
1073 |
public String[] getRequiredFieldNames() { |
|
1074 |
FeatureAttributeEmulator emulator = this.getFeatureAttributeEmulator(); |
|
1075 |
if( emulator==null ) { |
|
1076 |
return null; |
|
1077 |
} |
|
1078 |
return emulator.getRequiredFieldNames(); |
|
1079 |
} |
|
1080 |
|
|
1072 | 1081 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/indexes/geometry/GeometryIndexProviderFactory.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.feature.impl.indexes.geometry; |
|
2 |
|
|
3 |
import org.gvsig.fmap.dal.DataFactory; |
|
4 |
import org.gvsig.fmap.dal.DataFactoryUnit; |
|
5 |
import org.gvsig.fmap.dal.spi.AbstractDataFactory; |
|
6 |
import org.gvsig.tools.dynobject.DynObject; |
|
7 |
import org.gvsig.tools.service.spi.Services; |
|
8 |
|
|
9 |
public class GeometryIndexProviderFactory |
|
10 |
extends AbstractDataFactory |
|
11 |
implements DataFactory |
|
12 |
{ |
|
13 |
|
|
14 |
private static final String NAME = "GeometryIndex"; |
|
15 |
|
|
16 |
public GeometryIndexProviderFactory() { |
|
17 |
super(NAME, ""); |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
public DataFactoryUnit create(DynObject parameters, Services services) { |
|
22 |
return new GeometryIndexProvider(); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public DynObject createParameters() { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/indexes/geometry/GeometryIndexProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl.indexes.geometry; |
|
25 |
|
|
26 |
import java.util.List; |
|
27 |
|
|
28 |
import org.gvsig.fmap.dal.exception.DataException; |
|
29 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
30 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
31 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
|
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
34 |
import java.util.Iterator; |
|
35 |
import org.apache.commons.collections4.IteratorUtils; |
|
36 |
import org.gvsig.fmap.geom.SpatialIndex; |
|
37 |
|
|
38 |
public class GeometryIndexProvider extends AbstractFeatureIndexProvider { |
|
39 |
|
|
40 |
private SpatialIndex index = null; |
|
41 |
|
|
42 |
public GeometryIndexProvider() { |
|
43 |
|
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public void initialize() { |
|
48 |
} |
|
49 |
|
|
50 |
protected Envelope getEnvelope(Object value) { |
|
51 |
Envelope env = null; |
|
52 |
|
|
53 |
if (value instanceof Envelope) { |
|
54 |
env = (Envelope) value; |
|
55 |
} else |
|
56 |
if (value instanceof Geometry) { |
|
57 |
env = ((Geometry) value).getEnvelope(); |
|
58 |
} |
|
59 |
return env; |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
64 |
this.index.insert(getEnvelope(value), fref); |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
69 |
this.index.remove(getEnvelope(value), fref); |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public List match(Object value) throws FeatureIndexException { |
|
74 |
return this.index.queryAsList(getEnvelope(value)); |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public List nearest(int count, Object value) { |
|
79 |
if( !this.index.getFactory().isNearestQuerySupported() ) { |
|
80 |
throw new UnsupportedOperationException(); |
|
81 |
} |
|
82 |
if (value == null) { |
|
83 |
throw new IllegalArgumentException("value is null"); |
|
84 |
} |
|
85 |
|
|
86 |
Iterator x = this.index.queryNearest(getEnvelope(value), count); |
|
87 |
return IteratorUtils.toList(x); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public boolean isMatchSupported() { |
|
92 |
return true; |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public boolean isNearestSupported() { |
|
97 |
return this.index.getFactory().isNearestQuerySupported(); |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public boolean isNearestToleranceSupported() { |
|
102 |
return false; |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public boolean isRangeSupported() { |
|
107 |
return false; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public List nearest(int count, Object value, Object tolerance) |
|
112 |
throws FeatureIndexException { |
|
113 |
throw new UnsupportedOperationException(); |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public List range(Object value1, Object value2) |
|
118 |
throws FeatureIndexException { |
|
119 |
throw new UnsupportedOperationException(); |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public void clear() throws DataException { |
|
124 |
this.index.removeAll(); |
|
125 |
} |
|
126 |
} |
|
0 | 127 |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/indexes/memorybasictypes/MemoryDoubleIndexProviderFactory.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.DALLocator; |
|
5 |
import org.gvsig.fmap.dal.DataFactory; |
|
6 |
import org.gvsig.fmap.dal.DataFactoryUnit; |
|
7 |
import org.gvsig.fmap.dal.DataTypes; |
|
8 |
import org.gvsig.fmap.dal.impl.DefaultDataManager; |
|
9 |
import org.gvsig.fmap.dal.spi.AbstractDataFactory; |
|
10 |
import org.gvsig.tools.dynobject.DynObject; |
|
11 |
import org.gvsig.tools.service.spi.Services; |
|
12 |
|
|
13 |
|
|
14 |
public class MemoryDoubleIndexProviderFactory |
|
15 |
extends AbstractDataFactory |
|
16 |
implements DataFactory |
|
17 |
{ |
|
18 |
public static final String NAME = "MemoryDoubleIndexProvider"; |
|
19 |
|
|
20 |
public MemoryDoubleIndexProviderFactory() { |
|
21 |
super(NAME, ""); |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
public DataFactoryUnit create(DynObject parameters, Services services) { |
|
26 |
return new MemoryBasicTypesIndexProvider<Double>(); |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public DynObject createParameters() { |
|
31 |
return null; |
|
32 |
} |
|
33 |
|
|
34 |
public static void selfRegister() { |
|
35 |
DefaultDataManager dataManager = (DefaultDataManager) DALLocator.getDataManager(); |
|
36 |
|
|
37 |
if( !dataManager.getFeatureIndexRegister().exits(NAME) ) { |
|
38 |
dataManager.getFeatureIndexRegister().register(new MemoryDoubleIndexProviderFactory()); |
|
39 |
dataManager.setDefaultFeatureIndexProviderName( |
|
40 |
DataTypes.DOUBLE, |
|
41 |
NAME |
|
42 |
); |
|
43 |
} |
|
44 |
} |
|
45 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/indexes/memorybasictypes/MemoryIntIndexProviderFactory.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.DALLocator; |
|
5 |
import org.gvsig.fmap.dal.DataFactory; |
|
6 |
import org.gvsig.fmap.dal.DataFactoryUnit; |
|
7 |
import org.gvsig.fmap.dal.DataTypes; |
|
8 |
import org.gvsig.fmap.dal.impl.DefaultDataManager; |
|
9 |
import org.gvsig.fmap.dal.spi.AbstractDataFactory; |
|
10 |
import org.gvsig.tools.dynobject.DynObject; |
|
11 |
import org.gvsig.tools.service.spi.Services; |
|
12 |
|
|
13 |
|
|
14 |
public class MemoryIntIndexProviderFactory |
|
15 |
extends AbstractDataFactory |
|
16 |
implements DataFactory |
|
17 |
{ |
|
18 |
public static final String NAME = "MemoryIntIndexProvider"; |
|
19 |
|
|
20 |
public MemoryIntIndexProviderFactory() { |
|
21 |
super(NAME, ""); |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
public DataFactoryUnit create(DynObject parameters, Services services) { |
|
26 |
return new MemoryBasicTypesIndexProvider<Integer>(); |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public DynObject createParameters() { |
|
31 |
return null; |
|
32 |
} |
|
33 |
|
|
34 |
public static void selfRegister() { |
|
35 |
DefaultDataManager dataManager = (DefaultDataManager) DALLocator.getDataManager(); |
|
36 |
|
|
37 |
if( !dataManager.getFeatureIndexRegister().exits(NAME) ) { |
|
38 |
dataManager.getFeatureIndexRegister().register(new MemoryIntIndexProviderFactory()); |
|
39 |
dataManager.setDefaultFeatureIndexProviderName( |
|
40 |
DataTypes.INT, |
|
41 |
NAME |
|
42 |
); |
|
43 |
} |
|
44 |
} |
|
45 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/indexes/memorybasictypes/ArrayListOfLong.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes; |
|
2 |
|
|
3 |
import java.util.AbstractList; |
|
4 |
import java.util.Arrays; |
|
5 |
import java.util.BitSet; |
|
6 |
import java.util.Collection; |
|
7 |
import java.util.Collections; |
|
8 |
import java.util.Comparator; |
|
9 |
import java.util.ConcurrentModificationException; |
|
10 |
import java.util.Iterator; |
|
11 |
import java.util.List; |
|
12 |
import java.util.ListIterator; |
|
13 |
import java.util.NoSuchElementException; |
|
14 |
import java.util.Objects; |
|
15 |
import java.util.RandomAccess; |
|
16 |
import java.util.Spliterator; |
|
17 |
import java.util.function.Consumer; |
|
18 |
import java.util.function.Predicate; |
|
19 |
import java.util.function.UnaryOperator; |
|
20 |
|
|
21 |
/** |
|
22 |
* |
|
23 |
* @author jjdelcerro |
|
24 |
*/ |
|
25 |
public class ArrayListOfLong extends AbstractList<Long> |
|
26 |
implements ListOfLong, RandomAccess, Cloneable { |
|
27 |
|
|
28 |
private static final long serialVersionUID = 8683452581122892189L; |
|
29 |
|
|
30 |
/** |
|
31 |
* Default initial capacity. |
|
32 |
*/ |
|
33 |
private static final int DEFAULT_CAPACITY = 10; |
|
34 |
|
|
35 |
/** |
|
36 |
* Shared empty array instance used for empty instances. |
|
37 |
*/ |
|
38 |
private static final long[] EMPTY_ELEMENTDATA = {}; |
|
39 |
|
|
40 |
/** |
|
41 |
* Shared empty array instance used for default sized empty instances. We |
|
42 |
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when |
|
43 |
* first element is added. |
|
44 |
*/ |
|
45 |
private static final long[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; |
|
46 |
|
|
47 |
/** |
|
48 |
* The array buffer into which the elements of the ArrayList are stored. |
|
49 |
* The capacity of the ArrayList is the length of this array buffer. Any |
|
50 |
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA |
|
51 |
* will be expanded to DEFAULT_CAPACITY when the first element is added. |
|
52 |
*/ |
|
53 |
transient long[] elementData; // non-private to simplify nested class access |
|
54 |
|
|
55 |
/** |
|
56 |
* The size of the ArrayList (the number of elements it contains). |
|
57 |
* |
|
58 |
* @serial |
|
59 |
*/ |
|
60 |
private int size; |
|
61 |
|
|
62 |
/** |
|
63 |
* Constructs an empty list with the specified initial capacity. |
|
64 |
* |
|
65 |
* @param initialCapacity the initial capacity of the list |
|
66 |
* @throws IllegalArgumentException if the specified initial capacity |
|
67 |
* is negative |
|
68 |
*/ |
|
69 |
public ArrayListOfLong(int initialCapacity) { |
|
70 |
if (initialCapacity > 0) { |
|
71 |
this.elementData = new long[initialCapacity]; |
|
72 |
} else if (initialCapacity == 0) { |
|
73 |
this.elementData = EMPTY_ELEMENTDATA; |
|
74 |
} else { |
|
75 |
throw new IllegalArgumentException("Illegal Capacity: "+ |
|
76 |
initialCapacity); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Constructs an empty list with an initial capacity of ten. |
|
82 |
*/ |
|
83 |
public ArrayListOfLong() { |
|
84 |
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Constructs a list containing the elements of the specified |
|
89 |
* collection, in the order they are returned by the collection's |
|
90 |
* iterator. |
|
91 |
* |
|
92 |
* @param c the collection whose elements are to be placed into this list |
|
93 |
* @throws NullPointerException if the specified collection is null |
|
94 |
*/ |
|
95 |
public ArrayListOfLong(Collection<? extends Long> c) { |
|
96 |
if ((size = c.size()) != 0) { |
|
97 |
this.grow(c.size()); |
|
98 |
int i=0; |
|
99 |
for (Long e : c) { |
|
100 |
this.elementData[i++] = e; |
|
101 |
} |
|
102 |
} else { |
|
103 |
// replace with empty array. |
|
104 |
this.elementData = EMPTY_ELEMENTDATA; |
|
105 |
} |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* Trims the capacity of this <tt>ArrayList</tt> instance to be the |
|
110 |
* list's current size. An application can use this operation to minimize |
|
111 |
* the storage of an <tt>ArrayList</tt> instance. |
|
112 |
*/ |
|
113 |
public void trimToSize() { |
|
114 |
modCount++; |
|
115 |
if (size < elementData.length) { |
|
116 |
elementData = (size == 0) |
|
117 |
? EMPTY_ELEMENTDATA |
|
118 |
: Arrays.copyOf(elementData, size); |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Increases the capacity of this <tt>ArrayList</tt> instance, if |
|
124 |
* necessary, to ensure that it can hold at least the number of elements |
|
125 |
* specified by the minimum capacity argument. |
|
126 |
* |
|
127 |
* @param minCapacity the desired minimum capacity |
|
128 |
*/ |
|
129 |
public void ensureCapacity(int minCapacity) { |
|
130 |
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) |
|
131 |
// any size if not default element table |
|
132 |
? 0 |
|
133 |
// larger than default for default empty table. It's already |
|
134 |
// supposed to be at default size. |
|
135 |
: DEFAULT_CAPACITY; |
|
136 |
|
|
137 |
if (minCapacity > minExpand) { |
|
138 |
ensureExplicitCapacity(minCapacity); |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
private static int calculateCapacity(long[] elementData, int minCapacity) { |
|
143 |
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { |
|
144 |
return Math.max(DEFAULT_CAPACITY, minCapacity); |
|
145 |
} |
|
146 |
return minCapacity; |
|
147 |
} |
|
148 |
|
|
149 |
private void ensureCapacityInternal(int minCapacity) { |
|
150 |
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); |
|
151 |
} |
|
152 |
|
|
153 |
private void ensureExplicitCapacity(int minCapacity) { |
|
154 |
modCount++; |
|
155 |
|
|
156 |
// overflow-conscious code |
|
157 |
if (minCapacity - elementData.length > 0) |
|
158 |
grow(minCapacity); |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* The maximum size of array to allocate. |
|
163 |
* Some VMs reserve some header words in an array. |
|
164 |
* Attempts to allocate larger arrays may result in |
|
165 |
* OutOfMemoryError: Requested array size exceeds VM limit |
|
166 |
*/ |
|
167 |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
|
168 |
|
|
169 |
/** |
|
170 |
* Increases the capacity to ensure that it can hold at least the |
|
171 |
* number of elements specified by the minimum capacity argument. |
|
172 |
* |
|
173 |
* @param minCapacity the desired minimum capacity |
|
174 |
*/ |
|
175 |
private void grow(int minCapacity) { |
|
176 |
// overflow-conscious code |
|
177 |
int oldCapacity = elementData.length; |
|
178 |
int newCapacity = oldCapacity + (oldCapacity >> 1); |
|
179 |
if (newCapacity - minCapacity < 0) |
|
180 |
newCapacity = minCapacity; |
|
181 |
if (newCapacity - MAX_ARRAY_SIZE > 0) |
|
182 |
newCapacity = hugeCapacity(minCapacity); |
|
183 |
// minCapacity is usually close to size, so this is a win: |
|
184 |
elementData = Arrays.copyOf(elementData, newCapacity); |
|
185 |
} |
|
186 |
|
|
187 |
private static int hugeCapacity(int minCapacity) { |
|
188 |
if (minCapacity < 0) // overflow |
|
189 |
throw new OutOfMemoryError(); |
|
190 |
return (minCapacity > MAX_ARRAY_SIZE) ? |
|
191 |
Integer.MAX_VALUE : |
|
192 |
MAX_ARRAY_SIZE; |
|
193 |
} |
|
194 |
|
|
195 |
/** |
|
196 |
* Returns the number of elements in this list. |
|
197 |
* |
|
198 |
* @return the number of elements in this list |
|
199 |
*/ |
|
200 |
public int size() { |
|
201 |
return size; |
|
202 |
} |
|
203 |
|
|
204 |
/** |
|
205 |
* Returns <tt>true</tt> if this list contains no elements. |
|
206 |
* |
|
207 |
* @return <tt>true</tt> if this list contains no elements |
|
208 |
*/ |
|
209 |
public boolean isEmpty() { |
|
210 |
return size == 0; |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* Returns <tt>true</tt> if this list contains the specified element. |
|
215 |
* More formally, returns <tt>true</tt> if and only if this list contains |
|
216 |
* at least one element <tt>e</tt> such that |
|
217 |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
|
218 |
* |
|
219 |
* @param o element whose presence in this list is to be tested |
|
220 |
* @return <tt>true</tt> if this list contains the specified element |
|
221 |
*/ |
|
222 |
public boolean contains(Object o) { |
|
223 |
return indexOf(o) >= 0; |
|
224 |
} |
|
225 |
|
|
226 |
/** |
|
227 |
* Returns the index of the first occurrence of the specified element |
|
228 |
* in this list, or -1 if this list does not contain the element. |
|
229 |
* More formally, returns the lowest index <tt>i</tt> such that |
|
230 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
|
231 |
* or -1 if there is no such index. |
|
232 |
*/ |
|
233 |
public int indexOf(Object o) { |
|
234 |
if (o == null) { |
|
235 |
return -1; |
|
236 |
} else { |
|
237 |
for (int i = 0; i < size; i++) |
|
238 |
if (o.equals(elementData[i])) |
|
239 |
return i; |
|
240 |
} |
|
241 |
return -1; |
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
245 |
* Returns the index of the last occurrence of the specified element |
|
246 |
* in this list, or -1 if this list does not contain the element. |
|
247 |
* More formally, returns the highest index <tt>i</tt> such that |
|
248 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
|
249 |
* or -1 if there is no such index. |
|
250 |
*/ |
|
251 |
public int lastIndexOf(Object o) { |
|
252 |
if (o == null) { |
|
253 |
return -1; |
|
254 |
} else { |
|
255 |
for (int i = size-1; i >= 0; i--) |
|
256 |
if (o.equals(elementData[i])) |
|
257 |
return i; |
|
258 |
} |
|
259 |
return -1; |
|
260 |
} |
|
261 |
|
|
262 |
/** |
|
263 |
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The |
|
264 |
* elements themselves are not copied.) |
|
265 |
* |
|
266 |
* @return a clone of this <tt>ArrayList</tt> instance |
|
267 |
*/ |
|
268 |
public Object clone() { |
|
269 |
try { |
|
270 |
ArrayListOfLong v = (ArrayListOfLong) super.clone(); |
|
271 |
v.elementData = Arrays.copyOf(elementData, size); |
|
272 |
v.modCount = 0; |
|
273 |
return v; |
|
274 |
} catch (CloneNotSupportedException e) { |
|
275 |
// this shouldn't happen, since we are Cloneable |
|
276 |
throw new InternalError(e); |
|
277 |
} |
|
278 |
} |
|
279 |
|
|
280 |
public Long[] toArray() { |
|
281 |
return toArray(null); |
|
282 |
} |
|
283 |
|
|
284 |
public Long[] toArray(Long[] a) { |
|
285 |
if (a==null || a.length < size) { |
|
286 |
a = new Long[size]; |
|
287 |
} |
|
288 |
for (int i = 0; i < size; i++) { |
|
289 |
a[i] = this.elementData[i]; |
|
290 |
} |
|
291 |
return a; |
|
292 |
} |
|
293 |
|
|
294 |
// Positional Access Operations |
|
295 |
|
|
296 |
@SuppressWarnings("unchecked") |
|
297 |
long elementData(int index) { |
|
298 |
return elementData[index]; |
|
299 |
} |
|
300 |
|
|
301 |
public Long get(int index) { |
|
302 |
return this.getLong(index); |
|
303 |
} |
|
304 |
|
|
305 |
public long getLong(int index) { |
|
306 |
rangeCheck(index); |
|
307 |
|
|
308 |
return elementData(index); |
|
309 |
} |
|
310 |
|
|
311 |
/** |
|
312 |
* Replaces the element at the specified position in this list with |
|
313 |
* the specified element. |
|
314 |
* |
|
315 |
* @param index index of the element to replace |
|
316 |
* @param element element to be stored at the specified position |
|
317 |
* @return the element previously at the specified position |
|
318 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
319 |
*/ |
|
320 |
public Long set(int index, Long element) { |
|
321 |
rangeCheck(index); |
|
322 |
|
|
323 |
Long oldValue = elementData(index); |
|
324 |
elementData[index] = element; |
|
325 |
return oldValue; |
|
326 |
} |
|
327 |
|
|
328 |
public boolean add(Long e) { |
|
329 |
return this.add((long)e); |
|
330 |
} |
|
331 |
|
|
332 |
public boolean add(long e) { |
|
333 |
ensureCapacityInternal(size + 1); // Increments modCount!! |
|
334 |
elementData[size++] = e; |
|
335 |
return true; |
|
336 |
} |
|
337 |
|
|
338 |
public void add(int index, Long element) { |
|
339 |
this.add(index, (long)element); |
|
340 |
} |
|
341 |
|
|
342 |
public void add(int index, long element) { |
|
343 |
rangeCheckForAdd(index); |
|
344 |
|
|
345 |
ensureCapacityInternal(size + 1); // Increments modCount!! |
|
346 |
System.arraycopy(elementData, index, elementData, index + 1, |
|
347 |
size - index); |
|
348 |
elementData[index] = element; |
|
349 |
size++; |
|
350 |
} |
|
351 |
|
|
352 |
public Long remove(int index) { |
|
353 |
rangeCheck(index); |
|
354 |
|
|
355 |
modCount++; |
|
356 |
Long oldValue = elementData(index); |
|
357 |
|
|
358 |
int numMoved = size - index - 1; |
|
359 |
if (numMoved > 0) |
|
360 |
System.arraycopy(elementData, index+1, elementData, index, |
|
361 |
numMoved); |
|
362 |
return oldValue; |
|
363 |
} |
|
364 |
|
|
365 |
public boolean remove(Object o) { |
|
366 |
if (o == null) { |
|
367 |
return true; |
|
368 |
} |
|
369 |
return this.removeLong((long)o); |
|
370 |
} |
|
371 |
|
|
372 |
public boolean removeLong(long o) { |
|
373 |
for (int index = 0; index < size; index++) |
|
374 |
if (o == elementData[index]) { |
|
375 |
this.remove(index); |
|
376 |
return true; |
|
377 |
} |
|
378 |
return false; |
|
379 |
} |
|
380 |
|
|
381 |
/* |
|
382 |
* Private remove method that skips bounds checking and does not |
|
383 |
* return the value removed. |
|
384 |
*/ |
|
385 |
private void fastRemove(int index) { |
|
386 |
modCount++; |
|
387 |
int numMoved = size - index - 1; |
|
388 |
if (numMoved > 0) |
|
389 |
System.arraycopy(elementData, index+1, elementData, index, |
|
390 |
numMoved); |
|
391 |
} |
|
392 |
|
|
393 |
/** |
|
394 |
* Removes all of the elements from this list. The list will |
|
395 |
* be empty after this call returns. |
|
396 |
*/ |
|
397 |
public void clear() { |
|
398 |
modCount++; |
|
399 |
size = 0; |
|
400 |
} |
|
401 |
|
|
402 |
/** |
|
403 |
* Appends all of the elements in the specified collection to the end of |
|
404 |
* this list, in the order that they are returned by the |
|
405 |
* specified collection's Iterator. The behavior of this operation is |
|
406 |
* undefined if the specified collection is modified while the operation |
|
407 |
* is in progress. (This implies that the behavior of this call is |
|
408 |
* undefined if the specified collection is this list, and this |
|
409 |
* list is nonempty.) |
|
410 |
* |
|
411 |
* @param c collection containing elements to be added to this list |
|
412 |
* @return <tt>true</tt> if this list changed as a result of the call |
|
413 |
* @throws NullPointerException if the specified collection is null |
|
414 |
*/ |
|
415 |
public boolean addAll(Collection<? extends Long> c) { |
|
416 |
Object[] a = c.toArray(); |
|
417 |
int numNew = a.length; |
|
418 |
ensureCapacityInternal(size + numNew); // Increments modCount |
|
419 |
System.arraycopy(a, 0, elementData, size, numNew); |
|
420 |
size += numNew; |
|
421 |
return numNew != 0; |
|
422 |
} |
|
423 |
|
|
424 |
/** |
|
425 |
* Inserts all of the elements in the specified collection into this |
|
426 |
* list, starting at the specified position. Shifts the element |
|
427 |
* currently at that position (if any) and any subsequent elements to |
|
428 |
* the right (increases their indices). The new elements will appear |
|
429 |
* in the list in the order that they are returned by the |
|
430 |
* specified collection's iterator. |
|
431 |
* |
|
432 |
* @param index index at which to insert the first element from the |
|
433 |
* specified collection |
|
434 |
* @param c collection containing elements to be added to this list |
|
435 |
* @return <tt>true</tt> if this list changed as a result of the call |
|
436 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
437 |
* @throws NullPointerException if the specified collection is null |
|
438 |
*/ |
|
439 |
public boolean addAll(int index, Collection<? extends Long> c) { |
|
440 |
rangeCheckForAdd(index); |
|
441 |
|
|
442 |
Object[] a = c.toArray(); |
|
443 |
int numNew = a.length; |
|
444 |
ensureCapacityInternal(size + numNew); // Increments modCount |
|
445 |
|
|
446 |
int numMoved = size - index; |
|
447 |
if (numMoved > 0) |
|
448 |
System.arraycopy(elementData, index, elementData, index + numNew, |
|
449 |
numMoved); |
|
450 |
|
|
451 |
System.arraycopy(a, 0, elementData, index, numNew); |
|
452 |
size += numNew; |
|
453 |
return numNew != 0; |
|
454 |
} |
|
455 |
|
|
456 |
/** |
|
457 |
* Removes from this list all of the elements whose index is between |
|
458 |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
|
459 |
* Shifts any succeeding elements to the left (reduces their index). |
|
460 |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
|
461 |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
|
462 |
* |
|
463 |
* @throws IndexOutOfBoundsException if {@code fromIndex} or |
|
464 |
* {@code toIndex} is out of range |
|
465 |
* ({@code fromIndex < 0 || |
|
466 |
* fromIndex >= size() || |
|
467 |
* toIndex > size() || |
|
468 |
* toIndex < fromIndex}) |
|
469 |
*/ |
|
470 |
protected void removeRange(int fromIndex, int toIndex) { |
|
471 |
modCount++; |
|
472 |
int numMoved = size - toIndex; |
|
473 |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
|
474 |
numMoved); |
|
475 |
int newSize = size - (toIndex-fromIndex); |
|
476 |
size = newSize; |
|
477 |
} |
|
478 |
|
|
479 |
/** |
|
480 |
* Checks if the given index is in range. If not, throws an appropriate |
|
481 |
* runtime exception. This method does *not* check if the index is |
|
482 |
* negative: It is always used immediately prior to an array access, |
|
483 |
* which throws an ArrayIndexOutOfBoundsException if index is negative. |
|
484 |
*/ |
|
485 |
private void rangeCheck(int index) { |
|
486 |
if (index >= size) |
|
487 |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
|
488 |
} |
|
489 |
|
|
490 |
/** |
|
491 |
* A version of rangeCheck used by add and addAll. |
|
492 |
*/ |
|
493 |
private void rangeCheckForAdd(int index) { |
|
494 |
if (index > size || index < 0) |
|
495 |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
|
496 |
} |
|
497 |
|
|
498 |
/** |
|
499 |
* Constructs an IndexOutOfBoundsException detail message. |
|
500 |
* Of the many possible refactorings of the error handling code, |
|
501 |
* this "outlining" performs best with both server and client VMs. |
|
502 |
*/ |
|
503 |
private String outOfBoundsMsg(int index) { |
|
504 |
return "Index: "+index+", Size: "+size; |
|
505 |
} |
|
506 |
|
|
507 |
/** |
|
508 |
* Removes from this list all of its elements that are contained in the |
|
509 |
* specified collection. |
|
510 |
* |
|
511 |
* @param c collection containing elements to be removed from this list |
|
512 |
* @return {@code true} if this list changed as a result of the call |
|
513 |
* @throws ClassCastException if the class of an element of this list |
|
514 |
* is incompatible with the specified collection |
|
515 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
516 |
* @throws NullPointerException if this list contains a null element and the |
|
517 |
* specified collection does not permit null elements |
|
518 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
|
519 |
* or if the specified collection is null |
|
520 |
* @see Collection#contains(Object) |
|
521 |
*/ |
|
522 |
public boolean removeAll(Collection<?> c) { |
|
523 |
Objects.requireNonNull(c); |
|
524 |
return batchRemove(c, false); |
|
525 |
} |
|
526 |
|
|
527 |
/** |
|
528 |
* Retains only the elements in this list that are contained in the |
|
529 |
* specified collection. In other words, removes from this list all |
|
530 |
* of its elements that are not contained in the specified collection. |
|
531 |
* |
|
532 |
* @param c collection containing elements to be retained in this list |
|
533 |
* @return {@code true} if this list changed as a result of the call |
|
534 |
* @throws ClassCastException if the class of an element of this list |
|
535 |
* is incompatible with the specified collection |
|
536 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
537 |
* @throws NullPointerException if this list contains a null element and the |
|
538 |
* specified collection does not permit null elements |
|
539 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
|
540 |
* or if the specified collection is null |
|
541 |
* @see Collection#contains(Object) |
|
542 |
*/ |
|
543 |
public boolean retainAll(Collection<?> c) { |
|
544 |
Objects.requireNonNull(c); |
|
545 |
return batchRemove(c, true); |
|
546 |
} |
|
547 |
|
|
548 |
private boolean batchRemove(Collection<?> c, boolean complement) { |
|
549 |
final long[] elementData = this.elementData; |
|
550 |
int r = 0, w = 0; |
|
551 |
boolean modified = false; |
|
552 |
try { |
|
553 |
for (; r < size; r++) |
|
554 |
if (c.contains(elementData[r]) == complement) |
|
555 |
elementData[w++] = elementData[r]; |
|
556 |
} finally { |
|
557 |
// Preserve behavioral compatibility with AbstractCollection, |
|
558 |
// even if c.contains() throws. |
|
559 |
if (r != size) { |
|
560 |
System.arraycopy(elementData, r, |
|
561 |
elementData, w, |
|
562 |
size - r); |
|
563 |
w += size - r; |
|
564 |
} |
|
565 |
if (w != size) { |
|
566 |
modCount += size - w; |
|
567 |
size = w; |
|
568 |
modified = true; |
|
569 |
} |
|
570 |
} |
|
571 |
return modified; |
|
572 |
} |
|
573 |
|
|
574 |
/** |
|
575 |
* Returns a list iterator over the elements in this list (in proper |
|
576 |
* sequence), starting at the specified position in the list. |
|
577 |
* The specified index indicates the first element that would be |
|
578 |
* returned by an initial call to {@link ListIterator#next next}. |
|
579 |
* An initial call to {@link ListIterator#previous previous} would |
|
580 |
* return the element with the specified index minus one. |
|
581 |
* |
|
582 |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
|
583 |
* |
|
584 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
585 |
*/ |
|
586 |
public ListIterator<Long> listIterator(int index) { |
|
587 |
if (index < 0 || index > size) |
|
588 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
589 |
return new ListItr(index); |
|
590 |
} |
|
591 |
|
|
592 |
/** |
|
593 |
* Returns a list iterator over the elements in this list (in proper |
|
594 |
* sequence). |
|
595 |
* |
|
596 |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
|
597 |
* |
|
598 |
* @see #listIterator(int) |
|
599 |
*/ |
|
600 |
public ListIterator<Long> listIterator() { |
|
601 |
return new ListItr(0); |
|
602 |
} |
|
603 |
|
|
604 |
/** |
|
605 |
* Returns an iterator over the elements in this list in proper sequence. |
|
606 |
* |
|
607 |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
|
608 |
* |
|
609 |
* @return an iterator over the elements in this list in proper sequence |
Also available in: Unified diff