[Schmitzm-commits] r458 - branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Oct 11 23:58:08 CEST 2009


Author: alfonx
Date: 2009-10-11 23:58:08 +0200 (Sun, 11 Oct 2009)
New Revision: 458

Modified:
   branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
Log:
* Closing the featureIterator

Modified: branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2009-10-11 21:52:47 UTC (rev 457)
+++ branches/1.0-gt2-2.6/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2009-10-11 21:58:08 UTC (rev 458)
@@ -76,8 +76,8 @@
  * @version 1.0
  */
 public class FeatureChartUtil {
-	  final static Logger LOGGER = Logger.getLogger(FeatureChartUtil.class);
-	
+	final static Logger LOGGER = Logger.getLogger(FeatureChartUtil.class);
+
 	/** Instance of {@link ChartStyleXMLFactory}. */
 	public static final FeatureChartStyleXMLFactory FEATURE_CHART_STYLE_FACTORY = new FeatureChartStyleXMLFactory();
 
@@ -88,9 +88,9 @@
 	 * @param chart
 	 *            a chart
 	 */
-	public static List<FeatureDatasetSelectionModel<?,?,?>> getFeatureDatasetSelectionModelFor(
+	public static List<FeatureDatasetSelectionModel<?, ?, ?>> getFeatureDatasetSelectionModelFor(
 			JFreeChart chart) {
-		ArrayList<FeatureDatasetSelectionModel<?,?,?>> renderers = new ArrayList<FeatureDatasetSelectionModel<?,?,?>>();
+		ArrayList<FeatureDatasetSelectionModel<?, ?, ?>> renderers = new ArrayList<FeatureDatasetSelectionModel<?, ?, ?>>();
 
 		Plot plot = chart.getPlot();
 
@@ -151,7 +151,8 @@
 	 *            feature attribute(s) used for the Y-value (at least one; for
 	 *            each a series is created in the dataset)
 	 */
-	public static Dataset createDataset(FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
+	public static Dataset createDataset(
+			FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
 			FeatureChartStyle style) {
 		String xAttr = style.getAttributeName(0);
 		// Check the X attribute to differ between XY- and CategoryDataset
@@ -181,8 +182,8 @@
 	 *            the type the attribute must support (if {@code null} only the
 	 *            attribute existing is checked!)
 	 */
-	private static void checkAttributeType(SimpleFeatureType fType, String aName,
-			Class typeToCheck, String... errDesc) {
+	private static void checkAttributeType(SimpleFeatureType fType,
+			String aName, Class typeToCheck, String... errDesc) {
 		AttributeDescriptor aDesc = fType.getDescriptor(aName);
 		if (aDesc == null)
 			throw new UnsupportedOperationException("Unknown attribute: "
@@ -213,7 +214,8 @@
 	 * @throws UnsupportedOperationException
 	 *             if attributes are not numeric
 	 */
-	public static XYSeriesCollection createXYDataset(FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
+	public static XYSeriesCollection createXYDataset(
+			FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
 			FeatureChartStyle chartStyle) {
 		int attrCount = chartStyle.getAttributeCount();
 		if (attrCount < 2)
@@ -244,10 +246,11 @@
 		dataset.setGroup(new FeatureDatasetMetaData(mapping));
 		for (int i = 1; i < xySeries.length; i++)
 			dataset.addSeries(xySeries[i]);
-		
-		// Calculate any statistics needed for normalization. 
-		HashMap<String,StaticBin1D> statisticsForNormalization = calcStatisticsForNormalization(fc, chartStyle);
 
+		// Calculate any statistics needed for normalization.
+		HashMap<String, StaticBin1D> statisticsForNormalization = calcStatisticsForNormalization(
+				fc, chartStyle);
+
 		// If dataset should be sorted, the features must be sorted first
 		// according to the domain attribute. The "autoSort" functionality
 		// of XYSeries can NOT BE USED because of the following reason:
@@ -261,12 +264,13 @@
 		// order does not change!
 		Iterator<SimpleFeature> fi = null;
 		if (chartStyle.isSortDomainAxis()) {
-			Vector<SimpleFeature> sortedFeatures = FeatureUtil.sortFeatures(fc.features(), xAttrName);
+			Vector<SimpleFeature> sortedFeatures = FeatureUtil.sortFeatures(fc,
+					xAttrName);
 			// Create an Iterator for the sorted Features
 			fi = sortedFeatures.iterator();
 		} else
 			fi = new PipedFeatureIterator(fc.features());
-		
+
 		// Iterate the FeatureCollection and fill the series
 		for (int fIdx = 0; fi.hasNext(); fIdx++) {
 			SimpleFeature f = fi.next();
@@ -281,18 +285,23 @@
 				// // TODO: here maybe filtering the several NULL aliases
 				// if ( yValue is a NULL alias )
 				// yValue = null;
-				
+
 				// // TODO: her maybe ignore NULL values completely
 				// if ( yValue == null )
 				// continue;
 
 				/* Normalization of a range axis value */
 				if (chartStyle.isAttributeNormalized(attrIdx))
-					yValue = normalize(yValue, yAttrName, statisticsForNormalization);
-				
-				/* Normalization of the domain axis value (if they are not handled as categories) */
+					yValue = normalize(yValue, yAttrName,
+							statisticsForNormalization);
+
+				/*
+				 * Normalization of the domain axis value (if they are not
+				 * handled as categories)
+				 */
 				if (chartStyle.isAttributeNormalized(ChartStyle.DOMAIN_AXIS))
-					xValue = normalize(xValue, xAttrName, statisticsForNormalization);
+					xValue = normalize(xValue, xAttrName,
+							statisticsForNormalization);
 
 				xySeries[attrIdx].add(xValue, yValue);
 
@@ -316,79 +325,92 @@
 	 *            {@link ChartStyle} to determine which attributes shall be
 	 *            normalized.
 	 */
-	public static HashMap<String,StaticBin1D> calcStatisticsForNormalization(FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
+	public static HashMap<String, StaticBin1D> calcStatisticsForNormalization(
+			FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
 			FeatureChartStyle chartStyle) {
 		// NORMALIZATION:
 		// We have to create a descriptive statistic of all items before we can
 		// transform the first one. So we iterate over all items before we do
 		// optional sorting and insertion.
-			/**
-			 * Holds the statistics needed to normalize the attribute values. Key =
-			 * attributeName
-			 */
-			HashMap<String,StaticBin1D> attribStats = new HashMap<String, StaticBin1D>();
+		/**
+		 * Holds the statistics needed to normalize the attribute values. Key =
+		 * attributeName
+		 */
+		HashMap<String, StaticBin1D> attribStats = new HashMap<String, StaticBin1D>();
 
+		// First check if any attribute needs normalization. If not we can
+		// skip the whole iteration
+		boolean doNormalization = false;
+		for (int attrIdx = 0; attrIdx < chartStyle.getAttributeCount(); attrIdx++) {
+			if (chartStyle.isAttributeNormalized(attrIdx)) {
+				doNormalization = true;
+				break;
+			}
+		}
 
-			// First check if any attribute needs normalization. If not we can
-			// skip the whole iteration
-			boolean doNormalization = false;
-			for (int attrIdx = 0; attrIdx < chartStyle.getAttributeCount(); attrIdx++) {
-				if (chartStyle.isAttributeNormalized(attrIdx)) {
-					doNormalization = true;
-					break;
-				}
-			}
-			
-			if (doNormalization) {
-				final FeatureIterator<SimpleFeature> fIt = fc.features();
-				// Loop over all features, and collect the attribute values we are interested in.
-				while (fIt.hasNext()){
+		if (doNormalization) {
+			final FeatureIterator<SimpleFeature> fIt = fc.features();
+			try {
+
+				// Loop over all features, and collect the attribute values we
+				// are interested in.
+				while (fIt.hasNext()) {
 					SimpleFeature f = fIt.next();
 
-					for (int attrIdx = 0; attrIdx < chartStyle.getAttributeCount(); attrIdx++) {
-	
-					
+					for (int attrIdx = 0; attrIdx < chartStyle
+							.getAttributeCount(); attrIdx++) {
+
 						if (chartStyle.isAttributeNormalized(attrIdx)) {
-							String attrName = chartStyle.getAttributeName(attrIdx);
+							String attrName = chartStyle
+									.getAttributeName(attrIdx);
 							StaticBin1D stat = attribStats.get(attrName);
-							if (stat == null){
+							if (stat == null) {
 								stat = new DynamicBin1D();
 								attribStats.put(attrName, stat);
 							}
-//							// TODO: here also filtering the several NULL aliases and do not
-//							// insert them into the statistics calculation
-//							// if ( yValue is a NULL alias )
-//							// yValue = null;
-							final Double doubleValue = new Double(((Number) f.getAttribute(attrName)).doubleValue());
-							stat.add(doubleValue);				
-//							LOGGER.debug("Adding "+doubleValue+" and sd = "+stat.standardDeviation()+" and mean = "+stat.mean());				
+							// // TODO: here also filtering the several NULL
+							// aliases and do not
+							// // insert them into the statistics calculation
+							// // if ( yValue is a NULL alias )
+							// // yValue = null;
+							final Double doubleValue = new Double(((Number) f
+									.getAttribute(attrName)).doubleValue());
+							stat.add(doubleValue);
+							// LOGGER.debug("Adding "+doubleValue+" and sd = "+stat.standardDeviation()+" and mean = "+stat.mean());
 						}
 					}
 				} // Featuer iterator
-			} // doNormlization
-			
-			return attribStats;
+			} finally {
+				fc.close(fIt);
+			}
+		} // doNormlization
+
+		return attribStats;
 	}
 
 	/**
-	 * Do a z-transformation on the data item. 
+	 * Do a z-transformation on the data item.
 	 * 
-	 * @param statisticsForNormalization The statistics needed for normalization.   
+	 * @param statisticsForNormalization
+	 *            The statistics needed for normalization.
 	 * 
-	 * @see #calcStatisticsForNormalization(FeatureCollection, FeatureChartStyle, int)
+	 * @see #calcStatisticsForNormalization(FeatureCollection,
+	 *      FeatureChartStyle, int)
 	 */
-	private static Number normalize(Number yValue, String attrName, HashMap<String,StaticBin1D> statisticsForNormalization) {
+	private static Number normalize(Number yValue, String attrName,
+			HashMap<String, StaticBin1D> statisticsForNormalization) {
 		Number zValue;
 
 		if (statisticsForNormalization.get(attrName) == null) {
-			throw new IllegalArgumentException("The statistics needed for the normlization need to be created before normalizing.");
+			throw new IllegalArgumentException(
+					"The statistics needed for the normlization need to be created before normalizing.");
 		}
 
 		StaticBin1D stats = statisticsForNormalization.get(attrName);
 
 		zValue = (yValue.doubleValue() - stats.mean())
 				/ stats.standardDeviation();
-		
+
 		return zValue;
 	}
 
@@ -407,70 +429,93 @@
 	 * @throws UnsupportedOperationException
 	 *             if attributes are not numeric
 	 */
-	public static DefaultCategoryDataset createCategoryDataset(FeatureCollection<SimpleFeatureType, SimpleFeature> fc, FeatureChartStyle chartStyle) {
+	public static DefaultCategoryDataset createCategoryDataset(
+			FeatureCollection<SimpleFeatureType, SimpleFeature> fc,
+			FeatureChartStyle chartStyle) {
 		int attrCount = chartStyle.getAttributeCount();
 		if (attrCount < 2)
-		  throw new IllegalArgumentException("FeatureChartStyle must define at least 2 attributes to create CategoryDataset: "+ attrCount);
+			throw new IllegalArgumentException(
+					"FeatureChartStyle must define at least 2 attributes to create CategoryDataset: "
+							+ attrCount);
 		String xAttrName = chartStyle.getAttributeName(0);
 		// only check whether X attribute exists (numeric and not-numeric
 		// allowed)
-		checkAttributeType(fc.getSchema(), xAttrName, Comparable.class,"Domain attribute", "CategoryDataset");
+		checkAttributeType(fc.getSchema(), xAttrName, Comparable.class,
+				"Domain attribute", "CategoryDataset");
 		// check data types of Y attribute (only numeric allowed)
 		for (int i = 1; i < attrCount; i++)
-		  checkAttributeType(fc.getSchema(), chartStyle.getAttributeName(i), Number.class, "Range attribute", "CategoryDataset");
-		
-		// Calculate any statistics needed for normalization. 
-		HashMap<String,StaticBin1D> statisticsForNormalization = calcStatisticsForNormalization(fc, chartStyle);
+			checkAttributeType(fc.getSchema(), chartStyle.getAttributeName(i),
+					Number.class, "Range attribute", "CategoryDataset");
 
+		// Calculate any statistics needed for normalization.
+		HashMap<String, StaticBin1D> statisticsForNormalization = calcStatisticsForNormalization(
+				fc, chartStyle);
+
 		// Create a new dataset and insert the series
-		DefaultCategoryDataset         dataset = new DefaultCategoryDataset();
-		Feature2CategoryDatasetMapping mapping = new Feature2CategoryDatasetMapping(fc,dataset);
-		dataset.setGroup( new FeatureDatasetMetaData(mapping) );
+		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
+		Feature2CategoryDatasetMapping mapping = new Feature2CategoryDatasetMapping(
+				fc, dataset);
+		dataset.setGroup(new FeatureDatasetMetaData(mapping));
 
 		// If dataset should be sorted, the features must be sorted first
 		// according to the domain attribute. CategoryDataset has no
 		// "autoSort", and this - at all - would not work (see
 		// createXYDataset(..))
 		Iterator<SimpleFeature> fi = null;
+		FeatureIterator<SimpleFeature> features = null;
 		if (chartStyle.isSortDomainAxis()) {
-		  Vector<SimpleFeature> sortedFeatures = FeatureUtil.sortFeatures(fc.features(), xAttrName);
-		  // Create an Iterator for the sorted Features
-		  fi = sortedFeatures.iterator();
-		} else
-		  fi = new PipedFeatureIterator(fc.features());
+			Vector<SimpleFeature> sortedFeatures = FeatureUtil.sortFeatures(fc,
+					xAttrName);
+			// Create an Iterator for the sorted Features
+			fi = sortedFeatures.iterator();
+		} else {
+			features = fc.features();
+			fi = new PipedFeatureIterator(features);
+		}
 
-		// Iterate the FeatureCollection and fill the series
-		for (int fIdx = 0; fi.hasNext(); fIdx++) {
-			SimpleFeature f = fi.next();
-			// Determine the category (NULL not permitted!)
-			Comparable<?> catValue = (Comparable<?>)f.getAttribute(xAttrName);
-			if (catValue == null)
-				continue;
-			// Determine the Y values and fill the dataset
-			for (int attrIdx = 1; attrIdx < attrCount; attrIdx++) {
-				String yAttrName = chartStyle.getAttributeName(attrIdx);
-				Number yValue    = (Number) f.getAttribute(yAttrName);
-	
-				// TODO: here maybe filtering the several NULL aliases
-				// if ( yValue is a NULL alias )
-				// yValue = null;
+		try {
 
-				// TODO: here maybe ignore NULL values completely.
-				// if ( yValue == null )
-				// continue;
+			// Iterate the FeatureCollection and fill the series
+			for (int fIdx = 0; fi.hasNext(); fIdx++) {
+				SimpleFeature f = fi.next();
+				// Determine the category (NULL not permitted!)
+				Comparable<?> catValue = (Comparable<?>) f
+						.getAttribute(xAttrName);
+				if (catValue == null)
+					continue;
+				// Determine the Y values and fill the dataset
+				for (int attrIdx = 1; attrIdx < attrCount; attrIdx++) {
+					String yAttrName = chartStyle.getAttributeName(attrIdx);
+					Number yValue = (Number) f.getAttribute(yAttrName);
 
-                /* Normalization of a range axis value */
-                if (chartStyle.isAttributeNormalized(attrIdx))
-                  yValue = normalize(yValue, yAttrName, statisticsForNormalization);
+					// TODO: here maybe filtering the several NULL aliases
+					// if ( yValue is a NULL alias )
+					// yValue = null;
 
-                // Add data to dataset
-				dataset.addValue(yValue, yAttrName, catValue);
+					// TODO: here maybe ignore NULL values completely.
+					// if ( yValue == null )
+					// continue;
 
-				// Mapping between FID and data index in series
-				mapping.setMapping(f.getID(), yAttrName, catValue);
+					/* Normalization of a range axis value */
+					if (chartStyle.isAttributeNormalized(attrIdx))
+						yValue = normalize(yValue, yAttrName,
+								statisticsForNormalization);
+
+					// Add data to dataset
+					dataset.addValue(yValue, yAttrName, catValue);
+
+					// Mapping between FID and data index in series
+					mapping.setMapping(f.getID(), yAttrName, catValue);
+				}
+
 			}
+		} finally {
+			if (features != null) {
+				// this is a hint
+				fc.close(features);
+			}
+		}
 
-		}
 		return dataset;
 	}
 }



More information about the Schmitzm-commits mailing list