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.DescriptiveStatistics} 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: SynchronizedDescriptiveStatistics.java 1132432 2011-06-05 14:59:29Z luc $
034     */
035    public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics {
036    
037        /** Serialization UID */
038        private static final long serialVersionUID = 1L;
039    
040        /**
041         * Construct an instance with infinite window
042         */
043        public SynchronizedDescriptiveStatistics() {
044            this(INFINITE_WINDOW);
045        }
046    
047        /**
048         * Construct an instance with finite window
049         * @param window the finite window size.
050         */
051        public SynchronizedDescriptiveStatistics(int window) {
052            super(window);
053        }
054    
055        /**
056         * A copy constructor. Creates a deep-copy of the {@code original}.
057         *
058         * @param original the {@code SynchronizedDescriptiveStatistics} instance to copy
059         */
060        public SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original) {
061            copy(original, this);
062        }
063    
064        /**
065         * {@inheritDoc}
066         */
067        @Override
068        public synchronized void addValue(double v) {
069            super.addValue(v);
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        @Override
076        public synchronized double apply(UnivariateStatistic stat) {
077            return super.apply(stat);
078        }
079    
080        /**
081         * {@inheritDoc}
082         */
083        @Override
084        public synchronized void clear() {
085            super.clear();
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        @Override
092        public synchronized double getElement(int index) {
093            return super.getElement(index);
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        @Override
100        public synchronized long getN() {
101            return super.getN();
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[] getValues() {
117            return super.getValues();
118        }
119    
120        /**
121         * {@inheritDoc}
122         */
123        @Override
124        public synchronized int getWindowSize() {
125            return super.getWindowSize();
126        }
127    
128        /**
129         * {@inheritDoc}
130         */
131        @Override
132        public synchronized void setWindowSize(int windowSize) {
133            super.setWindowSize(windowSize);
134        }
135    
136        /**
137         * {@inheritDoc}
138         */
139        @Override
140        public synchronized String toString() {
141            return super.toString();
142        }
143    
144        /**
145         * Returns a copy of this SynchronizedDescriptiveStatistics instance with the
146         * same internal state.
147         *
148         * @return a copy of this
149         */
150        @Override
151        public synchronized SynchronizedDescriptiveStatistics copy() {
152            SynchronizedDescriptiveStatistics result =
153                new SynchronizedDescriptiveStatistics();
154            copy(this, result);
155            return result;
156        }
157    
158        /**
159         * Copies source to dest.
160         * <p>Neither source nor dest can be null.</p>
161         * <p>Acquires synchronization lock on source, then dest before copying.</p>
162         *
163         * @param source SynchronizedDescriptiveStatistics to copy
164         * @param dest SynchronizedDescriptiveStatistics to copy to
165         * @throws NullArgumentException if either source or dest is null
166         */
167        public static void copy(SynchronizedDescriptiveStatistics source,
168                                SynchronizedDescriptiveStatistics dest)
169            throws NullArgumentException {
170            MathUtils.checkNotNull(source);
171            MathUtils.checkNotNull(dest);
172            synchronized (source) {
173                synchronized (dest) {
174                    DescriptiveStatistics.copy(source, dest);
175                }
176            }
177        }
178    }