001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math3.stat.descriptive; 018 019 import org.apache.commons.math3.exception.MathIllegalStateException; 020 import org.apache.commons.math3.exception.NullArgumentException; 021 import org.apache.commons.math3.util.MathUtils; 022 023 /** 024 * Implementation of 025 * {@link org.apache.commons.math3.stat.descriptive.SummaryStatistics} that 026 * is safe to use in a multithreaded environment. Multiple threads can safely 027 * operate on a single instance without causing runtime exceptions due to race 028 * conditions. In effect, this implementation makes modification and access 029 * methods atomic operations for a single instance. That is to say, as one 030 * thread is computing a statistic from the instance, no other thread can modify 031 * the instance nor compute another statistic. 032 * 033 * @since 1.2 034 * @version $Id: SynchronizedSummaryStatistics.java 1416643 2012-12-03 19:37:14Z tn $ 035 */ 036 public class SynchronizedSummaryStatistics extends SummaryStatistics { 037 038 /** Serialization UID */ 039 private static final long serialVersionUID = 1909861009042253704L; 040 041 /** 042 * Construct a SynchronizedSummaryStatistics instance 043 */ 044 public SynchronizedSummaryStatistics() { 045 super(); 046 } 047 048 /** 049 * A copy constructor. Creates a deep-copy of the {@code original}. 050 * 051 * @param original the {@code SynchronizedSummaryStatistics} instance to copy 052 * @throws NullArgumentException if original is null 053 */ 054 public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original) 055 throws NullArgumentException { 056 copy(original, this); 057 } 058 059 /** 060 * {@inheritDoc} 061 */ 062 @Override 063 public synchronized StatisticalSummary getSummary() { 064 return super.getSummary(); 065 } 066 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public synchronized void addValue(double value) { 072 super.addValue(value); 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 @Override 079 public synchronized long getN() { 080 return super.getN(); 081 } 082 083 /** 084 * {@inheritDoc} 085 */ 086 @Override 087 public synchronized double getSum() { 088 return super.getSum(); 089 } 090 091 /** 092 * {@inheritDoc} 093 */ 094 @Override 095 public synchronized double getSumsq() { 096 return super.getSumsq(); 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 public synchronized double getMean() { 104 return super.getMean(); 105 } 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public synchronized double getStandardDeviation() { 112 return super.getStandardDeviation(); 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 public synchronized double getVariance() { 120 return super.getVariance(); 121 } 122 123 /** 124 * {@inheritDoc} 125 */ 126 @Override 127 public synchronized double getPopulationVariance() { 128 return super.getPopulationVariance(); 129 } 130 131 /** 132 * {@inheritDoc} 133 */ 134 @Override 135 public synchronized double getMax() { 136 return super.getMax(); 137 } 138 139 /** 140 * {@inheritDoc} 141 */ 142 @Override 143 public synchronized double getMin() { 144 return super.getMin(); 145 } 146 147 /** 148 * {@inheritDoc} 149 */ 150 @Override 151 public synchronized double getGeometricMean() { 152 return super.getGeometricMean(); 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public synchronized String toString() { 160 return super.toString(); 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 @Override 167 public synchronized void clear() { 168 super.clear(); 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override 175 public synchronized boolean equals(Object object) { 176 return super.equals(object); 177 } 178 179 /** 180 * {@inheritDoc} 181 */ 182 @Override 183 public synchronized int hashCode() { 184 return super.hashCode(); 185 } 186 187 /** 188 * {@inheritDoc} 189 */ 190 @Override 191 public synchronized StorelessUnivariateStatistic getSumImpl() { 192 return super.getSumImpl(); 193 } 194 195 /** 196 * {@inheritDoc} 197 */ 198 @Override 199 public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl) 200 throws MathIllegalStateException { 201 super.setSumImpl(sumImpl); 202 } 203 204 /** 205 * {@inheritDoc} 206 */ 207 @Override 208 public synchronized StorelessUnivariateStatistic getSumsqImpl() { 209 return super.getSumsqImpl(); 210 } 211 212 /** 213 * {@inheritDoc} 214 */ 215 @Override 216 public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl) 217 throws MathIllegalStateException { 218 super.setSumsqImpl(sumsqImpl); 219 } 220 221 /** 222 * {@inheritDoc} 223 */ 224 @Override 225 public synchronized StorelessUnivariateStatistic getMinImpl() { 226 return super.getMinImpl(); 227 } 228 229 /** 230 * {@inheritDoc} 231 */ 232 @Override 233 public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl) 234 throws MathIllegalStateException { 235 super.setMinImpl(minImpl); 236 } 237 238 /** 239 * {@inheritDoc} 240 */ 241 @Override 242 public synchronized StorelessUnivariateStatistic getMaxImpl() { 243 return super.getMaxImpl(); 244 } 245 246 /** 247 * {@inheritDoc} 248 */ 249 @Override 250 public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl) 251 throws MathIllegalStateException { 252 super.setMaxImpl(maxImpl); 253 } 254 255 /** 256 * {@inheritDoc} 257 */ 258 @Override 259 public synchronized StorelessUnivariateStatistic getSumLogImpl() { 260 return super.getSumLogImpl(); 261 } 262 263 /** 264 * {@inheritDoc} 265 */ 266 @Override 267 public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) 268 throws MathIllegalStateException { 269 super.setSumLogImpl(sumLogImpl); 270 } 271 272 /** 273 * {@inheritDoc} 274 */ 275 @Override 276 public synchronized StorelessUnivariateStatistic getGeoMeanImpl() { 277 return super.getGeoMeanImpl(); 278 } 279 280 /** 281 * {@inheritDoc} 282 */ 283 @Override 284 public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl) 285 throws MathIllegalStateException { 286 super.setGeoMeanImpl(geoMeanImpl); 287 } 288 289 /** 290 * {@inheritDoc} 291 */ 292 @Override 293 public synchronized StorelessUnivariateStatistic getMeanImpl() { 294 return super.getMeanImpl(); 295 } 296 297 /** 298 * {@inheritDoc} 299 */ 300 @Override 301 public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl) 302 throws MathIllegalStateException { 303 super.setMeanImpl(meanImpl); 304 } 305 306 /** 307 * {@inheritDoc} 308 */ 309 @Override 310 public synchronized StorelessUnivariateStatistic getVarianceImpl() { 311 return super.getVarianceImpl(); 312 } 313 314 /** 315 * {@inheritDoc} 316 */ 317 @Override 318 public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl) 319 throws MathIllegalStateException { 320 super.setVarianceImpl(varianceImpl); 321 } 322 323 /** 324 * Returns a copy of this SynchronizedSummaryStatistics instance with the 325 * same internal state. 326 * 327 * @return a copy of this 328 */ 329 @Override 330 public synchronized SynchronizedSummaryStatistics copy() { 331 SynchronizedSummaryStatistics result = 332 new SynchronizedSummaryStatistics(); 333 // No try-catch or advertised exception because arguments are guaranteed non-null 334 copy(this, result); 335 return result; 336 } 337 338 /** 339 * Copies source to dest. 340 * <p>Neither source nor dest can be null.</p> 341 * <p>Acquires synchronization lock on source, then dest before copying.</p> 342 * 343 * @param source SynchronizedSummaryStatistics to copy 344 * @param dest SynchronizedSummaryStatistics to copy to 345 * @throws NullArgumentException if either source or dest is null 346 */ 347 public static void copy(SynchronizedSummaryStatistics source, 348 SynchronizedSummaryStatistics dest) 349 throws NullArgumentException { 350 MathUtils.checkNotNull(source); 351 MathUtils.checkNotNull(dest); 352 synchronized (source) { 353 synchronized (dest) { 354 SummaryStatistics.copy(source, dest); 355 } 356 } 357 } 358 359 }