View Javadoc
1   package org.djutils.stats.summarizers;
2   
3   import java.io.Serializable;
4   
5   /**
6    * The Statistic interface defines the methods to implement for each of the statistics classes.
7    * <p>
8    * Copyright (c) 2023-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9    * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
10   * project is distributed under a three-clause BSD-style license, which can be found at
11   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
12   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
13   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
14   */
15  public interface Statistic extends Serializable
16  {
17      /**
18       * Initialize the statistic.
19       */
20      void initialize();
21  
22      /**
23       * Returns the description of the statistic.
24       * @return String; the description of the statistic
25       */
26      String getDescription();
27  
28      /**
29       * Return the current number of observations.
30       * @return long; the number of observations
31       */
32      long getN();
33  
34      /**
35       * Return a string representing a line with important statistics values for this statistic, for a textual table with a
36       * monospaced font that can contain multiple statistics.
37       * @return String; line with most important values of the statistic
38       */
39      String reportLine();
40  
41      /**
42       * Return a formatted string with 2 digits precision for a floating point value that fits the number of characters. The
43       * formatter will fall back to scientific notation when the value does not fit with floating point notation.
44       * @param value double; the value to format
45       * @param numberCharacters int; the number of characters for the result
46       * @return String; a string representation with the given number of characters
47       */
48      default String formatFixed(final double value, final int numberCharacters)
49      {
50          if (Double.isNaN(value) || Double.isInfinite(value) || value == 0.0 || Math.abs(value) >= 0.01)
51          {
52              String formatted = String.format("%" + numberCharacters + ".3f", value);
53              if (formatted.length() == numberCharacters)
54              {
55                  return formatted;
56              }
57          }
58          return String.format("%" + numberCharacters + ".3e", value);
59      }
60  }