From 70e3baa85f744fe1db93bd7bf683ff51dd17c831 Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Wed, 31 Oct 2007 05:41:18 +0000 Subject: [PATCH] MATH-170. added SynchronizedDescriptiveStatistics class. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@590566 13f79535-47bb-0310-9956-ffa450edef68 --- .../SynchronizedDescriptiveStatistics.java | 129 ++++++++++++++++++ ...SynchronizedDescriptiveStatisticsTest.java | 39 ++++++ 2 files changed, 168 insertions(+) create mode 100644 src/java/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatistics.java create mode 100644 src/test/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatisticsTest.java diff --git a/src/java/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatistics.java b/src/java/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatistics.java new file mode 100644 index 000000000..fee0f72ab --- /dev/null +++ b/src/java/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatistics.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.stat.descriptive; + +/** + * Implementation of + * {@link org.apache.commons.math.stat.descriptive.DescriptiveStatistics} that + * is safe to use in a mulithreaded environment. Multiple threads can safely + * operate on a single instance without causing runtime exceptions due to race + * conditions. In effect, this implementation makes modification and access + * methods atomic operations for a signle instance. That is to say, as one + * thread is computing a statistic from the instance, no other thread can modify + * the instance nor compute another statistic. + * + * @version $Revision: 480440 $ $Date: 2006-11-29 01:14:12 -0600 (Wed, 29 Nov 2006) $ + */ +public class SynchronizedDescriptiveStatistics extends DescriptiveStatisticsImpl { + + /** Serialization UID */ + private static final long serialVersionUID = 1L; + + /** + * Construct an instance with infinite window + */ + public SynchronizedDescriptiveStatistics() { + this(INFINITE_WINDOW); + } + + /** + * Construct an instance with finite window + * @param window the finite window size. + */ + public SynchronizedDescriptiveStatistics(int window) { + super(window); + } + + /** + * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#addValue(double) + */ + public synchronized void addValue(double v) { + super.addValue(v); + } + + /** + * Apply the given statistic to this univariate collection. + * @param stat the statistic to apply + * @return the computed value of the statistic. + */ + public synchronized double apply(UnivariateStatistic stat) { + return super.apply(stat); + } + + /** + * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#clear() + */ + public synchronized void clear() { + super.clear(); + } + + /** + * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getElement(int) + */ + public synchronized double getElement(int index) { + return super.getElement(index); + } + + /** + * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getN() + */ + public synchronized long getN() { + return super.getN(); + } + + /** + * Returns the standard deviation of the available values. + * @return The standard deviation, Double.NaN if no values have been added + * or 0.0 for a single value set. + */ + public synchronized double getStandardDeviation() { + return super.getStandardDeviation(); + } + + /** + * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getValues() + */ + public synchronized double[] getValues() { + return super.getValues(); + } + + /** + * Access the window size. + * @return the current window size. + */ + public synchronized int getWindowSize() { + return super.getWindowSize(); + } + + /** + * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#setWindowSize(int) + */ + public synchronized void setWindowSize(int windowSize) { + super.setWindowSize(windowSize); + } + + /** + * Generates a text report displaying univariate statistics from values + * that have been added. Each statistic is displayed on a separate + * line. + * + * @return String with line feeds displaying statistics + */ + public synchronized String toString() { + return super.toString(); + } +} diff --git a/src/test/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatisticsTest.java b/src/test/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatisticsTest.java new file mode 100644 index 000000000..3083c7487 --- /dev/null +++ b/src/test/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatisticsTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law + * or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.apache.commons.math.stat.descriptive; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test cases for the {@link SynchronizedDescriptiveStatisticsTest} class. + * @version $Revision: 566833 $ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug + * 2007) $ + */ +public final class SynchronizedDescriptiveStatisticsTest extends DescriptiveStatisticsTest { + + public SynchronizedDescriptiveStatisticsTest(String name) { + super(name); + } + + public static Test suite() { + TestSuite suite = new TestSuite(SynchronizedDescriptiveStatisticsTest.class); + suite.setName("SynchronizedDescriptiveStatistics Tests"); + return suite; + } + + protected DescriptiveStatistics createDescriptiveStatistics() { + return new SynchronizedDescriptiveStatistics(); + } +}