View Javadoc
1   package org.djunits.formatter;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.Unit;
6   
7   /**
8    * FormatContext contains a number of basic settings for formatting a quantity, vector, matrix or quantity table. The settings
9    * deal with the number, unit, and locale. Note that this class and its fields are package private. It is not the intention to
10   * use or extends the class, since the {@link Format} class can only deal with the fields as expressed in this class, and not
11   * with other added fields.
12   * <p>
13   * Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
14   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
15   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
16   * @author Alexander Verbraeck
17   */
18  @SuppressWarnings("checkstyle:visibilitymodifier")
19  abstract class FormatContext implements Cloneable
20  {
21      // Number formatting, contains default values
22  
23      /** Format mode. */
24      FloatFormatMode formatMode = FloatFormatMode.VARIABLE_LENGTH;
25  
26      /** Scientific notation: upper case E or lower case e. */
27      boolean upperE = true;
28  
29      /** Number of decimal digits. */
30      int decimals = 3;
31  
32      /** Fixed width of the numerical output. */
33      int width = 12;
34  
35      /** Use grouping separator (e.g., thousands) or not. */
36      boolean groupingSeparator = false;
37  
38      /** Maximum number of significant digits to show for variable length format. */
39      int maxSigDigits = 10;
40  
41      /** Scientific threshold for scientific notation (has to be negative); only used for variable length format. */
42      int sciThreshold = -3;
43  
44      /** Number format string. */
45      String formatString = null;
46  
47      // Unit formatting, contains default values
48  
49      /** Display unit to use. */
50      Unit<?, ?> displayUnit = null;
51  
52      /** Display unit to use, based on a String representation. */
53      String unitString = null;
54  
55      /** Textual representation, e.g., deg instead of degree symbol. */
56      boolean textual = false;
57  
58      /** SI representation rather than symbol. */
59      boolean siUnits = false;
60  
61      /** Use division symbol, e.g., TRUE: kgm2/s2, FALSE: kgm2s-2. */
62      boolean siDivisionSymbol = true;
63  
64      /** Symbol to use as the prefix for a power, e.g., "^" or "&lt;sup&gt;". */
65      String siPowerPrefix = "";
66  
67      /** Symbol to use as the postfix for a power, e.g., "&lt;/sup&gt;". */
68      String siPowerPostfix = "";
69  
70      /** Symbol to use for dot separation, e.g., "." to create kg.m2/s2. */
71      String siDotSeparator = "";
72  
73      /** prefix separator between the value and the unit. */
74      String unitPrefix = " ";
75  
76      /** postfix atring after the unit. */
77      String unitPostfix = "";
78  
79      // Absolute quantity formatting, contains default values
80  
81      /** Print the reference or not. */
82      boolean printReference = false;
83  
84      /** Prefix to the reference. */
85      String referencePrefix = " (";
86  
87      /** Postfix to the reference. */
88      String referencePostfix = ")";
89  
90      // Locale formatting
91  
92      /** Used locale for the entire output string (number and unit). */
93      Locale locale = null;
94  
95      /** The format mode. */
96      public enum FloatFormatMode
97      {
98          /** Use a format string. */
99          FORMAT_STRING,
100         /** Use a variable length format -- ignore width and decimals restrictions. */
101         VARIABLE_LENGTH,
102         /** Always apply an "f" formatter. */
103         FIXED_FLOAT,
104         /** Try to apply an "f" formatter, use scientific formatting as fallback. */
105         FIXED_WITH_SCI_FALLBACK,
106         /** Try to apply an "f" formatter, use engineering formatting (exponents are multiples of 3) as fallback. */
107         FIXED_WITH_ENG_FALLBACK,
108         /** Always apply scientific formatting (e formatter). */
109         SCIENTIFIC_ALWAYS,
110         /** Always apply engineering formatting (e formatter where exponents are multiples of 3). */
111         ENGINEERING_ALWAYS;
112     }
113 
114     @Override
115     protected FormatContext clone()
116     {
117         try
118         {
119             return (FormatContext) super.clone();
120         }
121         catch (CloneNotSupportedException exception)
122         {
123             throw new AssertionError(exception); // cannot happen
124         }
125     }
126 
127 }