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     */
017    package org.apache.commons.math.stat.descriptive;
018    
019    /**
020     * Implementation of
021     * {@link org.apache.commons.math.stat.descriptive.SummaryStatistics} that
022     * is safe to use in a multithreaded environment.  Multiple threads can safely
023     * operate on a single instance without causing runtime exceptions due to race
024     * conditions.  In effect, this implementation makes modification and access
025     * methods atomic operations for a single instance.  That is to say, as one
026     * thread is computing a statistic from the instance, no other thread can modify
027     * the instance nor compute another statistic.
028     *
029     * @since 1.2
030     * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $
031     */
032    public class SynchronizedSummaryStatistics extends SummaryStatistics {
033    
034        /** Serialization UID */
035        private static final long serialVersionUID = 1909861009042253704L;
036    
037        /**
038         * Construct a SynchronizedSummaryStatistics instance
039         */
040        public SynchronizedSummaryStatistics() {
041            super();
042        }
043    
044        /**
045         * A copy constructor. Creates a deep-copy of the {@code original}.
046         *
047         * @param original the {@code SynchronizedSummaryStatistics} instance to copy
048         */
049        public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original) {
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 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    }