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