001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.stat.descriptive;
018
019import org.apache.commons.math3.exception.MathIllegalStateException;
020import org.apache.commons.math3.exception.NullArgumentException;
021import org.apache.commons.math3.util.MathUtils;
022
023/**
024 * Implementation of
025 * {@link org.apache.commons.math3.stat.descriptive.SummaryStatistics} that
026 * is safe to use in a multithreaded environment.  Multiple threads can safely
027 * operate on a single instance without causing runtime exceptions due to race
028 * conditions.  In effect, this implementation makes modification and access
029 * methods atomic operations for a single instance.  That is to say, as one
030 * thread is computing a statistic from the instance, no other thread can modify
031 * the instance nor compute another statistic.
032 *
033 * @since 1.2
034 */
035public class SynchronizedSummaryStatistics extends SummaryStatistics {
036
037    /** Serialization UID */
038    private static final long serialVersionUID = 1909861009042253704L;
039
040    /**
041     * Construct a SynchronizedSummaryStatistics instance
042     */
043    public SynchronizedSummaryStatistics() {
044        super();
045    }
046
047    /**
048     * A copy constructor. Creates a deep-copy of the {@code original}.
049     *
050     * @param original the {@code SynchronizedSummaryStatistics} instance to copy
051     * @throws NullArgumentException if original is null
052     */
053    public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original)
054    throws NullArgumentException {
055        copy(original, this);
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public synchronized StatisticalSummary getSummary() {
063        return super.getSummary();
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public synchronized void addValue(double value) {
071        super.addValue(value);
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public synchronized long getN() {
079        return super.getN();
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public synchronized double getSum() {
087        return super.getSum();
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public synchronized double getSumsq() {
095        return super.getSumsq();
096    }
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public synchronized double getMean() {
103        return super.getMean();
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public synchronized double getStandardDeviation() {
111        return super.getStandardDeviation();
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public synchronized double getQuadraticMean() {
119        return super.getQuadraticMean();
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public synchronized double getVariance() {
127        return super.getVariance();
128    }
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public synchronized double getPopulationVariance() {
135        return super.getPopulationVariance();
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public synchronized double getMax() {
143        return super.getMax();
144    }
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public synchronized double getMin() {
151        return super.getMin();
152    }
153
154    /**
155     * {@inheritDoc}
156     */
157    @Override
158    public synchronized double getGeometricMean() {
159        return super.getGeometricMean();
160    }
161
162    /**
163     * {@inheritDoc}
164     */
165    @Override
166    public synchronized String toString() {
167        return super.toString();
168    }
169
170    /**
171     * {@inheritDoc}
172     */
173    @Override
174    public synchronized void clear() {
175        super.clear();
176    }
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public synchronized boolean equals(Object object) {
183        return super.equals(object);
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public synchronized int hashCode() {
191        return super.hashCode();
192    }
193
194    /**
195     * {@inheritDoc}
196     */
197    @Override
198    public synchronized StorelessUnivariateStatistic getSumImpl() {
199        return super.getSumImpl();
200    }
201
202    /**
203     * {@inheritDoc}
204     */
205    @Override
206    public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl)
207    throws MathIllegalStateException {
208        super.setSumImpl(sumImpl);
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public synchronized StorelessUnivariateStatistic getSumsqImpl() {
216        return super.getSumsqImpl();
217    }
218
219    /**
220     * {@inheritDoc}
221     */
222    @Override
223    public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl)
224    throws MathIllegalStateException {
225        super.setSumsqImpl(sumsqImpl);
226    }
227
228    /**
229     * {@inheritDoc}
230     */
231    @Override
232    public synchronized StorelessUnivariateStatistic getMinImpl() {
233        return super.getMinImpl();
234    }
235
236    /**
237     * {@inheritDoc}
238     */
239    @Override
240    public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl)
241    throws MathIllegalStateException {
242        super.setMinImpl(minImpl);
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @Override
249    public synchronized StorelessUnivariateStatistic getMaxImpl() {
250        return super.getMaxImpl();
251    }
252
253    /**
254     * {@inheritDoc}
255     */
256    @Override
257    public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl)
258    throws MathIllegalStateException {
259        super.setMaxImpl(maxImpl);
260    }
261
262    /**
263     * {@inheritDoc}
264     */
265    @Override
266    public synchronized StorelessUnivariateStatistic getSumLogImpl() {
267        return super.getSumLogImpl();
268    }
269
270    /**
271     * {@inheritDoc}
272     */
273    @Override
274    public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
275    throws MathIllegalStateException {
276        super.setSumLogImpl(sumLogImpl);
277    }
278
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public synchronized StorelessUnivariateStatistic getGeoMeanImpl() {
284        return super.getGeoMeanImpl();
285    }
286
287    /**
288     * {@inheritDoc}
289     */
290    @Override
291    public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl)
292    throws MathIllegalStateException {
293        super.setGeoMeanImpl(geoMeanImpl);
294    }
295
296    /**
297     * {@inheritDoc}
298     */
299    @Override
300    public synchronized StorelessUnivariateStatistic getMeanImpl() {
301        return super.getMeanImpl();
302    }
303
304    /**
305     * {@inheritDoc}
306     */
307    @Override
308    public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl)
309    throws MathIllegalStateException {
310        super.setMeanImpl(meanImpl);
311    }
312
313    /**
314     * {@inheritDoc}
315     */
316    @Override
317    public synchronized StorelessUnivariateStatistic getVarianceImpl() {
318        return super.getVarianceImpl();
319    }
320
321    /**
322     * {@inheritDoc}
323     */
324    @Override
325    public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl)
326    throws MathIllegalStateException {
327        super.setVarianceImpl(varianceImpl);
328    }
329
330    /**
331     * Returns a copy of this SynchronizedSummaryStatistics instance with the
332     * same internal state.
333     *
334     * @return a copy of this
335     */
336    @Override
337    public synchronized SynchronizedSummaryStatistics copy() {
338        SynchronizedSummaryStatistics result =
339            new SynchronizedSummaryStatistics();
340        // No try-catch or advertised exception because arguments are guaranteed non-null
341        copy(this, result);
342        return result;
343    }
344
345    /**
346     * Copies source to dest.
347     * <p>Neither source nor dest can be null.</p>
348     * <p>Acquires synchronization lock on source, then dest before copying.</p>
349     *
350     * @param source SynchronizedSummaryStatistics to copy
351     * @param dest SynchronizedSummaryStatistics to copy to
352     * @throws NullArgumentException if either source or dest is null
353     */
354    public static void copy(SynchronizedSummaryStatistics source,
355                            SynchronizedSummaryStatistics dest)
356        throws NullArgumentException {
357        MathUtils.checkNotNull(source);
358        MathUtils.checkNotNull(dest);
359        synchronized (source) {
360            synchronized (dest) {
361                SummaryStatistics.copy(source, dest);
362            }
363        }
364    }
365
366}