/*
 * All content copyright Terracotta, Inc., unless otherwise indicated.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terracotta.statistics.derived;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;

import org.terracotta.statistics.ValueStatistic;
import org.terracotta.statistics.observer.ChainedEventObserver;
import org.terracotta.statistics.util.InThreadExecutor;

import static java.lang.Double.doubleToLongBits;
import static java.lang.Double.longBitsToDouble;

/**
 *
 * @author cdennis
 */
public class MinMaxAverage implements ChainedEventObserver {

  private final AtomicLong maximum = new AtomicLong(Long.MIN_VALUE);
  private final AtomicLong minimum = new AtomicLong(Long.MAX_VALUE);
  
  private final AtomicLong summation = new AtomicLong(doubleToLongBits(0.0));
  private final AtomicLong count = new AtomicLong(0);

  private final Executor executor;
  
  public MinMaxAverage() {
    this(InThreadExecutor.INSTANCE);
  }
  
  public MinMaxAverage(Executor executor) {
    this.executor = executor;
  }
  
  
  @Override
  public void event(long time, final long ... parameters) {
    executor.execute(() -> {
      for (long max = maximum.get(); max < parameters[0] && !maximum.compareAndSet(max, parameters[0]); max = maximum.get());
      for (long min = minimum.get(); min > parameters[0] && !minimum.compareAndSet(min, parameters[0]); min = minimum.get());
      for (long sumBits = summation.get(); !summation.compareAndSet(sumBits, doubleToLongBits(longBitsToDouble(sumBits) + parameters[0])); sumBits = summation.get());
      count.incrementAndGet();
    });
  }

  public Long min() {
    if (count.get() == 0) {
      return null;
    } else {
      return minimum.get();
    }
  }
  
  public ValueStatistic<Long> minStatistic() {
    return this::min;
  }
  
  public Double mean() {
    if (count.get() == 0) {
      return null;
    } else {
      return longBitsToDouble(summation.get()) / count.get();
    }
  }
  
  public ValueStatistic<Double> meanStatistic() {
    return this::mean;
  }
  
  public Long max() {
    if (count.get() == 0) {
      return null;
    } else {
      return maximum.get();
    }
  }
  
  public ValueStatistic<Long> maxStatistic() {
    return this::max;
  }
}

@@ -758,16 +758,116 @@
 micLong;
+%0Aimport java.util.concurrent.atomic.DoubleAdder;%0Aimport java.util.concurrent.atomic.LongAccumulator;
 %0A%0Aimport
@@ -1034,107 +1034,8 @@
 r;%0A%0A
-import static java.lang.Double.doubleToLongBits;%0Aimport static java.lang.Double.longBitsToDouble;%0A%0A
 /**%0A
@@ -1142,113 +1142,155 @@
 nal 
-AtomicLong maximum = new AtomicLong(Long.MIN_VALUE);%0A  private final AtomicLong minimum = new AtomicLong(
+LongAccumulator maximum = new LongAccumulator(Math::max, Long.MIN_VALUE);%0A  private final LongAccumulator minimum = new LongAccumulator(Math::min, 
 Long
@@ -1302,18 +1302,16 @@
 VALUE);%0A
-  
 %0A  priva
@@ -1319,26 +1319,27 @@
 e final 
-AtomicLong
+DoubleAdder
  summati
@@ -1351,40 +1351,20 @@
 new 
-AtomicLong(doubleToLongBits(0.0)
+DoubleAdder(
 );%0A 
@@ -1607,19 +1607,16 @@
 %0A  %7D%0A  %0A
-  %0A
   @Overr
@@ -1718,418 +1718,127 @@
     
-for (long max = maximum.get(); max %3C parameters%5B0%5D && !maximum.compareAndSet(max, parameters%5B0%5D); max = maximum.get());%0A      for (long min = minimum.get(); min %3E parameters%5B0%5D && !minimum.compareAndSet(min, parameters%5B0%5D); min = minimum.get());%0A      for (long sumBits = summation.get(); !summation.compareAndSet(sumBits, doubleToLongBits(longBitsToDouble(sumBits) + parameters%5B0%5D)); sumBits = summation.get()
+long value = parameters%5B0%5D;%0A      maximum.accumulate(value);%0A      minimum.accumulate(value);%0A      summation.add(value
 );%0A 
@@ -2181,25 +2181,8 @@
 urn 
-longBitsToDouble(
 summ
@@ -2187,21 +2187,20 @@
 mmation.
-get()
+sum(
 ) / coun

Simplify calculations using Java 8 new classes