added dimension checks

removed findbugs warnings about internal representation exposition

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@620001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-02-08 21:16:38 +00:00
parent 24763608bf
commit 1c27da8a98
3 changed files with 99 additions and 54 deletions

View File

@ -144,10 +144,7 @@ public class MultivariateSummaryStatistics
*/
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);
@ -402,13 +399,30 @@ public class MultivariateSummaryStatistics
}
// 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 @@ public class MultivariateSummaryStatistics
*
* @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 @@ public class MultivariateSummaryStatistics
* @return the StorelessUnivariateStatistic implementing the sum of squares
*/
public StorelessUnivariateStatistic[] getSumsqImpl() {
return sumSqImpl;
return (StorelessUnivariateStatistic[]) sumSqImpl.clone();
}
/**
@ -446,12 +460,14 @@ public class MultivariateSummaryStatistics
*
* @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 @@ public class MultivariateSummaryStatistics
* @return the StorelessUnivariateStatistic implementing the minimum
*/
public StorelessUnivariateStatistic[] getMinImpl() {
return minImpl;
return (StorelessUnivariateStatistic[]) minImpl.clone();
}
/**
@ -471,12 +487,14 @@ public class MultivariateSummaryStatistics
*
* @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 @@ public class MultivariateSummaryStatistics
* @return the StorelessUnivariateStatistic implementing the maximum
*/
public StorelessUnivariateStatistic[] getMaxImpl() {
return maxImpl;
return (StorelessUnivariateStatistic[]) maxImpl.clone();
}
/**
@ -496,12 +514,14 @@ public class MultivariateSummaryStatistics
*
* @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 @@ public class MultivariateSummaryStatistics
* @return the StorelessUnivariateStatistic implementing the log sum
*/
public StorelessUnivariateStatistic[] getSumLogImpl() {
return sumLogImpl;
return (StorelessUnivariateStatistic[]) sumLogImpl.clone();
}
/**
@ -521,12 +541,14 @@ public class MultivariateSummaryStatistics
*
* @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 @@ public class MultivariateSummaryStatistics
* @return the StorelessUnivariateStatistic implementing the geometric mean
*/
public StorelessUnivariateStatistic[] getGeoMeanImpl() {
return geoMeanImpl;
return (StorelessUnivariateStatistic[]) geoMeanImpl.clone();
}
/**
@ -546,12 +568,14 @@ public class MultivariateSummaryStatistics
*
* @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 @@ public class MultivariateSummaryStatistics
* @return the StorelessUnivariateStatistic implementing the mean
*/
public StorelessUnivariateStatistic[] getMeanImpl() {
return meanImpl;
return (StorelessUnivariateStatistic[]) meanImpl.clone();
}
/**
@ -571,12 +595,14 @@ public class MultivariateSummaryStatistics
*
* @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);
}
/**
@ -589,4 +615,16 @@ public class MultivariateSummaryStatistics
}
}
/**
* 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);
}
}
}

View File

@ -83,15 +83,15 @@ public class StatisticalMultivariateSummaryValues
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 @@ public class StatisticalMultivariateSummaryValues
* @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 @@ public class StatisticalMultivariateSummaryValues
* @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();
}
/**

View File

@ -178,7 +178,8 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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 @@ public class SynchronizedMultivariateSummaryStatistics
/**
* @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);
}