You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2012/10/06 03:05:06 UTC
svn commit: r1394882 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
Author: psteitz
Date: Sat Oct 6 01:05:06 2012
New Revision: 1394882
URL: http://svn.apache.org/viewvc?rev=1394882&view=rev
Log:
Made file character set explicit and added file loading tests.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java?rev=1394882&r1=1394881&r2=1394882&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java Sat Oct 6 01:05:06 2012
@@ -19,11 +19,13 @@ package org.apache.commons.math3.random;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@@ -85,6 +87,9 @@ public class EmpiricalDistribution imple
/** Default bin count */
public static final int DEFAULT_BIN_COUNT = 1000;
+ /** Character set for file input */
+ private static final String FILE_CHARSET = "US-ASCII";
+
/** Serializable version identifier */
private static final long serialVersionUID = 5729073523949762654L;
@@ -204,6 +209,10 @@ public class EmpiricalDistribution imple
/**
* Computes the empirical distribution using data read from a URL.
+
+ * <p>The input file <i>must</i> be an ASCII text file containing one
+ * valid numeric entry per line.</p>
+ *
* @param url url of the input file
*
* @throws IOException if an IO error occurs
@@ -212,8 +221,9 @@ public class EmpiricalDistribution imple
*/
public void load(URL url) throws IOException, NullArgumentException, ZeroException {
MathUtils.checkNotNull(url);
+ Charset charset = Charset.forName(FILE_CHARSET);
BufferedReader in =
- new BufferedReader(new InputStreamReader(url.openStream()));
+ new BufferedReader(new InputStreamReader(url.openStream(), charset));
try {
DataAdapter da = new StreamDataAdapter(in);
da.computeStats();
@@ -221,7 +231,7 @@ public class EmpiricalDistribution imple
throw new ZeroException(LocalizedFormats.URL_CONTAINS_NO_DATA, url);
}
// new adapter for the second pass
- in = new BufferedReader(new InputStreamReader(url.openStream()));
+ in = new BufferedReader(new InputStreamReader(url.openStream(), charset));
fillBinStats(new StreamDataAdapter(in));
loaded = true;
} finally {
@@ -236,18 +246,24 @@ public class EmpiricalDistribution imple
/**
* Computes the empirical distribution from the input file.
*
+ * <p>The input file <i>must</i> be an ASCII text file containing one
+ * valid numeric entry per line.</p>
+ *
* @param file the input file
* @throws IOException if an IO error occurs
* @throws NullArgumentException if file is null
*/
public void load(File file) throws IOException, NullArgumentException {
MathUtils.checkNotNull(file);
- BufferedReader in = new BufferedReader(new FileReader(file));
+ Charset charset = Charset.forName(FILE_CHARSET);
+ InputStream is = new FileInputStream(file);
+ BufferedReader in = new BufferedReader(new InputStreamReader(is, charset));
try {
DataAdapter da = new StreamDataAdapter(in);
da.computeStats();
// new adapter for second pass
- in = new BufferedReader(new FileReader(file));
+ is = new FileInputStream(file);
+ in = new BufferedReader(new InputStreamReader(is, charset));
fillBinStats(new StreamDataAdapter(in));
loaded = true;
} finally {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java?rev=1394882&r1=1394881&r2=1394882&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java Sat Oct 6 01:05:06 2012
@@ -72,11 +72,21 @@ public final class EmpiricalDistribution
/**
* Test EmpiricalDistrbution.load() using sample data file.<br>
* Check that the sampleCount, mu and sigma match data in
- * the sample data file.
+ * the sample data file. Also verify that load is idempotent.
*/
@Test
public void testLoad() throws Exception {
+ // Load from a URL
empiricalDistribution.load(url);
+ checkDistribution();
+
+ // Load again from a file (also verifies idempotency of load)
+ File file = new File(url.getFile());
+ empiricalDistribution.load(file);
+ checkDistribution();
+ }
+
+ private void checkDistribution() {
// testData File has 10000 values, with mean ~ 5.0, std dev ~ 1
// Make sure that loaded distribution matches this
Assert.assertEquals(empiricalDistribution.getSampleStats().getN(),1000,10E-7);