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-2025 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 the description of the statistic 25 */ 26 String getDescription(); 27 28 /** 29 * Set a new description of the statistic. 30 * @param description the new description of the statistic 31 */ 32 void setDescription(String description); 33 34 /** 35 * Return the current number of observations. 36 * @return the number of observations 37 */ 38 long getN(); 39 40 /** 41 * Return a string representing a line with important statistics values for this statistic, for a textual table with a 42 * monospaced font that can contain multiple statistics. 43 * @return line with most important values of the statistic 44 */ 45 String reportLine(); 46 47 /** 48 * Return a formatted string with 2 digits precision for a floating point value that fits the number of characters. The 49 * formatter will fall back to scientific notation when the value does not fit with floating point notation. 50 * @param value the value to format 51 * @param numberCharacters the number of characters for the result 52 * @return a string representation with the given number of characters 53 */ 54 default String formatFixed(final double value, final int numberCharacters) 55 { 56 if (Double.isNaN(value) || Double.isInfinite(value) || value == 0.0 || Math.abs(value) >= 0.01) 57 { 58 String formatted = String.format("%" + numberCharacters + ".3f", value); 59 if (formatted.length() == numberCharacters) 60 { 61 return formatted; 62 } 63 } 64 return String.format("%" + numberCharacters + ".3e", value); 65 } 66 }