SynchronizedSummaryStatistics.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.MathIllegalStateException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;

  20. /**
  21.  * Implementation of
  22.  * {@link org.apache.commons.math4.legacy.stat.descriptive.SummaryStatistics} 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 SynchronizedSummaryStatistics extends SummaryStatistics {
  33.     /**
  34.      * Construct a SynchronizedSummaryStatistics instance.
  35.      */
  36.     public SynchronizedSummaryStatistics() {
  37.         super();
  38.     }

  39.     /**
  40.      * A copy constructor. Creates a deep-copy of the {@code original}.
  41.      *
  42.      * @param original the {@code SynchronizedSummaryStatistics} instance to copy
  43.      * @throws NullArgumentException if original is null
  44.      */
  45.     public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original)
  46.     throws NullArgumentException {
  47.         copy(original, this);
  48.     }

  49.     /**
  50.      * {@inheritDoc}
  51.      */
  52.     @Override
  53.     public synchronized StatisticalSummary getSummary() {
  54.         return super.getSummary();
  55.     }

  56.     /**
  57.      * {@inheritDoc}
  58.      */
  59.     @Override
  60.     public synchronized void addValue(double value) {
  61.         super.addValue(value);
  62.     }

  63.     /**
  64.      * {@inheritDoc}
  65.      */
  66.     @Override
  67.     public synchronized long getN() {
  68.         return super.getN();
  69.     }

  70.     /**
  71.      * {@inheritDoc}
  72.      */
  73.     @Override
  74.     public synchronized double getSum() {
  75.         return super.getSum();
  76.     }

  77.     /**
  78.      * {@inheritDoc}
  79.      */
  80.     @Override
  81.     public synchronized double getSumsq() {
  82.         return super.getSumsq();
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     @Override
  88.     public synchronized double getMean() {
  89.         return super.getMean();
  90.     }

  91.     /**
  92.      * {@inheritDoc}
  93.      */
  94.     @Override
  95.     public synchronized double getStandardDeviation() {
  96.         return super.getStandardDeviation();
  97.     }

  98.     /**
  99.      * {@inheritDoc}
  100.      */
  101.     @Override
  102.     public synchronized double getQuadraticMean() {
  103.         return super.getQuadraticMean();
  104.     }

  105.     /**
  106.      * {@inheritDoc}
  107.      */
  108.     @Override
  109.     public synchronized double getVariance() {
  110.         return super.getVariance();
  111.     }

  112.     /**
  113.      * {@inheritDoc}
  114.      */
  115.     @Override
  116.     public synchronized double getPopulationVariance() {
  117.         return super.getPopulationVariance();
  118.     }

  119.     /**
  120.      * {@inheritDoc}
  121.      */
  122.     @Override
  123.     public synchronized double getMax() {
  124.         return super.getMax();
  125.     }

  126.     /**
  127.      * {@inheritDoc}
  128.      */
  129.     @Override
  130.     public synchronized double getMin() {
  131.         return super.getMin();
  132.     }

  133.     /**
  134.      * {@inheritDoc}
  135.      */
  136.     @Override
  137.     public synchronized double getGeometricMean() {
  138.         return super.getGeometricMean();
  139.     }

  140.     /**
  141.      * {@inheritDoc}
  142.      */
  143.     @Override
  144.     public synchronized String toString() {
  145.         return super.toString();
  146.     }

  147.     /**
  148.      * {@inheritDoc}
  149.      */
  150.     @Override
  151.     public synchronized void clear() {
  152.         super.clear();
  153.     }

  154.     /**
  155.      * {@inheritDoc}
  156.      */
  157.     @Override
  158.     public synchronized boolean equals(Object object) {
  159.         return super.equals(object);
  160.     }

  161.     /**
  162.      * {@inheritDoc}
  163.      */
  164.     @Override
  165.     public synchronized int hashCode() {
  166.         return super.hashCode();
  167.     }

  168.     /**
  169.      * {@inheritDoc}
  170.      */
  171.     @Override
  172.     public synchronized StorelessUnivariateStatistic getSumImpl() {
  173.         return super.getSumImpl();
  174.     }

  175.     /**
  176.      * {@inheritDoc}
  177.      */
  178.     @Override
  179.     public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl)
  180.     throws MathIllegalStateException {
  181.         super.setSumImpl(sumImpl);
  182.     }

  183.     /**
  184.      * {@inheritDoc}
  185.      */
  186.     @Override
  187.     public synchronized StorelessUnivariateStatistic getSumsqImpl() {
  188.         return super.getSumsqImpl();
  189.     }

  190.     /**
  191.      * {@inheritDoc}
  192.      */
  193.     @Override
  194.     public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl)
  195.     throws MathIllegalStateException {
  196.         super.setSumsqImpl(sumsqImpl);
  197.     }

  198.     /**
  199.      * {@inheritDoc}
  200.      */
  201.     @Override
  202.     public synchronized StorelessUnivariateStatistic getMinImpl() {
  203.         return super.getMinImpl();
  204.     }

  205.     /**
  206.      * {@inheritDoc}
  207.      */
  208.     @Override
  209.     public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl)
  210.     throws MathIllegalStateException {
  211.         super.setMinImpl(minImpl);
  212.     }

  213.     /**
  214.      * {@inheritDoc}
  215.      */
  216.     @Override
  217.     public synchronized StorelessUnivariateStatistic getMaxImpl() {
  218.         return super.getMaxImpl();
  219.     }

  220.     /**
  221.      * {@inheritDoc}
  222.      */
  223.     @Override
  224.     public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl)
  225.     throws MathIllegalStateException {
  226.         super.setMaxImpl(maxImpl);
  227.     }

  228.     /**
  229.      * {@inheritDoc}
  230.      */
  231.     @Override
  232.     public synchronized StorelessUnivariateStatistic getSumLogImpl() {
  233.         return super.getSumLogImpl();
  234.     }

  235.     /**
  236.      * {@inheritDoc}
  237.      */
  238.     @Override
  239.     public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
  240.     throws MathIllegalStateException {
  241.         super.setSumLogImpl(sumLogImpl);
  242.     }

  243.     /**
  244.      * {@inheritDoc}
  245.      */
  246.     @Override
  247.     public synchronized StorelessUnivariateStatistic getGeoMeanImpl() {
  248.         return super.getGeoMeanImpl();
  249.     }

  250.     /**
  251.      * {@inheritDoc}
  252.      */
  253.     @Override
  254.     public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl)
  255.     throws MathIllegalStateException {
  256.         super.setGeoMeanImpl(geoMeanImpl);
  257.     }

  258.     /**
  259.      * {@inheritDoc}
  260.      */
  261.     @Override
  262.     public synchronized StorelessUnivariateStatistic getMeanImpl() {
  263.         return super.getMeanImpl();
  264.     }

  265.     /**
  266.      * {@inheritDoc}
  267.      */
  268.     @Override
  269.     public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl)
  270.     throws MathIllegalStateException {
  271.         super.setMeanImpl(meanImpl);
  272.     }

  273.     /**
  274.      * {@inheritDoc}
  275.      */
  276.     @Override
  277.     public synchronized StorelessUnivariateStatistic getVarianceImpl() {
  278.         return super.getVarianceImpl();
  279.     }

  280.     /**
  281.      * {@inheritDoc}
  282.      */
  283.     @Override
  284.     public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl)
  285.     throws MathIllegalStateException {
  286.         super.setVarianceImpl(varianceImpl);
  287.     }

  288.     /**
  289.      * Returns a copy of this SynchronizedSummaryStatistics instance with the
  290.      * same internal state.
  291.      *
  292.      * @return a copy of this
  293.      */
  294.     @Override
  295.     public synchronized SynchronizedSummaryStatistics copy() {
  296.         SynchronizedSummaryStatistics result =
  297.             new SynchronizedSummaryStatistics();
  298.         // No try-catch or advertised exception because arguments are guaranteed non-null
  299.         copy(this, result);
  300.         return result;
  301.     }

  302.     /**
  303.      * Copies source to dest.
  304.      * <p>Neither source nor dest can be null.</p>
  305.      * <p>Acquires synchronization lock on source, then dest before copying.</p>
  306.      *
  307.      * @param source SynchronizedSummaryStatistics to copy
  308.      * @param dest SynchronizedSummaryStatistics to copy to
  309.      * @throws NullArgumentException if either source or dest is null
  310.      */
  311.     public static void copy(SynchronizedSummaryStatistics source,
  312.                             SynchronizedSummaryStatistics dest)
  313.         throws NullArgumentException {
  314.         NullArgumentException.check(source);
  315.         NullArgumentException.check(dest);
  316.         synchronized (source) {
  317.             synchronized (dest) {
  318.                 SummaryStatistics.copy(source, dest);
  319.             }
  320.         }
  321.     }
  322. }