001package org.javasimon.callback.quantiles;
002
003/**
004 * Bucket count the number of samples in the range min-max.
005 *
006 * @author gquintana
007 * @since 3.2
008 */
009public final class Bucket {
010
011        /** Minimal value. */
012        private final long min;
013        /** Maximal value. */
014        private final long max;
015        /** Number of values in the range min-max. */
016        private int count;
017
018        /**
019         * Constructor with min/max value specified.
020         *
021         * @param min min value
022         * @param max max value
023         */
024        public Bucket(long min, long max) {
025                this.min = min;
026                this.max = max;
027        }
028
029        /**
030         * Get number of values in the range.
031         *
032         * @return number of value in the range
033         */
034        public int getCount() {
035                return count;
036        }
037
038        /**
039         * Get upper bound of the range.
040         *
041         * @return max value
042         */
043        public long getMax() {
044                return max;
045        }
046
047        /**
048         * Get lower bound of the range.
049         *
050         * @return min value
051         */
052        public long getMin() {
053                return min;
054        }
055
056        /**
057         * Check whether value is in the range.
058         *
059         * @param value Value
060         * @return true if in range
061         */
062        public boolean contains(long value) {
063                return (value >= min) && (value <= max);
064        }
065
066        /**
067         * Increment value number
068         */
069        public void incrementCount() {
070                count++;
071        }
072
073        /**
074         * Check if value is in range and increment value number.
075         *
076         * @param value added value
077         * @return true if value is in bucket range (count was increased)
078         */
079        public boolean addValue(long value) {
080                if (contains(value)) {
081                        incrementCount();
082                        return true;
083                } else {
084                        return false;
085                }
086        }
087
088        /**
089         * Resets value number.
090         */
091        public void clear() {
092                count = 0;
093        }
094
095        /**
096         * Get sample from this bucket
097         *
098         * @return Sample
099         */
100        public BucketSample sample() {
101                return new BucketSample(min, max, count);
102        }
103}