5151 * offer easy ways to set common bucket patterns.
5252 */
5353public class Histogram extends SimpleCollector <Histogram .Child > {
54- double [] buckets ;
54+ private final double [] buckets ;
5555
5656 Histogram (Builder b ) {
5757 super (b );
@@ -60,7 +60,7 @@ public class Histogram extends SimpleCollector<Histogram.Child> {
6060 }
6161
6262 public static class Builder extends SimpleCollector .Builder <Builder , Histogram > {
63- double [] buckets = new double []{.005 , .01 , .025 , .05 , .075 , .1 , .25 , .5 , .75 , 1 , 2.5 , 5 , 7.5 , 10 };
63+ private double [] buckets = new double []{.005 , .01 , .025 , .05 , .075 , .1 , .25 , .5 , .75 , 1 , 2.5 , 5 , 7.5 , 10 };
6464
6565 @ Override
6666 public Histogram create () {
@@ -82,9 +82,7 @@ public Histogram create() {
8282 // Append infinity bucket if it's not already there.
8383 if (buckets [buckets .length - 1 ] != Double .POSITIVE_INFINITY ) {
8484 double [] tmp = new double [buckets .length + 1 ];
85- for (int i = 0 ; i < buckets .length ; i ++) {
86- tmp [i ] = buckets [i ];
87- }
85+ System .arraycopy (buckets , 0 , tmp , 0 , buckets .length );
8886 tmp [buckets .length ] = Double .POSITIVE_INFINITY ;
8987 buckets = tmp ;
9088 }
@@ -139,8 +137,8 @@ protected Child newChild() {
139137 * Represents an event being timed.
140138 */
141139 public static class Timer {
142- Child child ;
143- long start ;
140+ private final Child child ;
141+ private final long start ;
144142 private Timer (Child child ) {
145143 this .child = child ;
146144 start = Child .timeProvider .nanoTime ();
@@ -164,8 +162,13 @@ public double observeDuration() {
164162 */
165163 public static class Child {
166164 public static class Value {
167- public double sum ;
168- public double [] buckets ;
165+ public final double sum ;
166+ public final double [] buckets ;
167+
168+ public Value (double sum , double [] buckets ) {
169+ this .sum = sum ;
170+ this .buckets = buckets ;
171+ }
169172 }
170173
171174 private Child (double [] buckets ) {
@@ -175,9 +178,9 @@ private Child(double[] buckets) {
175178 cumulativeCounts [i ] = new DoubleAdder ();
176179 }
177180 }
178- private double [] upperBounds ;
179- private DoubleAdder [] cumulativeCounts ;
180- private DoubleAdder sum = new DoubleAdder ();
181+ private final double [] upperBounds ;
182+ private final DoubleAdder [] cumulativeCounts ;
183+ private final DoubleAdder sum = new DoubleAdder ();
181184
182185 static TimeProvider timeProvider = new TimeProvider ();
183186 /**
@@ -207,15 +210,13 @@ public Timer startTimer() {
207210 * <em>Warning:</em> The definition of {@link Value} is subject to change.
208211 */
209212 public Value get () {
210- Value v = new Value ();
211- v .buckets = new double [cumulativeCounts .length ];
213+ double [] buckets = new double [cumulativeCounts .length ];
212214 double acc = 0 ;
213215 for (int i = 0 ; i < cumulativeCounts .length ; ++i ) {
214216 acc += cumulativeCounts [i ].sum ();
215- v . buckets [i ] = acc ;
217+ buckets [i ] = acc ;
216218 }
217- v .sum = sum .sum ();
218- return v ;
219+ return new Value (sum .sum (), buckets );
219220 }
220221 }
221222
@@ -257,6 +258,10 @@ public List<MetricFamilySamples> collect() {
257258 return mfsList ;
258259 }
259260
261+ double [] getBuckets () {
262+ return buckets ;
263+ }
264+
260265 static class TimeProvider {
261266 long nanoTime () {
262267 return System .nanoTime ();
0 commit comments