/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You 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.apache.geode.internal.statistics;

import java.util.concurrent.atomic.DoubleAdder;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Stream;

import org.apache.geode.StatisticsType;

/**
 * Stripes statistic counters across threads to reduce contention using {@link LongAdder} and
 * {@link DoubleAdder}.
 */
public class StripedStatisticsImpl extends StatisticsImpl {

  private final LongAdder[] intAdders;
  private final LongAdder[] longAdders;
  private final DoubleAdder[] doubleAdders;

  public StripedStatisticsImpl(StatisticsType type, String textId, long numericId,
      long uniqueId, StatisticsManager statisticsManager) {
    super(type, textId, numericId, uniqueId, 0, statisticsManager);

    StatisticsTypeImpl realType = (StatisticsTypeImpl) type;

    this.intAdders =
        Stream.generate(LongAdder::new).limit(realType.getIntStatCount()).toArray(LongAdder[]::new);
    this.longAdders =
        Stream.generate(LongAdder::new).limit(realType.getLongStatCount())
            .toArray(LongAdder[]::new);
    this.doubleAdders =
        Stream.generate(DoubleAdder::new).limit(realType.getDoubleStatCount())
            .toArray(DoubleAdder[]::new);
  }

  @Override
  public boolean isAtomic() {
    return true;
  }

  @Override
  protected void _setInt(int offset, int value) {
    intAdders[offset].reset();
    intAdders[offset].add(value);
  }

  @Override
  protected void _setLong(int offset, long value) {
    longAdders[offset].reset();
    longAdders[offset].add(value);
  }

  @Override
  protected void _setDouble(int offset, double value) {
    doubleAdders[offset].reset();
    doubleAdders[offset].add(value);
  }

  @Override
  protected int _getInt(int offset) {
    return intAdders[offset].intValue();
  }

  @Override
  protected long _getLong(int offset) {
    return longAdders[offset].sum();
  }

  @Override
  protected double _getDouble(int offset) {
    return doubleAdders[offset].sum();
  }

  @Override
  protected void _incInt(int offset, int delta) {
    intAdders[offset].add(delta);
  }

  @Override
  protected void _incLong(int offset, long delta) {
    longAdders[offset].add(delta);
  }

  @Override
  protected void _incDouble(int offset, double delta) {
    doubleAdders[offset].add(delta);
  }
}

@@ -2115,24 +2115,65 @@
 nt value) %7B%0A
+    synchronized (intAdders%5Boffset%5D) %7B%0A  
     intAdder
@@ -2187,24 +2187,26 @@
 t%5D.reset();%0A
+  
     intAdder
@@ -2219,32 +2219,38 @@
 et%5D.add(value);%0A
+    %7D%0A
   %7D%0A%0A  @Override
@@ -2298,24 +2298,66 @@
 ng value) %7B%0A
+    synchronized (longAdders%5Boffset%5D) %7B%0A  
     longAdde
@@ -2372,24 +2372,26 @@
 t%5D.reset();%0A
+  
     longAdde
@@ -2405,32 +2405,38 @@
 et%5D.add(value);%0A
+    %7D%0A
   %7D%0A%0A  @Override
@@ -2488,24 +2488,68 @@
 le value) %7B%0A
+    synchronized (doubleAdders%5Boffset%5D) %7B%0A  
     doubleAd
@@ -2566,24 +2566,26 @@
 t%5D.reset();%0A
+  
     doubleAd
@@ -2609,16 +2609,22 @@
 value);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -2662,32 +2662,73 @@
 t(int offset) %7B%0A
+    synchronized (intAdders%5Boffset%5D) %7B%0A  
     return intAd
@@ -2748,24 +2748,30 @@
 intValue();%0A
+    %7D%0A
   %7D%0A%0A  @Over
@@ -2807,32 +2807,74 @@
 g(int offset) %7B%0A
+    synchronized (longAdders%5Boffset%5D) %7B%0A  
     return longA
@@ -2886,32 +2886,38 @@
 %5Boffset%5D.sum();%0A
+    %7D%0A
   %7D%0A%0A  @Override
@@ -2957,24 +2957,68 @@
 t offset) %7B%0A
+    synchronized (doubleAdders%5Boffset%5D) %7B%0A  
     return d
@@ -3044,16 +3044,22 @@
 .sum();%0A
+    %7D%0A
   %7D%0A%0A  @

synchronize when getting and setting (#3220)