View Javadoc
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  
19  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20  import org.apache.commons.math4.legacy.exception.NullArgumentException;
21  
22  /**
23   * Implementation of
24   * {@link org.apache.commons.math4.legacy.stat.descriptive.DescriptiveStatistics} that
25   * is safe to use in a multithreaded environment.  Multiple threads can safely
26   * operate on a single instance without causing runtime exceptions due to race
27   * conditions.  In effect, this implementation makes modification and access
28   * methods atomic operations for a single instance.  That is to say, as one
29   * thread is computing a statistic from the instance, no other thread can modify
30   * the instance nor compute another statistic.
31   *
32   * @since 1.2
33   */
34  public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics {
35      /**
36       * Construct an instance with infinite window.
37       */
38      public SynchronizedDescriptiveStatistics() {
39          // no try-catch or advertized IAE because arg is valid
40          this(INFINITE_WINDOW);
41      }
42  
43      /**
44       * Construct an instance with finite window.
45       * @param window the finite window size.
46       * @throws MathIllegalArgumentException if window size is less than 1 but
47       * not equal to {@link #INFINITE_WINDOW}
48       */
49      public SynchronizedDescriptiveStatistics(int window) throws MathIllegalArgumentException {
50          super(window);
51      }
52  
53      /**
54       * A copy constructor. Creates a deep-copy of the {@code original}.
55       *
56       * @param original the {@code SynchronizedDescriptiveStatistics} instance to copy
57       * @throws NullArgumentException if original is null
58       */
59      public SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original)
60      throws NullArgumentException {
61          copy(original, this);
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public synchronized void addValue(double v) {
69          super.addValue(v);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public synchronized double apply(UnivariateStatistic stat) {
77          return super.apply(stat);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public synchronized void clear() {
85          super.clear();
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public synchronized double getElement(int index) {
93          return super.getElement(index);
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @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 getQuadraticMean() {
117         return super.getQuadraticMean();
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public synchronized double[] getValues() {
125         return super.getValues();
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public synchronized int getWindowSize() {
133         return super.getWindowSize();
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public synchronized void setWindowSize(int windowSize) throws MathIllegalArgumentException {
141         super.setWindowSize(windowSize);
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public synchronized String toString() {
149         return super.toString();
150     }
151 
152     /**
153      * Returns a copy of this SynchronizedDescriptiveStatistics instance with the
154      * same internal state.
155      *
156      * @return a copy of this
157      */
158     @Override
159     public synchronized SynchronizedDescriptiveStatistics copy() {
160         SynchronizedDescriptiveStatistics result =
161             new SynchronizedDescriptiveStatistics();
162         // No try-catch or advertised exception because arguments are guaranteed non-null
163         copy(this, result);
164         return result;
165     }
166 
167     /**
168      * Copies source to dest.
169      * <p>Neither source nor dest can be null.</p>
170      * <p>Acquires synchronization lock on source, then dest before copying.</p>
171      *
172      * @param source SynchronizedDescriptiveStatistics to copy
173      * @param dest SynchronizedDescriptiveStatistics to copy to
174      * @throws NullArgumentException if either source or dest is null
175      */
176     public static void copy(SynchronizedDescriptiveStatistics source,
177                             SynchronizedDescriptiveStatistics dest)
178         throws NullArgumentException {
179         NullArgumentException.check(source);
180         NullArgumentException.check(dest);
181         synchronized (source) {
182             synchronized (dest) {
183                 DescriptiveStatistics.copy(source, dest);
184             }
185         }
186     }
187 }