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: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $ 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 * A copy constructor. Creates a deep-copy of the {@code original}. 54 * 55 * @param original the {@code SynchronizedDescriptiveStatistics} instance to copy 56 */ 57 public SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original) { 58 copy(original, this); 59 } 60 61 /** 62 * {@inheritDoc} 63 */ 64 @Override 65 public synchronized void addValue(double v) { 66 super.addValue(v); 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 public synchronized double apply(UnivariateStatistic stat) { 74 return super.apply(stat); 75 } 76 77 /** 78 * {@inheritDoc} 79 */ 80 @Override 81 public synchronized void clear() { 82 super.clear(); 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 @Override 89 public synchronized double getElement(int index) { 90 return super.getElement(index); 91 } 92 93 /** 94 * {@inheritDoc} 95 */ 96 @Override 97 public synchronized long getN() { 98 return super.getN(); 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[] getValues() { 114 return super.getValues(); 115 } 116 117 /** 118 * {@inheritDoc} 119 */ 120 @Override 121 public synchronized int getWindowSize() { 122 return super.getWindowSize(); 123 } 124 125 /** 126 * {@inheritDoc} 127 */ 128 @Override 129 public synchronized void setWindowSize(int windowSize) { 130 super.setWindowSize(windowSize); 131 } 132 133 /** 134 * {@inheritDoc} 135 */ 136 @Override 137 public synchronized String toString() { 138 return super.toString(); 139 } 140 141 /** 142 * Returns a copy of this SynchronizedDescriptiveStatistics instance with the 143 * same internal state. 144 * 145 * @return a copy of this 146 */ 147 @Override 148 public synchronized SynchronizedDescriptiveStatistics copy() { 149 SynchronizedDescriptiveStatistics result = 150 new SynchronizedDescriptiveStatistics(); 151 copy(this, result); 152 return result; 153 } 154 155 /** 156 * Copies source to dest. 157 * <p>Neither source nor dest can be null.</p> 158 * <p>Acquires synchronization lock on source, then dest before copying.</p> 159 * 160 * @param source SynchronizedDescriptiveStatistics to copy 161 * @param dest SynchronizedDescriptiveStatistics to copy to 162 * @throws NullPointerException if either source or dest is null 163 */ 164 public static void copy(SynchronizedDescriptiveStatistics source, 165 SynchronizedDescriptiveStatistics dest) { 166 synchronized (source) { 167 synchronized (dest) { 168 DescriptiveStatistics.copy(source, dest); 169 } 170 } 171 } 172 } 173