Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.avro.ipc.stats;
 
 import java.util.Map;
Represents a histogram of values. This class uses a Histogram.Segmenter to determine which bucket to place a given value into. Note that Histogram, by itself, is not synchronized.

Parameters:
<B> Bucket type. Often String, since buckets are typically used for their toString() representation.
<T> Type of value
 
 class Histogram<B, T> {
   private Segmenter<B, T> segmenter;
   private int[] counts;
   protected int totalCount;

  
Interface to determine which bucket to place a value in. Segmenters should be immutable, so many histograms can re-use the same segmenter.
 
   interface Segmenter<B, T> {
    
Number of buckets to use.
 
     int size();
    
Which bucket to place value in.

Returns:
Index of bucket for the value. At least 0 and less than size().
Throws:
Histogram.SegmenterException if value does not fit in a bucket.
 
     int segment(T value);
    
Returns an iterator of buckets. The order of iteration is consistent with the segment numbers.
 
     Iterator<B> getBuckets();
   }
 
   public static class SegmenterException extends RuntimeException {
     public SegmenterException(String s) {
       super(s);
     }
   }
 
   public static class TreeMapSegmenter<T extends Comparable<T>>
       implements Segmenter<String, T> {
     private TreeMap<T, Integerindex = new TreeMap<T, Integer>();
     public TreeMapSegmenter(SortedSet<T> leftEndpoints) {
       if (leftEndpoints.isEmpty()) {
         throw new IllegalArgumentException(
             "Endpoints must not be empty: " + leftEndpoints);
       }
       int i = 0;
       for (T t : leftEndpoints) {
         .put(ti++);
       }
     }
 
     public int segment(T value) {
       Map.Entry<T, Integere = .floorEntry(value);
       if (e == null) {
         throw new SegmenterException("Could not find bucket for: " + value);
       }
       return e.getValue();
     }
 
     @Override
     public int size() {
       return .size();
     }
 
     private String rangeAsString(T a, T b) {
       return String.format("[%s,%s)"ab == null ? "infinity" : b);
     }
 
     @Override
    public Iterator<StringgetBuckets() {
      return new Iterator<String>() {
        Iterator<T> it = .keySet().iterator();
        T cur = .next(); // there's always at least one element
        @Override
        public boolean hasNext() {
          return .hasNext();
        }
        @Override
        public String next() {
          T left = ;
           = .hasNext() ? .next() : null;
          return rangeAsString(left);
        }
        @Override
        public void remove() {
          throw new UnsupportedOperationException();
        }
      };
    }
  }

  
Creates a histogram using the specified segmenter.
  public Histogram(Segmenter<B, T> segmenter) {
    this. = segmenter;
    this. = new int[segmenter.size()];
  }

  
Tallies a value in the histogram.
  public void add(T value) {
    int i = .segment(value);
    [i]++;
    ++;
  }

  
Returns the underlying bucket values.
  public int[] getHistogram() {
    return ;
  }

  
Returns the total count of entries.
  public int getCount() {
    return ;
  }
  public String toString() {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (Entry<B> e : entries()) {
      if (!first) {
        sb.append(";");
      } else {
        first = false;
      }
      sb.append(e.bucket).append("=").append(e.count);
    }
    return sb.toString();
  }
  static class Entry<B> {
    public Entry(B bucketint count) {
      this. = bucket;
      this. = count;
    }
    B bucket;
    int count;
  }
  private class EntryIterator implements Iterable<Entry<B>>, Iterator<Entry<B>> {
    int i = 0;
    @Override
    public Iterator<Entry<B>> iterator() {
      return this;
    }
    @Override
    public boolean hasNext() {
      return  < .size();
    }
    @Override
    public Entry<B> next() {
      return new Entry<B>(.next(), [++]);
    }
    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  }
  public Iterable<Entry<B>> entries() {
    return new EntryIterator();
  }
New to GrepCode? Check out our FAQ X