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.MathIllegalStateException;
20  import org.apache.commons.math4.legacy.exception.NullArgumentException;
21  
22  /**
23   * Implementation of
24   * {@link org.apache.commons.math4.legacy.stat.descriptive.SummaryStatistics} 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 SynchronizedSummaryStatistics extends SummaryStatistics {
35      /**
36       * Construct a SynchronizedSummaryStatistics instance.
37       */
38      public SynchronizedSummaryStatistics() {
39          super();
40      }
41  
42      /**
43       * A copy constructor. Creates a deep-copy of the {@code original}.
44       *
45       * @param original the {@code SynchronizedSummaryStatistics} instance to copy
46       * @throws NullArgumentException if original is null
47       */
48      public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original)
49      throws NullArgumentException {
50          copy(original, this);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public synchronized StatisticalSummary getSummary() {
58          return super.getSummary();
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public synchronized void addValue(double value) {
66          super.addValue(value);
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public synchronized long getN() {
74          return super.getN();
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public synchronized double getSum() {
82          return super.getSum();
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public synchronized double getSumsq() {
90          return super.getSumsq();
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public synchronized double getMean() {
98          return super.getMean();
99      }
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 getQuadraticMean() {
114         return super.getQuadraticMean();
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public synchronized double getVariance() {
122         return super.getVariance();
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public synchronized double getPopulationVariance() {
130         return super.getPopulationVariance();
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public synchronized double getMax() {
138         return super.getMax();
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public synchronized double getMin() {
146         return super.getMin();
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public synchronized double getGeometricMean() {
154         return super.getGeometricMean();
155     }
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public synchronized String toString() {
162         return super.toString();
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public synchronized void clear() {
170         super.clear();
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public synchronized boolean equals(Object object) {
178         return super.equals(object);
179     }
180 
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public synchronized int hashCode() {
186         return super.hashCode();
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     @Override
193     public synchronized StorelessUnivariateStatistic getSumImpl() {
194         return super.getSumImpl();
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     @Override
201     public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl)
202     throws MathIllegalStateException {
203         super.setSumImpl(sumImpl);
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public synchronized StorelessUnivariateStatistic getSumsqImpl() {
211         return super.getSumsqImpl();
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl)
219     throws MathIllegalStateException {
220         super.setSumsqImpl(sumsqImpl);
221     }
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public synchronized StorelessUnivariateStatistic getMinImpl() {
228         return super.getMinImpl();
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl)
236     throws MathIllegalStateException {
237         super.setMinImpl(minImpl);
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public synchronized StorelessUnivariateStatistic getMaxImpl() {
245         return super.getMaxImpl();
246     }
247 
248     /**
249      * {@inheritDoc}
250      */
251     @Override
252     public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl)
253     throws MathIllegalStateException {
254         super.setMaxImpl(maxImpl);
255     }
256 
257     /**
258      * {@inheritDoc}
259      */
260     @Override
261     public synchronized StorelessUnivariateStatistic getSumLogImpl() {
262         return super.getSumLogImpl();
263     }
264 
265     /**
266      * {@inheritDoc}
267      */
268     @Override
269     public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
270     throws MathIllegalStateException {
271         super.setSumLogImpl(sumLogImpl);
272     }
273 
274     /**
275      * {@inheritDoc}
276      */
277     @Override
278     public synchronized StorelessUnivariateStatistic getGeoMeanImpl() {
279         return super.getGeoMeanImpl();
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl)
287     throws MathIllegalStateException {
288         super.setGeoMeanImpl(geoMeanImpl);
289     }
290 
291     /**
292      * {@inheritDoc}
293      */
294     @Override
295     public synchronized StorelessUnivariateStatistic getMeanImpl() {
296         return super.getMeanImpl();
297     }
298 
299     /**
300      * {@inheritDoc}
301      */
302     @Override
303     public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl)
304     throws MathIllegalStateException {
305         super.setMeanImpl(meanImpl);
306     }
307 
308     /**
309      * {@inheritDoc}
310      */
311     @Override
312     public synchronized StorelessUnivariateStatistic getVarianceImpl() {
313         return super.getVarianceImpl();
314     }
315 
316     /**
317      * {@inheritDoc}
318      */
319     @Override
320     public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl)
321     throws MathIllegalStateException {
322         super.setVarianceImpl(varianceImpl);
323     }
324 
325     /**
326      * Returns a copy of this SynchronizedSummaryStatistics instance with the
327      * same internal state.
328      *
329      * @return a copy of this
330      */
331     @Override
332     public synchronized SynchronizedSummaryStatistics copy() {
333         SynchronizedSummaryStatistics result =
334             new SynchronizedSummaryStatistics();
335         // No try-catch or advertised exception because arguments are guaranteed non-null
336         copy(this, result);
337         return result;
338     }
339 
340     /**
341      * Copies source to dest.
342      * <p>Neither source nor dest can be null.</p>
343      * <p>Acquires synchronization lock on source, then dest before copying.</p>
344      *
345      * @param source SynchronizedSummaryStatistics to copy
346      * @param dest SynchronizedSummaryStatistics to copy to
347      * @throws NullArgumentException if either source or dest is null
348      */
349     public static void copy(SynchronizedSummaryStatistics source,
350                             SynchronizedSummaryStatistics dest)
351         throws NullArgumentException {
352         NullArgumentException.check(source);
353         NullArgumentException.check(dest);
354         synchronized (source) {
355             synchronized (dest) {
356                 SummaryStatistics.copy(source, dest);
357             }
358         }
359     }
360 }