View Javadoc
1   package org.djunits.formatter;
2   
3   import org.djunits.formatter.FormatContext.FloatFormatMode;
4   
5   /**
6    * MatrixFormat stores the settings that influence both the value part and the unit part of an output string when formatting a
7    * matrix.
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 MatrixFormat extends Format<MatrixFormat, MatrixFormatContext>
15  {
16      /** The defaults (which can be changed). */
17      @SuppressWarnings("checkstyle:staticvariablename")
18      private static MatrixFormatContext DEFAULT = makeDefault();
19  
20      /**
21       * Make the default format context for a matrix.
22       * @return a new default context for a matrix
23       */
24      private static MatrixFormatContext makeDefault()
25      {
26          var mfc = new MatrixFormatContext();
27          mfc.formatMode = FloatFormatMode.FIXED_WITH_SCI_FALLBACK;
28          return mfc;
29      }
30  
31      /**
32       * Construct a MatrixFormat 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 matrix format context to use
35       */
36      protected MatrixFormat(final MatrixFormatContext ctx)
37      {
38          super(ctx);
39      }
40  
41      /**
42       * Return an instance of MatrixFormat, initialized with the default values.
43       * @return an instance of MatrixFormat, initialized with the default values
44       */
45      public static MatrixFormat defaults()
46      {
47          return new MatrixFormat(DEFAULT.clone());
48      }
49  
50      /**
51       * Return an instance of MatrixFormat with the DEFAULT values, which can be changed for all subsequent calls.
52       * @return an instance of MatrixFormat with the DEFAULT values
53       */
54      public static MatrixFormat changeDefaults()
55      {
56          return new MatrixFormat(DEFAULT);
57      }
58  
59      /**
60       * Reset the default values of MatrixFormat to their original values.
61       */
62      public static void resetDefaults()
63      {
64          DEFAULT = makeDefault();
65      }
66  
67      /**
68       * Set the start symbol to use for a middle row in a matrix, e.g., "|".
69       * @param startSymbol new startSymbol for a matrix
70       * @return MatrixFormat object for fluent design
71       */
72      public MatrixFormat 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 matrix, e.g., "]".
80       * @param endSymbol new endSymbol for a matrix
81       * @return MatrixFormat object for fluent design
82       */
83      public MatrixFormat 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 matrix, e.g., "|".
91       * @param startSymbol new start symbol for the first row in a matrix
92       * @return MatrixFormat object for fluent design
93       */
94      public MatrixFormat 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 matrix, e.g., "|".
102      * @param endSymbol new end symbol for the first row in a matrix
103      * @return MatrixFormat object for fluent design
104      */
105     public MatrixFormat 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 matrix, e.g., "|".
113      * @param startSymbol new start symbol for the last row in a matrix
114      * @return MatrixFormat object for fluent design
115      */
116     public MatrixFormat 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 matrix, e.g., "|".
124      * @param endSymbol new end symbol for the last row in a matrix
125      * @return MatrixFormat object for fluent design
126      */
127     public MatrixFormat 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 matrix, e.g., " ".
135      * @param separatorSymbol new separatorSymbol for a matrix
136      * @return MatrixFormat object for fluent design
137      */
138     public MatrixFormat setColSeparatorSymbol(final String separatorSymbol)
139     {
140         this.ctx.colSeparatorSymbol = separatorSymbol;
141         return this;
142     }
143 
144     /**
145      * Set the matrix prefix, e.g., "Matrix\n".
146      * @param matrixPrefix new table prefix
147      * @return MatrixFormat object for fluent design
148      */
149     public MatrixFormat setMatrixPrefix(final String matrixPrefix)
150     {
151         this.ctx.matrixPrefix = matrixPrefix;
152         return this;
153     }
154 
155     /**
156      * Set the matrix postfix, e.g., "".
157      * @param matrixPostfix new table postfix
158      * @return MatrixFormat object for fluent design
159      */
160     public MatrixFormat setMatrixPostfix(final String matrixPostfix)
161     {
162         this.ctx.matrixPostfix = matrixPostfix;
163         return this;
164     }
165 
166 }