Details
Description
I have had the pleasure of running through the geotools training material a couple times in the last month; and I have watched each person get stuck in the same spot.
Generics.
Generics in the Csv2Shape.java class.
public static void main(String[] args) throws Exception { ... String typeName = newDataStore.getTypeNames()[0]; FeatureSource<SimpleFeatureType,SimpleFeature> featureSource = (<SimpleFeatureType,SimpleFeature>) newDataStore.getFeatureSource(typeName); if (featureSource instanceof FeatureStore<?,?>) { FeatureStore<SimpleFeatureType,SimpleFeature> featureStore = (FeatureStore<SimpleFeatureType,SimpleFeature>) featureSource; ... } } else { System.out.println(typeName + " does not support read/write access"); System.exit(1); } }
For now the solution (committed) is to use:
@SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { ... String typeName = newDataStore.getTypeNames()[0]; FeatureSource featureSource = newDataStore.getFeatureSource(typeName); if (featureSource instanceof FeatureStore) { FeatureStore featureStore = (FeatureStore) featureSource; ... } } else { System.out.println(typeName + " does not support read/write access"); System.exit(1); } }
In addition to being magic to beginning Java users generics make the lines so long it no longer looks like a simple assignment; and by the time they have read word for word through the cast to feature store they need to start again to see what happened.
Previous attempts using FeatureSource<?,?> were even more scary; they did not even try reading the code.
So here is the minimal idea for the Csv2SShape example:
- no new methods
- SimpleFeatureCollection extends FeatureCollection<SimpleFeatureType,SimpleFeature>
- SimpleFeatureSource and SimpleFeatureStore once again with no new API; just returning SimpleFeatureCollection
This task is done when:
- casts are no longer needed example
I am prepared for this to be a change only on trunk; but I would need some assurances I will have a release of trunk for FOSS4G this year.
Update
Here is what the change would look like:
public static void main(String[] args) throws Exception { ... String typeName = newDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStorefeatureStore = (SimpleFeatureStore) featureSource; ... } } else { System.out.println(typeName + " does not support read/write access"); System.exit(1); } }