Class FunctionBasedBucketSelector<T>
- java.lang.Object
-
- com.pervasivecode.utils.stats.histogram.FunctionBasedBucketSelector<T>
-
- Type Parameters:
T
- The type of upper bound value used by this BucketSelector.
- All Implemented Interfaces:
BucketingSystem<T>
,BucketSelector<T>
public final class FunctionBasedBucketSelector<T> extends Object implements BucketSelector<T>
A BucketSelector based on a pair ofFunction
s, one of which converts values into bucket indices, the other of which converts bucket indices into upper-bound values.- See Also:
BucketingSystem
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
bucketIndexFor(T value)
Determine the index of the bucket that a specified value should be counted in.T
bucketUpperBound(int index)
Get the upper-bound value of the specified bucket.int
numBuckets()
Get the total number of buckets.
-
-
-
Constructor Detail
-
FunctionBasedBucketSelector
public FunctionBasedBucketSelector(Function<T,Integer> valueToBucketIndexFunction, Function<Integer,T> bucketIndexToUpperBoundFunction, int numBuckets)
Create a BucketSelector using aFunction
that converts an upper-bound value into a bucket index, and anotherFunction
that converts a bucket index into an upper-bound value.Note: neither function will have to handle null values.
- Parameters:
valueToBucketIndexFunction
- A function that selects a bucket index for a given value of type T.bucketIndexToUpperBoundFunction
- A function that returns the upper-bound value for a given bucket index.numBuckets
- The total number of buckets including the highest, unbounded bucket.
-
-
Method Detail
-
numBuckets
public int numBuckets()
Description copied from interface:BucketingSystem
Get the total number of buckets. This will not be smaller than 1.- Specified by:
numBuckets
in interfaceBucketingSystem<T>
- Returns:
- The number of buckets.
-
bucketUpperBound
public T bucketUpperBound(int index)
Description copied from interface:BucketingSystem
Get the upper-bound value of the specified bucket.The first bucket has an index of 0. The last bucket has no upper bound, so passing the last index value in
index
is not allowed and will result in an IllegalArgumentException being thrown. In other words, if there are 10 buckets in a given instance, there are only 9 upper bound values, so the valid index values are 0 through 8 inclusive.A BucketingSystem with only one bucket is valid (all values fall in that bucket), but would have no upper bound values, so this method would always throw an IllegalArgumentException (for
index
value 0) or an IndexOutOfBoundsException (for all other values ofindex
).- Specified by:
bucketUpperBound
in interfaceBucketingSystem<T>
- Parameters:
index
- The bucket index to examine. This must be a value from zero through the next-to-last bucket index.- Returns:
- The upper bound value of the specified bucket. This will not be null.
-
bucketIndexFor
public int bucketIndexFor(T value)
Description copied from interface:BucketSelector
Determine the index of the bucket that a specified value should be counted in.- Specified by:
bucketIndexFor
in interfaceBucketSelector<T>
- Parameters:
value
- The value to compare to bucket upper bounds values when identifying the appropriate bucket.- Returns:
- The bucket index for the specified value.
-
-