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