commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r620001 - in /commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive: MultivariateSummaryStatistics.java StatisticalMultivariateSummaryValues.java SynchronizedMultivariateSummaryStatistics.java
Date Fri, 08 Feb 2008 21:16:40 GMT
Author: luc
Date: Fri Feb  8 13:16:38 2008
New Revision: 620001

URL: http://svn.apache.org/viewvc?rev=620001&view=rev
Log:
added dimension checks
removed findbugs warnings about internal representation exposition

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/StatisticalMultivariateSummaryValues.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/SynchronizedMultivariateSummaryStatistics.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java?rev=620001&r1=620000&r2=620001&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java
Fri Feb  8 13:16:38 2008
@@ -144,10 +144,7 @@
      */
     public void addValue(double[] value)
       throws DimensionMismatchException {
-        if (value.length != k) {
-            throw new DimensionMismatchException(value.length, k);
-        }
-
+        checkDimension(value.length);
         for (int i = 0; i < k; ++i) {
             double v = value[i];
             sumImpl[i].increment(v);
@@ -403,12 +400,29 @@
 
     // Getters and setters for statistics implementations
     /**
+     * Sets statistics implementations.
+     * @param newImpl new implementations for statistics
+     * @param oldImpl old implementations for statistics
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
+     * @throws IllegalStateException if data has already been added
+     *  (i.e if n > 0)
+     */
+    private void setImpl(StorelessUnivariateStatistic[] newImpl,
+                         StorelessUnivariateStatistic[] oldImpl)
+       throws DimensionMismatchException, IllegalStateException {
+        checkEmpty();
+        checkDimension(newImpl.length);
+        System.arraycopy(newImpl, 0, oldImpl, 0, newImpl.length);
+    }
+
+    /**
      * Returns the currently configured Sum implementation
      * 
      * @return the StorelessUnivariateStatistic implementing the sum
      */
     public StorelessUnivariateStatistic[] getSumImpl() {
-        return sumImpl;
+        return (StorelessUnivariateStatistic[]) sumImpl.clone();
     }
 
     /**
@@ -419,14 +433,14 @@
      * 
      * @param sumImpl the StorelessUnivariateStatistic instance to use
      * for computing the Sum
-     * @throws IllegalArgumentException if the array dimension
+     * @throws DimensionMismatchException if the array dimension
      * does not match the one used at construction
      * @throws IllegalStateException if data has already been added
      *  (i.e if n > 0)
      */
-    public void setSumImpl(StorelessUnivariateStatistic[] sumImpl) {
-        checkEmpty();
-        this.sumImpl = sumImpl;
+    public void setSumImpl(StorelessUnivariateStatistic[] sumImpl)
+      throws DimensionMismatchException {
+        setImpl(sumImpl, this.sumImpl);
     }
 
     /**
@@ -435,7 +449,7 @@
      * @return the StorelessUnivariateStatistic implementing the sum of squares
      */
     public StorelessUnivariateStatistic[] getSumsqImpl() {
-        return sumSqImpl;
+        return (StorelessUnivariateStatistic[]) sumSqImpl.clone();
     }
 
     /**
@@ -446,12 +460,14 @@
      * 
      * @param sumsqImpl the StorelessUnivariateStatistic instance to use
      * for computing the sum of squares
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
      * @throws IllegalStateException if data has already been added
      *  (i.e if n > 0)
      */
-    public void setSumsqImpl(StorelessUnivariateStatistic[] sumsqImpl) {
-        checkEmpty();
-        this.sumSqImpl = sumsqImpl;
+    public void setSumsqImpl(StorelessUnivariateStatistic[] sumsqImpl)
+      throws DimensionMismatchException {
+        setImpl(sumsqImpl, this.sumSqImpl);
     }
 
     /**
@@ -460,7 +476,7 @@
      * @return the StorelessUnivariateStatistic implementing the minimum
      */
     public StorelessUnivariateStatistic[] getMinImpl() {
-        return minImpl;
+        return (StorelessUnivariateStatistic[]) minImpl.clone();
     }
 
     /**
@@ -471,12 +487,14 @@
      * 
      * @param minImpl the StorelessUnivariateStatistic instance to use
      * for computing the minimum
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
      * @throws IllegalStateException if data has already been added
      *  (i.e if n > 0)
      */
-    public void setMinImpl(StorelessUnivariateStatistic[] minImpl) {
-        checkEmpty();
-        this.minImpl = minImpl;
+    public void setMinImpl(StorelessUnivariateStatistic[] minImpl)
+      throws DimensionMismatchException {
+        setImpl(minImpl, this.minImpl);
     }
 
     /**
@@ -485,7 +503,7 @@
      * @return the StorelessUnivariateStatistic implementing the maximum
      */
     public StorelessUnivariateStatistic[] getMaxImpl() {
-        return maxImpl;
+        return (StorelessUnivariateStatistic[]) maxImpl.clone();
     }
 
     /**
@@ -496,12 +514,14 @@
      * 
      * @param maxImpl the StorelessUnivariateStatistic instance to use
      * for computing the maximum
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
      * @throws IllegalStateException if data has already been added
      *  (i.e if n > 0)
      */
-    public void setMaxImpl(StorelessUnivariateStatistic[] maxImpl) {
-        checkEmpty();
-        this.maxImpl = maxImpl;
+    public void setMaxImpl(StorelessUnivariateStatistic[] maxImpl)
+      throws DimensionMismatchException {
+        setImpl(maxImpl, this.maxImpl);
     }
 
     /**
@@ -510,7 +530,7 @@
      * @return the StorelessUnivariateStatistic implementing the log sum
      */
     public StorelessUnivariateStatistic[] getSumLogImpl() {
-        return sumLogImpl;
+        return (StorelessUnivariateStatistic[]) sumLogImpl.clone();
     }
 
     /**
@@ -521,12 +541,14 @@
      * 
      * @param sumLogImpl the StorelessUnivariateStatistic instance to use
      * for computing the log sum
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
      * @throws IllegalStateException if data has already been added 
      *  (i.e if n > 0)
      */
-    public void setSumLogImpl(StorelessUnivariateStatistic[] sumLogImpl) {
-        checkEmpty();
-        this.sumLogImpl = sumLogImpl;
+    public void setSumLogImpl(StorelessUnivariateStatistic[] sumLogImpl)
+      throws DimensionMismatchException {
+        setImpl(sumLogImpl, this.sumLogImpl);
     }
 
     /**
@@ -535,7 +557,7 @@
      * @return the StorelessUnivariateStatistic implementing the geometric mean
      */
     public StorelessUnivariateStatistic[] getGeoMeanImpl() {
-        return geoMeanImpl;
+        return (StorelessUnivariateStatistic[]) geoMeanImpl.clone();
     }
 
     /**
@@ -546,12 +568,14 @@
      * 
      * @param geoMeanImpl the StorelessUnivariateStatistic instance to use
      * for computing the geometric mean
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
      * @throws IllegalStateException if data has already been added
      *  (i.e if n > 0)
      */
-    public void setGeoMeanImpl(StorelessUnivariateStatistic[] geoMeanImpl) {
-        checkEmpty();
-        this.geoMeanImpl = geoMeanImpl;
+    public void setGeoMeanImpl(StorelessUnivariateStatistic[] geoMeanImpl)
+      throws DimensionMismatchException {
+        setImpl(geoMeanImpl, this.geoMeanImpl);
     }
 
     /**
@@ -560,7 +584,7 @@
      * @return the StorelessUnivariateStatistic implementing the mean
      */
     public StorelessUnivariateStatistic[] getMeanImpl() {
-        return meanImpl;
+        return (StorelessUnivariateStatistic[]) meanImpl.clone();
     }
 
     /**
@@ -571,12 +595,14 @@
      * 
      * @param meanImpl the StorelessUnivariateStatistic instance to use
      * for computing the mean
+     * @throws DimensionMismatchException if the array dimension
+     * does not match the one used at construction
      * @throws IllegalStateException if data has already been added
      *  (i.e if n > 0)
      */
-    public void setMeanImpl(StorelessUnivariateStatistic[] meanImpl) {
-        checkEmpty();
-        this.meanImpl = meanImpl;
+    public void setMeanImpl(StorelessUnivariateStatistic[] meanImpl)
+      throws DimensionMismatchException {
+        setImpl(meanImpl, this.meanImpl);
     }
 
     /**
@@ -586,6 +612,18 @@
         if (n > 0) {
             throw new IllegalStateException(
                 "Implementations must be configured before values are added.");
+        }
+    }
+
+    /**
+     * Throws DimensionMismatchException if dimension != k.
+     * @param dimension dimension to check
+     * @throws DimensionMismatchException if dimension != k
+     */
+    private void checkDimension(int dimension)
+      throws DimensionMismatchException {
+        if (dimension != k) {
+            throw new DimensionMismatchException(dimension, k);
         }
     }
 

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/StatisticalMultivariateSummaryValues.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/StatisticalMultivariateSummaryValues.java?rev=620001&r1=620000&r2=620001&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/StatisticalMultivariateSummaryValues.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/StatisticalMultivariateSummaryValues.java
Fri Feb  8 13:16:38 2008
@@ -83,15 +83,15 @@
                                                 double[] sum, double[] sumSq, double[] sumLog)
{
         super();
         this.k = k;
-        this.mean = mean;
+        this.mean = (double[]) mean.clone();
         this.covariance = covariance;
-        this.stdev = stdev;
+        this.stdev = (double[]) stdev.clone();
         this.n = n;
-        this.max = max;
-        this.min = min;
-        this.sum = sum;
-        this.sumSq = sumSq;
-        this.sumLog = sumLog;
+        this.max = (double[]) max.clone();
+        this.min = (double[]) min.clone();
+        this.sum = (double[]) sum.clone();
+        this.sumSq = (double[]) sumSq.clone();
+        this.sumLog = (double[]) sumLog.clone();
     }
 
     /** 
@@ -106,21 +106,21 @@
      * @return Returns the max.
      */
     public double[] getMax() {
-        return max;
+        return (double[]) max.clone();
     }
 
     /**
      * @return Returns the mean.
      */
     public double[] getMean() {
-        return mean;
+        return (double[]) mean.clone();
     }
 
     /**
      * @return Returns the min.
      */
     public double[] getMin() {
-        return min;
+        return (double[]) min.clone();
     }
 
     /**
@@ -134,28 +134,28 @@
      * @return Returns the sum.
      */
     public double[] getSum() {
-        return sum;
+        return (double[]) sum.clone();
     }
     
     /**
      * @return Returns the sum of the squares.
      */
     public double[] getSumSq() {
-        return sumSq;
+        return (double[]) sumSq.clone();
     }
     
     /**
      * @return Returns the sum of the logarithms.
      */
     public double[] getSumLog() {
-        return sumLog;
+        return (double[]) sumLog.clone();
     }
     
     /**
      * @return Returns the standard deviation (roots of the diagonal elements)
      */
     public double[] getStandardDeviation() {
-        return stdev; 
+        return (double[]) stdev.clone(); 
     }
 
     /**

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/SynchronizedMultivariateSummaryStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/SynchronizedMultivariateSummaryStatistics.java?rev=620001&r1=620000&r2=620001&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/SynchronizedMultivariateSummaryStatistics.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/SynchronizedMultivariateSummaryStatistics.java
Fri Feb  8 13:16:38 2008
@@ -178,7 +178,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setSumImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setSumImpl(StorelessUnivariateStatistic[] sumImpl) {
+    public synchronized void setSumImpl(StorelessUnivariateStatistic[] sumImpl)
+      throws DimensionMismatchException {
         super.setSumImpl(sumImpl);
     }
 
@@ -192,7 +193,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setSumsqImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setSumsqImpl(StorelessUnivariateStatistic[] sumsqImpl) {
+    public synchronized void setSumsqImpl(StorelessUnivariateStatistic[] sumsqImpl)
+      throws DimensionMismatchException {
         super.setSumsqImpl(sumsqImpl);
     }
 
@@ -206,7 +208,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setMinImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setMinImpl(StorelessUnivariateStatistic[] minImpl) {
+    public synchronized void setMinImpl(StorelessUnivariateStatistic[] minImpl)
+      throws DimensionMismatchException {
         super.setMinImpl(minImpl);
     }
 
@@ -220,7 +223,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setMaxImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setMaxImpl(StorelessUnivariateStatistic[] maxImpl) {
+    public synchronized void setMaxImpl(StorelessUnivariateStatistic[] maxImpl)
+      throws DimensionMismatchException {
         super.setMaxImpl(maxImpl);
     }
 
@@ -234,7 +238,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setSumLogImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setSumLogImpl(StorelessUnivariateStatistic[] sumLogImpl) {
+    public synchronized void setSumLogImpl(StorelessUnivariateStatistic[] sumLogImpl)
+      throws DimensionMismatchException {
         super.setSumLogImpl(sumLogImpl);
     }
 
@@ -248,7 +253,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setGeoMeanImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic[] geoMeanImpl) {
+    public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic[] geoMeanImpl)
+      throws DimensionMismatchException {
         super.setGeoMeanImpl(geoMeanImpl);
     }
 
@@ -262,7 +268,8 @@
     /**
      * @see org.apache.commons.math.stat.descriptive.MultivariateSummary#setMeanImpl(StorelessUnivariateStatistic[])
      */
-    public synchronized void setMeanImpl(StorelessUnivariateStatistic[] meanImpl) {
+    public synchronized void setMeanImpl(StorelessUnivariateStatistic[] meanImpl)
+      throws DimensionMismatchException {
         super.setMeanImpl(meanImpl);
     }
 



Mime
View raw message