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.math.stat.descriptive;
18
19 /**
20 * Implementation of
21 * {@link org.apache.commons.math.stat.descriptive.DescriptiveStatistics} that
22 * is safe to use in a multithreaded environment. Multiple threads can safely
23 * operate on a single instance without causing runtime exceptions due to race
24 * conditions. In effect, this implementation makes modification and access
25 * methods atomic operations for a single instance. That is to say, as one
26 * thread is computing a statistic from the instance, no other thread can modify
27 * the instance nor compute another statistic.
28 *
29 * @since 1.2
30 * @version $Revision: 608819 $ $Date: 2008-01-04 05:46:21 -0700 (Fri, 04 Jan 2008) $
31 */
32 public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics {
33
34 /** Serialization UID */
35 private static final long serialVersionUID = 1L;
36
37 /**
38 * Construct an instance with infinite window
39 */
40 public SynchronizedDescriptiveStatistics() {
41 this(INFINITE_WINDOW);
42 }
43
44 /**
45 * Construct an instance with finite window
46 * @param window the finite window size.
47 */
48 public SynchronizedDescriptiveStatistics(int window) {
49 super(window);
50 }
51
52 /**
53 * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#addValue(double)
54 */
55 public synchronized void addValue(double v) {
56 super.addValue(v);
57 }
58
59 /**
60 * Apply the given statistic to this univariate collection.
61 * @param stat the statistic to apply
62 * @return the computed value of the statistic.
63 */
64 public synchronized double apply(UnivariateStatistic stat) {
65 return super.apply(stat);
66 }
67
68 /**
69 * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#clear()
70 */
71 public synchronized void clear() {
72 super.clear();
73 }
74
75 /**
76 * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getElement(int)
77 */
78 public synchronized double getElement(int index) {
79 return super.getElement(index);
80 }
81
82 /**
83 * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getN()
84 */
85 public synchronized long getN() {
86 return super.getN();
87 }
88
89 /**
90 * Returns the standard deviation of the available values.
91 * @return The standard deviation, Double.NaN if no values have been added
92 * or 0.0 for a single value set.
93 */
94 public synchronized double getStandardDeviation() {
95 return super.getStandardDeviation();
96 }
97
98 /**
99 * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getValues()
100 */
101 public synchronized double[] getValues() {
102 return super.getValues();
103 }
104
105 /**
106 * Access the window size.
107 * @return the current window size.
108 */
109 public synchronized int getWindowSize() {
110 return super.getWindowSize();
111 }
112
113 /**
114 * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#setWindowSize(int)
115 */
116 public synchronized void setWindowSize(int windowSize) {
117 super.setWindowSize(windowSize);
118 }
119
120 /**
121 * Generates a text report displaying univariate statistics from values
122 * that have been added. Each statistic is displayed on a separate
123 * line.
124 *
125 * @return String with line feeds displaying statistics
126 */
127 public synchronized String toString() {
128 return super.toString();
129 }
130 }