001package io.prometheus.metrics.model.snapshots;
002
003import io.prometheus.metrics.annotations.StableApi;
004
005/**
006 * Helper class for iterating over {@link ClassicHistogramBuckets}. Note that the {@code count} is
007 * <i>not</i> cumulative.
008 */
009@StableApi
010public class ClassicHistogramBucket implements Comparable<ClassicHistogramBucket> {
011
012  private final long count; // not cumulative
013  private final double upperBound;
014
015  public ClassicHistogramBucket(double upperBound, long count) {
016    this.count = count;
017    this.upperBound = upperBound;
018    if (Double.isNaN(upperBound)) {
019      throw new IllegalArgumentException("Cannot use NaN as an upper bound for a histogram bucket");
020    }
021    if (count < 0) {
022      throw new IllegalArgumentException(
023          count
024              + ": "
025              + ClassicHistogramBuckets.class.getSimpleName()
026              + " cannot have a negative count");
027    }
028  }
029
030  public long getCount() {
031    return count;
032  }
033
034  public double getUpperBound() {
035    return upperBound;
036  }
037
038  /** For sorting a list of buckets by upper bound. */
039  @Override
040  public int compareTo(ClassicHistogramBucket other) {
041    int result = Double.compare(upperBound, other.upperBound);
042    if (result != 0) {
043      return result;
044    }
045    return Long.compare(count, other.count);
046  }
047}