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 getVariance() {
119        return super.getVariance();
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public synchronized double getPopulationVariance() {
127        return super.getPopulationVariance();
128    }
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public synchronized double getMax() {
135        return super.getMax();
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public synchronized double getMin() {
143        return super.getMin();
144    }
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public synchronized double getGeometricMean() {
151        return super.getGeometricMean();
152    }
153
154    /**
155     * {@inheritDoc}
156     */
157    @Override
158    public synchronized String toString() {
159        return super.toString();
160    }
161
162    /**
163     * {@inheritDoc}
164     */
165    @Override
166    public synchronized void clear() {
167        super.clear();
168    }
169
170    /**
171     * {@inheritDoc}
172     */
173    @Override
174    public synchronized boolean equals(Object object) {
175        return super.equals(object);
176    }
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public synchronized int hashCode() {
183        return super.hashCode();
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public synchronized StorelessUnivariateStatistic getSumImpl() {
191        return super.getSumImpl();
192    }
193
194    /**
195     * {@inheritDoc}
196     */
197    @Override
198    public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl)
199    throws MathIllegalStateException {
200        super.setSumImpl(sumImpl);
201    }
202
203    /**
204     * {@inheritDoc}
205     */
206    @Override
207    public synchronized StorelessUnivariateStatistic getSumsqImpl() {
208        return super.getSumsqImpl();
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl)
216    throws MathIllegalStateException {
217        super.setSumsqImpl(sumsqImpl);
218    }
219
220    /**
221     * {@inheritDoc}
222     */
223    @Override
224    public synchronized StorelessUnivariateStatistic getMinImpl() {
225        return super.getMinImpl();
226    }
227
228    /**
229     * {@inheritDoc}
230     */
231    @Override
232    public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl)
233    throws MathIllegalStateException {
234        super.setMinImpl(minImpl);
235    }
236
237    /**
238     * {@inheritDoc}
239     */
240    @Override
241    public synchronized StorelessUnivariateStatistic getMaxImpl() {
242        return super.getMaxImpl();
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @Override
249    public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl)
250    throws MathIllegalStateException {
251        super.setMaxImpl(maxImpl);
252    }
253
254    /**
255     * {@inheritDoc}
256     */
257    @Override
258    public synchronized StorelessUnivariateStatistic getSumLogImpl() {
259        return super.getSumLogImpl();
260    }
261
262    /**
263     * {@inheritDoc}
264     */
265    @Override
266    public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
267    throws MathIllegalStateException {
268        super.setSumLogImpl(sumLogImpl);
269    }
270
271    /**
272     * {@inheritDoc}
273     */
274    @Override
275    public synchronized StorelessUnivariateStatistic getGeoMeanImpl() {
276        return super.getGeoMeanImpl();
277    }
278
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl)
284    throws MathIllegalStateException {
285        super.setGeoMeanImpl(geoMeanImpl);
286    }
287
288    /**
289     * {@inheritDoc}
290     */
291    @Override
292    public synchronized StorelessUnivariateStatistic getMeanImpl() {
293        return super.getMeanImpl();
294    }
295
296    /**
297     * {@inheritDoc}
298     */
299    @Override
300    public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl)
301    throws MathIllegalStateException {
302        super.setMeanImpl(meanImpl);
303    }
304
305    /**
306     * {@inheritDoc}
307     */
308    @Override
309    public synchronized StorelessUnivariateStatistic getVarianceImpl() {
310        return super.getVarianceImpl();
311    }
312
313    /**
314     * {@inheritDoc}
315     */
316    @Override
317    public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl)
318    throws MathIllegalStateException {
319        super.setVarianceImpl(varianceImpl);
320    }
321
322    /**
323     * Returns a copy of this SynchronizedSummaryStatistics instance with the
324     * same internal state.
325     *
326     * @return a copy of this
327     */
328    @Override
329    public synchronized SynchronizedSummaryStatistics copy() {
330        SynchronizedSummaryStatistics result =
331            new SynchronizedSummaryStatistics();
332        // No try-catch or advertised exception because arguments are guaranteed non-null
333        copy(this, result);
334        return result;
335    }
336
337    /**
338     * Copies source to dest.
339     * <p>Neither source nor dest can be null.</p>
340     * <p>Acquires synchronization lock on source, then dest before copying.</p>
341     *
342     * @param source SynchronizedSummaryStatistics to copy
343     * @param dest SynchronizedSummaryStatistics to copy to
344     * @throws NullArgumentException if either source or dest is null
345     */
346    public static void copy(SynchronizedSummaryStatistics source,
347                            SynchronizedSummaryStatistics dest)
348        throws NullArgumentException {
349        MathUtils.checkNotNull(source);
350        MathUtils.checkNotNull(dest);
351        synchronized (source) {
352            synchronized (dest) {
353                SummaryStatistics.copy(source, dest);
354            }
355        }
356    }
357
358}