1 /*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
54 min = val;
55 }
56 if (val > max) {
57 max = val;
58 }
59 }
60
61 public void add(final long val) {
62 count++;
63 sum += val;
64 if (val < min) {
65 min = val;
66 }
67 if (val > max) {
68 max = val;
69 }
70 }
71
72 @Override
73 public String toString() {
74 final StringBuilder sb = new StringBuilder(128);
75 toString(sb);
76 return sb.toString();
77 }
78
79 public final StringBuilder toString(final StringBuilder sb) {
80 sb.append(name).append('[').append(count);
81 sb.append("] sum: ").append(sum).append(" avg: ");
82 sb.append(trimTo3Digits(((double) sum) / count));
83 sb.append(" [").append(min).append(" | ").append(max).append("]");
84 return sb;
85 }
86
87 /**
88 * Adjust the given double value to keep only 3 decimal digits
89 *
90 * @param value value to adjust
91 * @return double value with only 3 decimal digits
92 */
93 public static double trimTo3Digits(final double value) {
94 return ((long) (1e3d * value)) / 1e3d;
95 }
96 }
|
1 /*
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
54 min = val;
55 }
56 if (val > max) {
57 max = val;
58 }
59 }
60
61 public void add(final long val) {
62 count++;
63 sum += val;
64 if (val < min) {
65 min = val;
66 }
67 if (val > max) {
68 max = val;
69 }
70 }
71
72 @Override
73 public String toString() {
74 return toString(new StringBuilder(128)).toString();
75 }
76
77 public final StringBuilder toString(final StringBuilder sb) {
78 sb.append(name).append('[').append(count);
79 sb.append("] sum: ").append(sum).append(" avg: ");
80 sb.append(trimTo3Digits(((double) sum) / count));
81 sb.append(" [").append(min).append(" | ").append(max).append("]");
82 return sb;
83 }
84
85 /**
86 * Adjust the given double value to keep only 3 decimal digits
87 *
88 * @param value value to adjust
89 * @return double value with only 3 decimal digits
90 */
91 public static double trimTo3Digits(final double value) {
92 return ((long) (1e3d * value)) / 1e3d;
93 }
94 }
|