Home | History | Annotate | Download | only in stats
      1 /*
      2  * Copyright 2017, OpenCensus Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.opencensus.stats;
     18 
     19 import com.google.auto.value.AutoValue;
     20 import io.opencensus.internal.Utils;
     21 import java.util.ArrayList;
     22 import java.util.Collections;
     23 import java.util.List;
     24 import javax.annotation.concurrent.Immutable;
     25 
     26 /**
     27  * The bucket boundaries for a histogram.
     28  *
     29  * @since 0.8
     30  */
     31 @Immutable
     32 @AutoValue
     33 public abstract class BucketBoundaries {
     34 
     35   /**
     36    * Returns a {@code BucketBoundaries} with the given buckets.
     37    *
     38    * @param bucketBoundaries the boundaries for the buckets in the underlying histogram.
     39    * @return a new {@code BucketBoundaries} with the specified boundaries.
     40    * @throws NullPointerException if {@code bucketBoundaries} is null.
     41    * @throws IllegalArgumentException if {@code bucketBoundaries} is not sorted.
     42    * @since 0.8
     43    */
     44   public static final BucketBoundaries create(List<Double> bucketBoundaries) {
     45     Utils.checkNotNull(bucketBoundaries, "bucketBoundaries");
     46     List<Double> bucketBoundariesCopy = new ArrayList<Double>(bucketBoundaries); // Deep copy.
     47     // Check if sorted.
     48     if (bucketBoundariesCopy.size() > 1) {
     49       double lower = bucketBoundariesCopy.get(0);
     50       for (int i = 1; i < bucketBoundariesCopy.size(); i++) {
     51         double next = bucketBoundariesCopy.get(i);
     52         Utils.checkArgument(lower < next, "Bucket boundaries not sorted.");
     53         lower = next;
     54       }
     55     }
     56     return new AutoValue_BucketBoundaries(Collections.unmodifiableList(bucketBoundariesCopy));
     57   }
     58 
     59   /**
     60    * Returns a list of histogram bucket boundaries.
     61    *
     62    * @return a list of histogram bucket boundaries.
     63    * @since 0.8
     64    */
     65   public abstract List<Double> getBoundaries();
     66 }
     67