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 sun.java2d.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         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 }
  95