View Javadoc
1   package org.djunits.formatter;
2   
3   import org.djunits.formatter.FormatContext.FloatFormatMode;
4   
5   /**
6    * TableFormat stores the settings that influence both the value part and the unit part of an output string when formatting a
7    * table.
8    * <p>
9    * Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
10   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
11   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
12   * @author Alexander Verbraeck
13   */
14  public class TableFormat extends Format<TableFormat, TableFormatContext>
15  {
16      /** The defaults (which can be changed). */
17      @SuppressWarnings("checkstyle:staticvariablename")
18      private static TableFormatContext DEFAULT = makeDefault();
19  
20      /**
21       * Make the default format context for a table.
22       * @return a new default context for a table
23       */
24      private static TableFormatContext makeDefault()
25      {
26          var tfc = new TableFormatContext();
27          tfc.formatMode = FloatFormatMode.FIXED_WITH_SCI_FALLBACK;
28          return tfc;
29      }
30  
31      /**
32       * Construct a TableFormat object with a given context. Note that the context can be an existing context that is being
33       * modified or a default context.
34       * @param ctx the table format context to use
35       */
36      protected TableFormat(final TableFormatContext ctx)
37      {
38          super(ctx);
39      }
40  
41      /**
42       * Return an instance of TableFormat, initialized with the default values.
43       * @return an instance of TableFormat, initialized with the default values
44       */
45      public static TableFormat defaults()
46      {
47          return new TableFormat(DEFAULT.clone());
48      }
49  
50      /**
51       * Return an instance of TableFormat with the DEFAULT values, which can be changed for all subsequent calls.
52       * @return an instance of TableFormat with the DEFAULT values
53       */
54      public static TableFormat changeDefaults()
55      {
56          return new TableFormat(DEFAULT);
57      }
58  
59      /**
60       * Reset the default values of TableFormat to their original values.
61       */
62      public static void resetDefaults()
63      {
64          DEFAULT = new TableFormatContext();
65      }
66  
67      /**
68       * Set the start symbol to use for a middle row in a table, e.g., "|".
69       * @param startSymbol new startSymbol for a table
70       * @return TableFormat object for fluent design
71       */
72      public TableFormat setMiddleRowStartSymbol(final String startSymbol)
73      {
74          this.ctx.middleRowStartSymbol = startSymbol;
75          return this;
76      }
77  
78      /**
79       * Set the end symbol for a middle row for a table, e.g., "]".
80       * @param endSymbol new endSymbol for a table
81       * @return TableFormat object for fluent design
82       */
83      public TableFormat setMiddleRowEndSymbol(final String endSymbol)
84      {
85          this.ctx.middleRowEndSymbol = endSymbol;
86          return this;
87      }
88  
89      /**
90       * Set the start symbol to use for the first row for a table, e.g., "|".
91       * @param startSymbol new start symbol for the first row in a table
92       * @return TableFormat object for fluent design
93       */
94      public TableFormat setFirstRowStartSymbol(final String startSymbol)
95      {
96          this.ctx.firstRowStartSymbol = startSymbol;
97          return this;
98      }
99  
100     /**
101      * Set the end symbol to use for the first row for a table, e.g., "|".
102      * @param endSymbol new end symbol for the first row in a table
103      * @return TableFormat object for fluent design
104      */
105     public TableFormat setFirstRowEndSymbol(final String endSymbol)
106     {
107         this.ctx.firstRowEndSymbol = endSymbol;
108         return this;
109     }
110 
111     /**
112      * Set the start symbol to use for the last row for a table, e.g., "|".
113      * @param startSymbol new start symbol for the last row in a table
114      * @return TableFormat object for fluent design
115      */
116     public TableFormat setLastRowStartSymbol(final String startSymbol)
117     {
118         this.ctx.lastRowStartSymbol = startSymbol;
119         return this;
120     }
121 
122     /**
123      * Set the end symbol to use for the last row for a table, e.g., "|".
124      * @param endSymbol new end symbol for the last row in a table
125      * @return TableFormat object for fluent design
126      */
127     public TableFormat setLastRowEndSymbol(final String endSymbol)
128     {
129         this.ctx.lastRowEndSymbol = endSymbol;
130         return this;
131     }
132 
133     /**
134      * Set the separator symbol to use between columns in a row for a table, e.g., " ".
135      * @param separatorSymbol new separatorSymbol for a table
136      * @return TableFormat object for fluent design
137      */
138     public TableFormat setColSeparatorSymbol(final String separatorSymbol)
139     {
140         this.ctx.colSeparatorSymbol = separatorSymbol;
141         return this;
142     }
143 
144     /**
145      * Set the table prefix, e.g., "Table\n".
146      * @param tablePrefix new table prefix
147      * @return TableFormat object for fluent design
148      */
149     public TableFormat setTablePrefix(final String tablePrefix)
150     {
151         this.ctx.tablePrefix = tablePrefix;
152         return this;
153     }
154 
155     /**
156      * Set the table postfix, e.g., "".
157      * @param tablePostfix new table postfix
158      * @return TableFormat object for fluent design
159      */
160     public TableFormat setTablePostfix(final String tablePostfix)
161     {
162         this.ctx.tablePostfix = tablePostfix;
163         return this;
164     }
165 
166 }