You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/01/11 08:22:14 UTC

cvs commit: jakarta-commons/math/src/java/org/apache/commons/math/random EmpiricalDistributionImpl.java

psteitz     2004/01/10 23:22:14

  Modified:    math/src/java/org/apache/commons/math/random
                        EmpiricalDistributionImpl.java
  Log:
  Refactored load methods to eliminate use of URL.toFile(). Addressing PR #25972, reported by Bill Barker.
  
  Revision  Changes    Path
  1.12      +100 -71   jakarta-commons/math/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
  
  Index: EmpiricalDistributionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- EmpiricalDistributionImpl.java	19 Nov 2003 03:28:24 -0000	1.11
  +++ EmpiricalDistributionImpl.java	11 Jan 2004 07:22:14 -0000	1.12
  @@ -59,7 +59,10 @@
   import java.io.BufferedReader;
   import java.io.FileReader;
   import java.io.File;
  +import java.io.FileInputStream;
   import java.io.IOException;
  +import java.io.InputStreamReader;
  +import java.net.URL;
   
   import org.apache.commons.math.stat.DescriptiveStatistics;
   import org.apache.commons.math.stat.StorelessDescriptiveStatisticsImpl;
  @@ -128,42 +131,75 @@
           binStats = new ArrayList();
       }
       
  -    
       public void load(String filePath) throws IOException {
  -        File file = new File(filePath);
  -        load(file);
  +        BufferedReader in = 
  +            new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));  
  +        try {
  +            computeStats(in);
  +            in = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));  
  +            fillBinStats(in);
  +            loaded = true;
  +        } finally {
  +           if (in != null) try {in.close();} catch (Exception ex) {};
  +        }
       }
       
  +    public void load(URL url) throws IOException {
  +        BufferedReader in = 
  +            new BufferedReader(new InputStreamReader(url.openStream()));
  +        try {
  +            computeStats(in);
  +            in = new BufferedReader(new InputStreamReader(url.openStream()));
  +            fillBinStats(in);
  +            loaded = true;
  +        } finally {
  +           if (in != null) try {in.close();} catch (Exception ex) {};
  +        }
  +    }
        
       public void load(File file) throws IOException {
  -        // Pass the file once to get sample stats
  -         BufferedReader in = null;
  -         try {  
  +        BufferedReader in = new BufferedReader(new FileReader(file));
  +        try {
  +            computeStats(in);
               in = new BufferedReader(new FileReader(file));
  -            String str = null;
  -            double val = 0.0;
  -            sampleStats = new StorelessDescriptiveStatisticsImpl();
  -            while ((str = in.readLine()) != null) {
  -              val = new Double(str).doubleValue();
  -              sampleStats.addValue(val);   
  -            }
  -            in.close();
  -            in = null;
  -         } finally {
  -             if (in != null) try {in.close();} catch (Exception ex) {};
  -         }               
  +            fillBinStats(in);
  +            loaded = true;
  +        } finally {
  +           if (in != null) try {in.close();} catch (Exception ex) {};
  +        }
  +    }
  +    
  +    /**
  +     * Computes sampleStats (first pass through data file).
  +     */
  +    private void computeStats(BufferedReader in) throws IOException {
  +        String str = null;
  +        double val = 0.0;
  +        sampleStats = new StorelessDescriptiveStatisticsImpl();
  +        while ((str = in.readLine()) != null) {
  +            val = new Double(str).doubleValue();
  +            sampleStats.addValue(val);
  +        }
  +        in.close();
  +        in = null;
  +    }
  +    
  +    /**
  +     * Fills binStats array (second pass through data file).
  +     */
  +    private void fillBinStats(BufferedReader in) throws IOException {
  +        
  +        // Load array of bin upper bounds -- evenly spaced from min - max
  +        double min = sampleStats.getMin();
  +        double max = sampleStats.getMax();
  +        double delta = (max - min)/(new Double(binCount)).doubleValue();
  +        double[] binUpperBounds = new double[binCount];
  +        binUpperBounds[0] = min + delta;
  +        for (int i = 1; i< binCount - 1; i++) {
  +            binUpperBounds[i] = binUpperBounds[i-1] + delta;
  +        }
  +        binUpperBounds[binCount -1] = max;
           
  -         // Load array of bin upper bounds -- evenly spaced from min - max
  -         double min = sampleStats.getMin();
  -         double max = sampleStats.getMax();
  -         double delta = (max - min)/(new Double(binCount)).doubleValue();
  -         double[] binUpperBounds = new double[binCount];
  -         binUpperBounds[0] = min + delta;
  -         for (int i = 1; i< binCount - 1; i++) {
  -             binUpperBounds[i] = binUpperBounds[i-1] + delta;
  -         }
  -         binUpperBounds[binCount -1] = max;
  -         
           // Initialize binStats ArrayList
           if (!binStats.isEmpty()) {
               binStats.clear();
  @@ -172,49 +208,42 @@
               DescriptiveStatistics stats = new StorelessDescriptiveStatisticsImpl();
               binStats.add(i,stats);
           }
  -         
  -        // Pass the data again, filling data in binStats Array 
  -         try {
  -            in = new BufferedReader(new FileReader(file));
  -            String str = null;
  -            double val = 0.0d;
  -            while ((str = in.readLine()) != null) {
  -              val = new Double(str).doubleValue();
  -              
  -              // Find bin and add value to binStats for the bin
  -              boolean found = false;
  -              int i = 0; 
  -              while (!found) {
  -                  if (i >= binCount) {
  -                      throw new RuntimeException("bin alignment error");
  -                  }
  -                  if (val <= binUpperBounds[i]) {
  -                      found = true;
  -                      DescriptiveStatistics stats = (DescriptiveStatistics)binStats.get(i);
  -                      stats.addValue(val);
  -                  }
  -                  i++;
  -              }       
  +        
  +        // Pass the data again, filling data in binStats Array
  +        String str = null;
  +        double val = 0.0d;
  +        while ((str = in.readLine()) != null) {
  +            val = new Double(str).doubleValue();
  +            
  +            // Find bin and add value to binStats for the bin
  +            boolean found = false;
  +            int i = 0;
  +            while (!found) {
  +                if (i >= binCount) {
  +                    throw new RuntimeException("bin alignment error");
  +                }
  +                if (val <= binUpperBounds[i]) {
  +                    found = true;
  +                    DescriptiveStatistics stats = (DescriptiveStatistics)binStats.get(i);
  +                    stats.addValue(val);
  +                }
  +                i++;
               }
  -            in.close();
  -            in = null;
  -         } finally {
  -             if (in != null) try {in.close();} catch (Exception ex) {};
  -         }               
  +        }
  +        in.close();
  +        in = null;
           
  -         // Assign upperBounds based on bin counts
  -         upperBounds = new double[binCount];
  -         upperBounds[0] = 
  -            ((double)((DescriptiveStatistics)binStats.get(0)).getN())/
  -                (double)sampleStats.getN();
  -         for (int i = 1; i < binCount-1; i++) {
  -             upperBounds[i] = upperBounds[i-1] +
  -             ((double)((DescriptiveStatistics)binStats.get(i)).getN())/
  -                (double)sampleStats.getN();
  -         }
  -         upperBounds[binCount-1] = 1.0d;   
  -         
  -         loaded = true;
  +        // Assign upperBounds based on bin counts
  +        upperBounds = new double[binCount];
  +        upperBounds[0] =
  +        ((double)((DescriptiveStatistics)binStats.get(0)).getN())/
  +        (double)sampleStats.getN();
  +        for (int i = 1; i < binCount-1; i++) {
  +            upperBounds[i] = upperBounds[i-1] +
  +            ((double)((DescriptiveStatistics)binStats.get(i)).getN())/
  +            (double)sampleStats.getN();
  +        }
  +        upperBounds[binCount-1] = 1.0d;
       }
       
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org