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    import org.apache.commons.math.exception.NullArgumentException;
020    import org.apache.commons.math.util.MathUtils;
021    
022    /**
023     * Implementation of
024     * {@link org.apache.commons.math.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     * @version $Id: SynchronizedSummaryStatistics.java 1132432 2011-06-05 14:59:29Z luc $
034     */
035    public 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         */
052        public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original) {
053            copy(original, this);
054        }
055    
056        /**
057         * {@inheritDoc}
058         */
059        @Override
060        public synchronized StatisticalSummary getSummary() {
061            return super.getSummary();
062        }
063    
064        /**
065         * {@inheritDoc}
066         */
067        @Override
068        public synchronized void addValue(double value) {
069            super.addValue(value);
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        @Override
076        public synchronized long getN() {
077            return super.getN();
078        }
079    
080        /**
081         * {@inheritDoc}
082         */
083        @Override
084        public synchronized double getSum() {
085            return super.getSum();
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        @Override
092        public synchronized double getSumsq() {
093            return super.getSumsq();
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        @Override
100        public synchronized double getMean() {
101            return super.getMean();
102        }
103    
104        /**
105         * {@inheritDoc}
106         */
107        @Override
108        public synchronized double getStandardDeviation() {
109            return super.getStandardDeviation();
110        }
111    
112        /**
113         * {@inheritDoc}
114         */
115        @Override
116        public synchronized double getVariance() {
117            return super.getVariance();
118        }
119    
120        /**
121         * {@inheritDoc}
122         */
123        @Override
124        public synchronized double getMax() {
125            return super.getMax();
126        }
127    
128        /**
129         * {@inheritDoc}
130         */
131        @Override
132        public synchronized double getMin() {
133            return super.getMin();
134        }
135    
136        /**
137         * {@inheritDoc}
138         */
139        @Override
140        public synchronized double getGeometricMean() {
141            return super.getGeometricMean();
142        }
143    
144        /**
145         * {@inheritDoc}
146         */
147        @Override
148        public synchronized String toString() {
149            return super.toString();
150        }
151    
152        /**
153         * {@inheritDoc}
154         */
155        @Override
156        public synchronized void clear() {
157            super.clear();
158        }
159    
160        /**
161         * {@inheritDoc}
162         */
163        @Override
164        public synchronized boolean equals(Object object) {
165            return super.equals(object);
166        }
167    
168        /**
169         * {@inheritDoc}
170         */
171        @Override
172        public synchronized int hashCode() {
173            return super.hashCode();
174        }
175    
176        /**
177         * {@inheritDoc}
178         */
179        @Override
180        public synchronized StorelessUnivariateStatistic getSumImpl() {
181            return super.getSumImpl();
182        }
183    
184        /**
185         * {@inheritDoc}
186         */
187        @Override
188        public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl) {
189            super.setSumImpl(sumImpl);
190        }
191    
192        /**
193         * {@inheritDoc}
194         */
195        @Override
196        public synchronized StorelessUnivariateStatistic getSumsqImpl() {
197            return super.getSumsqImpl();
198        }
199    
200        /**
201         * {@inheritDoc}
202         */
203        @Override
204        public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl) {
205            super.setSumsqImpl(sumsqImpl);
206        }
207    
208        /**
209         * {@inheritDoc}
210         */
211        @Override
212        public synchronized StorelessUnivariateStatistic getMinImpl() {
213            return super.getMinImpl();
214        }
215    
216        /**
217         * {@inheritDoc}
218         */
219        @Override
220        public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl) {
221            super.setMinImpl(minImpl);
222        }
223    
224        /**
225         * {@inheritDoc}
226         */
227        @Override
228        public synchronized StorelessUnivariateStatistic getMaxImpl() {
229            return super.getMaxImpl();
230        }
231    
232        /**
233         * {@inheritDoc}
234         */
235        @Override
236        public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl) {
237            super.setMaxImpl(maxImpl);
238        }
239    
240        /**
241         * {@inheritDoc}
242         */
243        @Override
244        public synchronized StorelessUnivariateStatistic getSumLogImpl() {
245            return super.getSumLogImpl();
246        }
247    
248        /**
249         * {@inheritDoc}
250         */
251        @Override
252        public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) {
253            super.setSumLogImpl(sumLogImpl);
254        }
255    
256        /**
257         * {@inheritDoc}
258         */
259        @Override
260        public synchronized StorelessUnivariateStatistic getGeoMeanImpl() {
261            return super.getGeoMeanImpl();
262        }
263    
264        /**
265         * {@inheritDoc}
266         */
267        @Override
268        public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl) {
269            super.setGeoMeanImpl(geoMeanImpl);
270        }
271    
272        /**
273         * {@inheritDoc}
274         */
275        @Override
276        public synchronized StorelessUnivariateStatistic getMeanImpl() {
277            return super.getMeanImpl();
278        }
279    
280        /**
281         * {@inheritDoc}
282         */
283        @Override
284        public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl) {
285            super.setMeanImpl(meanImpl);
286        }
287    
288        /**
289         * {@inheritDoc}
290         */
291        @Override
292        public synchronized StorelessUnivariateStatistic getVarianceImpl() {
293            return super.getVarianceImpl();
294        }
295    
296        /**
297         * {@inheritDoc}
298         */
299        @Override
300        public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl) {
301            super.setVarianceImpl(varianceImpl);
302        }
303    
304        /**
305         * Returns a copy of this SynchronizedSummaryStatistics instance with the
306         * same internal state.
307         *
308         * @return a copy of this
309         */
310        @Override
311        public synchronized SynchronizedSummaryStatistics copy() {
312            SynchronizedSummaryStatistics result =
313                new SynchronizedSummaryStatistics();
314            copy(this, result);
315            return result;
316        }
317    
318        /**
319         * Copies source to dest.
320         * <p>Neither source nor dest can be null.</p>
321         * <p>Acquires synchronization lock on source, then dest before copying.</p>
322         *
323         * @param source SynchronizedSummaryStatistics to copy
324         * @param dest SynchronizedSummaryStatistics to copy to
325         * @throws NullArgumentException if either source or dest is null
326         */
327        public static void copy(SynchronizedSummaryStatistics source,
328                                SynchronizedSummaryStatistics dest)
329            throws NullArgumentException {
330            MathUtils.checkNotNull(source);
331            MathUtils.checkNotNull(dest);
332            synchronized (source) {
333                synchronized (dest) {
334                    SummaryStatistics.copy(source, dest);
335                }
336            }
337        }
338    
339    }