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
  23  * questions.
  24  */
  25 
  26 package com.sun.marlin.stats;
  27 
  28 /**
  29  * Statistics as long values
  30  */
  31 public class StatLong {
  32 
  33     public final String name;
  34     public long count = 0l;
  35     public long sum = 0l;
  36     public long min = Integer.MAX_VALUE;
  37     public long max = Integer.MIN_VALUE;
  38 
  39     public StatLong(final String name) {
  40         this.name = name;
  41     }
  42 
  43     public void reset() {
  44         count = 0l;
  45         sum = 0l;
  46         min = Integer.MAX_VALUE;
  47         max = Integer.MIN_VALUE;
  48     }
  49 
  50     public void add(final int val) {
  51         count++;
  52         sum += val;
  53         if (val < min) {
  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 }
  97