@Override
  public InternalAggregation buildAggregation(long owningBucketOrdinal) {
    assert owningBucketOrdinal == 0;
    List<HistogramBase.Bucket> buckets =
        new ArrayList<HistogramBase.Bucket>((int) bucketOrds.size());
    for (long i = 0; i < bucketOrds.capacity(); ++i) {
      final long ord = bucketOrds.id(i);
      if (ord < 0) {
        continue; // slot is not allocated
      }
      buckets.add(
          histogramFactory.createBucket(
              rounding.valueForKey(bucketOrds.key(i)),
              bucketDocCount(ord),
              bucketAggregations(ord)));
    }

    CollectionUtil.introSort(buckets, order.comparator());

    // value source will be null for unmapped fields
    ValueFormatter formatter = valuesSource != null ? valuesSource.formatter() : null;
    AbstractHistogramBase.EmptyBucketInfo emptyBucketInfo =
        minDocCount == 0
            ? new AbstractHistogramBase.EmptyBucketInfo(rounding, buildEmptySubAggregations())
            : null;
    return histogramFactory.create(
        name, buckets, order, minDocCount, emptyBucketInfo, formatter, keyed);
  }
 @Override
 public InternalAggregation buildEmptyAggregation() {
   ValueFormatter formatter = valuesSource != null ? valuesSource.formatter() : null;
   AbstractHistogramBase.EmptyBucketInfo emptyBucketInfo =
       minDocCount == 0
           ? new AbstractHistogramBase.EmptyBucketInfo(rounding, buildEmptySubAggregations())
           : null;
   return histogramFactory.create(
       name, Collections.emptyList(), order, minDocCount, emptyBucketInfo, formatter, keyed);
 }
 public Factory(
     String name,
     ValuesSourceConfig<NumericValuesSource> valueSourceConfig,
     Rounding rounding,
     InternalOrder order,
     boolean keyed,
     long minDocCount,
     AbstractHistogramBase.Factory<?> histogramFactory) {
   super(name, histogramFactory.type(), valueSourceConfig);
   this.rounding = rounding;
   this.order = order;
   this.keyed = keyed;
   this.minDocCount = minDocCount;
   this.histogramFactory = histogramFactory;
 }