1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.commons.math.stat.descriptive;
18
19/**
20 * Implementation of
21 * {@link org.apache.commons.math.stat.descriptive.SummaryStatistics} that
22 * is safe to use in a multithreaded environment.  Multiple threads can safely
23 * operate on a single instance without causing runtime exceptions due to race
24 * conditions.  In effect, this implementation makes modification and access
25 * methods atomic operations for a single instance.  That is to say, as one
26 * thread is computing a statistic from the instance, no other thread can modify
27 * the instance nor compute another statistic.
28 *
29 * @since 1.2
30 * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $
31 */
32public class SynchronizedSummaryStatistics extends SummaryStatistics {
33
34    /** Serialization UID */
35    private static final long serialVersionUID = 1909861009042253704L;
36
37    /**
38     * Construct a SynchronizedSummaryStatistics instance
39     */
40    public SynchronizedSummaryStatistics() {
41        super();
42    }
43
44    /**
45     * A copy constructor. Creates a deep-copy of the {@code original}.
46     *
47     * @param original the {@code SynchronizedSummaryStatistics} instance to copy
48     */
49    public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original) {
50        copy(original, this);
51    }
52
53    /**
54     * {@inheritDoc}
55     */
56    @Override
57    public synchronized StatisticalSummary getSummary() {
58        return super.getSummary();
59    }
60
61    /**
62     * {@inheritDoc}
63     */
64    @Override
65    public synchronized void addValue(double value) {
66        super.addValue(value);
67    }
68
69    /**
70     * {@inheritDoc}
71     */
72    @Override
73    public synchronized long getN() {
74        return super.getN();
75    }
76
77    /**
78     * {@inheritDoc}
79     */
80    @Override
81    public synchronized double getSum() {
82        return super.getSum();
83    }
84
85    /**
86     * {@inheritDoc}
87     */
88    @Override
89    public synchronized double getSumsq() {
90        return super.getSumsq();
91    }
92
93    /**
94     * {@inheritDoc}
95     */
96    @Override
97    public synchronized double getMean() {
98        return super.getMean();
99    }
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 getVariance() {
114        return super.getVariance();
115    }
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public synchronized double getMax() {
122        return super.getMax();
123    }
124
125    /**
126     * {@inheritDoc}
127     */
128    @Override
129    public synchronized double getMin() {
130        return super.getMin();
131    }
132
133    /**
134     * {@inheritDoc}
135     */
136    @Override
137    public synchronized double getGeometricMean() {
138        return super.getGeometricMean();
139    }
140
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    public synchronized String toString() {
146        return super.toString();
147    }
148
149    /**
150     * {@inheritDoc}
151     */
152    @Override
153    public synchronized void clear() {
154        super.clear();
155    }
156
157    /**
158     * {@inheritDoc}
159     */
160    @Override
161    public synchronized boolean equals(Object object) {
162        return super.equals(object);
163    }
164
165    /**
166     * {@inheritDoc}
167     */
168    @Override
169    public synchronized int hashCode() {
170        return super.hashCode();
171    }
172
173    /**
174     * {@inheritDoc}
175     */
176    @Override
177    public synchronized StorelessUnivariateStatistic getSumImpl() {
178        return super.getSumImpl();
179    }
180
181    /**
182     * {@inheritDoc}
183     */
184    @Override
185    public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl) {
186        super.setSumImpl(sumImpl);
187    }
188
189    /**
190     * {@inheritDoc}
191     */
192    @Override
193    public synchronized StorelessUnivariateStatistic getSumsqImpl() {
194        return super.getSumsqImpl();
195    }
196
197    /**
198     * {@inheritDoc}
199     */
200    @Override
201    public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl) {
202        super.setSumsqImpl(sumsqImpl);
203    }
204
205    /**
206     * {@inheritDoc}
207     */
208    @Override
209    public synchronized StorelessUnivariateStatistic getMinImpl() {
210        return super.getMinImpl();
211    }
212
213    /**
214     * {@inheritDoc}
215     */
216    @Override
217    public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl) {
218        super.setMinImpl(minImpl);
219    }
220
221    /**
222     * {@inheritDoc}
223     */
224    @Override
225    public synchronized StorelessUnivariateStatistic getMaxImpl() {
226        return super.getMaxImpl();
227    }
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl) {
234        super.setMaxImpl(maxImpl);
235    }
236
237    /**
238     * {@inheritDoc}
239     */
240    @Override
241    public synchronized StorelessUnivariateStatistic getSumLogImpl() {
242        return super.getSumLogImpl();
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @Override
249    public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) {
250        super.setSumLogImpl(sumLogImpl);
251    }
252
253    /**
254     * {@inheritDoc}
255     */
256    @Override
257    public synchronized StorelessUnivariateStatistic getGeoMeanImpl() {
258        return super.getGeoMeanImpl();
259    }
260
261    /**
262     * {@inheritDoc}
263     */
264    @Override
265    public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl) {
266        super.setGeoMeanImpl(geoMeanImpl);
267    }
268
269    /**
270     * {@inheritDoc}
271     */
272    @Override
273    public synchronized StorelessUnivariateStatistic getMeanImpl() {
274        return super.getMeanImpl();
275    }
276
277    /**
278     * {@inheritDoc}
279     */
280    @Override
281    public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl) {
282        super.setMeanImpl(meanImpl);
283    }
284
285    /**
286     * {@inheritDoc}
287     */
288    @Override
289    public synchronized StorelessUnivariateStatistic getVarianceImpl() {
290        return super.getVarianceImpl();
291    }
292
293    /**
294     * {@inheritDoc}
295     */
296    @Override
297    public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl) {
298        super.setVarianceImpl(varianceImpl);
299    }
300
301    /**
302     * Returns a copy of this SynchronizedSummaryStatistics instance with the
303     * same internal state.
304     *
305     * @return a copy of this
306     */
307    @Override
308    public synchronized SynchronizedSummaryStatistics copy() {
309        SynchronizedSummaryStatistics result =
310            new SynchronizedSummaryStatistics();
311        copy(this, result);
312        return result;
313    }
314
315    /**
316     * Copies source to dest.
317     * <p>Neither source nor dest can be null.</p>
318     * <p>Acquires synchronization lock on source, then dest before copying.</p>
319     *
320     * @param source SynchronizedSummaryStatistics to copy
321     * @param dest SynchronizedSummaryStatistics to copy to
322     * @throws NullPointerException if either source or dest is null
323     */
324    public static void copy(SynchronizedSummaryStatistics source,
325            SynchronizedSummaryStatistics dest) {
326        synchronized (source) {
327            synchronized (dest) {
328                SummaryStatistics.copy(source, dest);
329            }
330        }
331    }
332
333}
334