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