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 = 10;
34  
35      /** Use grouping separator (e.g., thousands) or not. */
36      boolean groupingSeparator = false;
37  
38      /** Number format string. */
39      String formatString = null;
40  
41      // Unit formatting, contains default values
42  
43      /** Display unit to use. */
44      Unit<?, ?> displayUnit = null;
45  
46      /** Display unit to use, based on a String representation. */
47      String unitString = null;
48  
49      /** Textual representation, e.g., deg instead of degree symbol. */
50      boolean textual = false;
51  
52      /** SI representation rather than symbol. */
53      boolean siUnits = false;
54  
55      /** Use division symbol, e.g., TRUE: kgm2/s2, FALSE: kgm2s-2. */
56      boolean siDivisionSymbol = true;
57  
58      /** Symbol to use as the prefix for a power, e.g., "^" or "&lt;sup&gt;". */
59      String siPowerPrefix = "";
60  
61      /** Symbol to use as the postfix for a power, e.g., "&lt;/sup&gt;". */
62      String siPowerPostfix = "";
63  
64      /** Symbol to use for dot separation, e.g., "." to create kg.m2/s2. */
65      String siDotSeparator = "";
66  
67      // Quantity formatting, contains default values
68  
69      /** Use closest SI prefix. E.g., turn 20400 m into "20.4 km". */
70      boolean scaleSiPrefixes = false;
71  
72      /** minimum 10th power to use SI prefixes for. */
73      int minimumPrefixPower = -30;
74  
75      /** maximum 10th power to use SI prefixes for. */
76      int maximumPrefixPower = 32;
77  
78      /** prefix separator between the value and the unit. */
79      String unitPrefix = " ";
80  
81      /** postfix atring after the unit. */
82      String unitPostfix = "";
83  
84      // Absolute quantity formatting, contains default values
85  
86      /** Print the reference or not. */
87      boolean printReference = false;
88  
89      /** Prefix to the reference. */
90      String referencePrefix = " (";
91  
92      /** Postfix to the reference. */
93      String referencePostfix = ")";
94  
95      // Locale formatting
96  
97      /** Used locale for the entire output string (number and unit). */
98      Locale locale = null;
99  
100     /** The format mode. */
101     public enum FloatFormatMode
102     {
103         /** Use a format string. */
104         FORMAT_STRING,
105         /** Use a variable length format -- ignore width and decimals restrictions. */
106         VARIABLE_LENGTH,
107         /** Always apply an "f" formatter. */
108         FIXED_FLOAT,
109         /** Try to apply an "f" formatter, use scientific formatting as fallback. */
110         FIXED_WITH_SCI_FALLBACK,
111         /** Try to apply an "f" formatter, use engineering formatting (exponents are multiples of 3) as fallback. */
112         FIXED_WITH_ENG_FALLBACK,
113         /** Always apply scientific formatting (e formatter). */
114         SCIENTIFIC_ALWAYS,
115         /** Always apply engineering formatting (e formatter where exponents are multiples of 3). */
116         ENGINEERING_ALWAYS;
117     }
118 
119     @Override
120     protected FormatContext clone()
121     {
122         try
123         {
124             return (FormatContext) super.clone();
125         }
126         catch (CloneNotSupportedException exception)
127         {
128             throw new AssertionError(exception); // cannot happen
129         }
130     }
131 
132 }