SynchronizedDescriptiveStatistics.java

  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.  */
  17. package org.apache.commons.math4.legacy.stat.descriptive;

  18. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;

  20. /**
  21.  * Implementation of
  22.  * {@link org.apache.commons.math4.legacy.stat.descriptive.DescriptiveStatistics} that
  23.  * is safe to use in a multithreaded environment.  Multiple threads can safely
  24.  * operate on a single instance without causing runtime exceptions due to race
  25.  * conditions.  In effect, this implementation makes modification and access
  26.  * methods atomic operations for a single instance.  That is to say, as one
  27.  * thread is computing a statistic from the instance, no other thread can modify
  28.  * the instance nor compute another statistic.
  29.  *
  30.  * @since 1.2
  31.  */
  32. public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics {
  33.     /**
  34.      * Construct an instance with infinite window.
  35.      */
  36.     public SynchronizedDescriptiveStatistics() {
  37.         // no try-catch or advertized IAE because arg is valid
  38.         this(INFINITE_WINDOW);
  39.     }

  40.     /**
  41.      * Construct an instance with finite window.
  42.      * @param window the finite window size.
  43.      * @throws MathIllegalArgumentException if window size is less than 1 but
  44.      * not equal to {@link #INFINITE_WINDOW}
  45.      */
  46.     public SynchronizedDescriptiveStatistics(int window) throws MathIllegalArgumentException {
  47.         super(window);
  48.     }

  49.     /**
  50.      * A copy constructor. Creates a deep-copy of the {@code original}.
  51.      *
  52.      * @param original the {@code SynchronizedDescriptiveStatistics} instance to copy
  53.      * @throws NullArgumentException if original is null
  54.      */
  55.     public SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original)
  56.     throws NullArgumentException {
  57.         copy(original, this);
  58.     }

  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     @Override
  63.     public synchronized void addValue(double v) {
  64.         super.addValue(v);
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      */
  69.     @Override
  70.     public synchronized double apply(UnivariateStatistic stat) {
  71.         return super.apply(stat);
  72.     }

  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     @Override
  77.     public synchronized void clear() {
  78.         super.clear();
  79.     }

  80.     /**
  81.      * {@inheritDoc}
  82.      */
  83.     @Override
  84.     public synchronized double getElement(int index) {
  85.         return super.getElement(index);
  86.     }

  87.     /**
  88.      * {@inheritDoc}
  89.      */
  90.     @Override
  91.     public synchronized long getN() {
  92.         return super.getN();
  93.     }

  94.     /**
  95.      * {@inheritDoc}
  96.      */
  97.     @Override
  98.     public synchronized double getStandardDeviation() {
  99.         return super.getStandardDeviation();
  100.     }

  101.     /**
  102.      * {@inheritDoc}
  103.      */
  104.     @Override
  105.     public synchronized double getQuadraticMean() {
  106.         return super.getQuadraticMean();
  107.     }

  108.     /**
  109.      * {@inheritDoc}
  110.      */
  111.     @Override
  112.     public synchronized double[] getValues() {
  113.         return super.getValues();
  114.     }

  115.     /**
  116.      * {@inheritDoc}
  117.      */
  118.     @Override
  119.     public synchronized int getWindowSize() {
  120.         return super.getWindowSize();
  121.     }

  122.     /**
  123.      * {@inheritDoc}
  124.      */
  125.     @Override
  126.     public synchronized void setWindowSize(int windowSize) throws MathIllegalArgumentException {
  127.         super.setWindowSize(windowSize);
  128.     }

  129.     /**
  130.      * {@inheritDoc}
  131.      */
  132.     @Override
  133.     public synchronized String toString() {
  134.         return super.toString();
  135.     }

  136.     /**
  137.      * Returns a copy of this SynchronizedDescriptiveStatistics instance with the
  138.      * same internal state.
  139.      *
  140.      * @return a copy of this
  141.      */
  142.     @Override
  143.     public synchronized SynchronizedDescriptiveStatistics copy() {
  144.         SynchronizedDescriptiveStatistics result =
  145.             new SynchronizedDescriptiveStatistics();
  146.         // No try-catch or advertised exception because arguments are guaranteed non-null
  147.         copy(this, result);
  148.         return result;
  149.     }

  150.     /**
  151.      * Copies source to dest.
  152.      * <p>Neither source nor dest can be null.</p>
  153.      * <p>Acquires synchronization lock on source, then dest before copying.</p>
  154.      *
  155.      * @param source SynchronizedDescriptiveStatistics to copy
  156.      * @param dest SynchronizedDescriptiveStatistics to copy to
  157.      * @throws NullArgumentException if either source or dest is null
  158.      */
  159.     public static void copy(SynchronizedDescriptiveStatistics source,
  160.                             SynchronizedDescriptiveStatistics dest)
  161.         throws NullArgumentException {
  162.         NullArgumentException.check(source);
  163.         NullArgumentException.check(dest);
  164.         synchronized (source) {
  165.             synchronized (dest) {
  166.                 DescriptiveStatistics.copy(source, dest);
  167.             }
  168.         }
  169.     }
  170. }