// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static void checkDouble(double value) {
//   checkArgument(
//       !Double.isInfinite(value) && !Double.isNaN(value) && !NEGATIVE_ZERO.equals(value),
//       "value must be finite, not NaN, and not -0.0");
// }


import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.monitoring.metrics.MetricsUtils.checkDouble;
import com.google.common.collect.ImmutableRangeMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.collect.TreeRangeMap;
import com.google.common.primitives.Doubles;
import java.util.Map;
import javax.annotation.concurrent.NotThreadSafe;
// Copyright 2017 Google LLC. All Rights Reserved.
//
// 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 com.google.monitoring.metrics;



/**
 * A mutable {@link Distribution}. Instances of this class <b>should not</b> be used to construct
 * {@link MetricPoint} instances as {@link MetricPoint} instances are supposed to represent
 * immutable values.
 *
 * @see ImmutableDistribution
 */
@NotThreadSafe
public final class MutableDistribution implements Distribution {

  private final TreeRangeMap<Double, Long> intervalCounts;
  private final DistributionFitter distributionFitter;
  private double sumOfSquaredDeviation = 0.0;
  private double mean = 0.0;
  private long count = 0;

  /** Constructs an empty Distribution with the specified {@link DistributionFitter}. */
  public MutableDistribution(DistributionFitter distributionFitter) {
    this.distributionFitter = checkNotNull(distributionFitter);
    ImmutableSortedSet<Double> boundaries = distributionFitter.boundaries();

    checkArgument(boundaries.size() > 0);
    checkArgument(Ordering.natural().isOrdered(boundaries));

    this.intervalCounts = TreeRangeMap.create();

    double[] boundariesArray = Doubles.toArray(distributionFitter.boundaries());

    // Add underflow and overflow intervals
    this.intervalCounts.put(Range.lessThan(boundariesArray[0]), 0L);
    this.intervalCounts.put(Range.atLeast(boundariesArray[boundariesArray.length - 1]), 0L);

    // Add finite intervals
    for (int i = 1; i < boundariesArray.length; i++) {
      this.intervalCounts.put(Range.closedOpen(boundariesArray[i - 1], boundariesArray[i]), 0L);
    }
  }

  public void add(double value) {
    add(value, 1L);
  }

  public void add(double value, long numSamples) {
    checkArgument(numSamples >= 0, "numSamples must be non-negative");
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static void checkDouble(double value) {
//   checkArgument(
//       !Double.isInfinite(value) && !Double.isNaN(value) && !NEGATIVE_ZERO.equals(value),
//       "value must be finite, not NaN, and not -0.0");
// }


// Path: metrics/src/main/java/com/google/monitoring/metrics/MutableDistribution.java
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.monitoring.metrics.MetricsUtils.checkDouble;
import com.google.common.collect.ImmutableRangeMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.collect.TreeRangeMap;
import com.google.common.primitives.Doubles;
import java.util.Map;
import javax.annotation.concurrent.NotThreadSafe;

// Copyright 2017 Google LLC. All Rights Reserved.
//
// 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 com.google.monitoring.metrics;



/**
 * A mutable {@link Distribution}. Instances of this class <b>should not</b> be used to construct
 * {@link MetricPoint} instances as {@link MetricPoint} instances are supposed to represent
 * immutable values.
 *
 * @see ImmutableDistribution
 */
@NotThreadSafe
public final class MutableDistribution implements Distribution {

  private final TreeRangeMap<Double, Long> intervalCounts;
  private final DistributionFitter distributionFitter;
  private double sumOfSquaredDeviation = 0.0;
  private double mean = 0.0;
  private long count = 0;

  /** Constructs an empty Distribution with the specified {@link DistributionFitter}. */
  public MutableDistribution(DistributionFitter distributionFitter) {
    this.distributionFitter = checkNotNull(distributionFitter);
    ImmutableSortedSet<Double> boundaries = distributionFitter.boundaries();

    checkArgument(boundaries.size() > 0);
    checkArgument(Ordering.natural().isOrdered(boundaries));

    this.intervalCounts = TreeRangeMap.create();

    double[] boundariesArray = Doubles.toArray(distributionFitter.boundaries());

    // Add underflow and overflow intervals
    this.intervalCounts.put(Range.lessThan(boundariesArray[0]), 0L);
    this.intervalCounts.put(Range.atLeast(boundariesArray[boundariesArray.length - 1]), 0L);

    // Add finite intervals
    for (int i = 1; i < boundariesArray.length; i++) {
      this.intervalCounts.put(Range.closedOpen(boundariesArray[i - 1], boundariesArray[i]), 0L);
    }
  }

  public void add(double value) {
    add(value, 1L);
  }

  public void add(double value, long numSamples) {
    checkArgument(numSamples >= 0, "numSamples must be non-negative");
    checkDouble(value);