View Javadoc
1   package org.djunits.formatter;
2   
3   /**
4    * QuantityFormat stores the settings that influence both the value part and the unit part of an output string when formatting a
5    * quantity.
6    * <p>
7    * Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
8    * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
9    * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
10   * @author Alexander Verbraeck
11   */
12  public class QuantityFormat extends Format<QuantityFormat, QuantityFormatContext>
13  {
14      /** The defaults (which can be changed). */
15      @SuppressWarnings("checkstyle:staticvariablename")
16      private static QuantityFormatContext DEFAULT = new QuantityFormatContext();
17  
18      /**
19       * Construct a QuantityFormat object with a given context. Note that the context can be an existing context that is being
20       * modified or a default context.
21       * @param ctx the quantity format context to use
22       */
23      protected QuantityFormat(final QuantityFormatContext ctx)
24      {
25          super(ctx);
26      }
27  
28      /**
29       * Return an instance of QuantityFormat, initialized with the default values.
30       * @return an instance of QuantityFormat, initialized with the default values
31       */
32      public static QuantityFormat defaults()
33      {
34          return new QuantityFormat(DEFAULT.clone());
35      }
36  
37      /**
38       * Return an instance of QuantityFormat with the DEFAULT values, which can be changed for all subsequent calls.
39       * @return an instance of QuantityFormat with the DEFAULT values
40       */
41      public static QuantityFormat changeDefaults()
42      {
43          return new QuantityFormat(DEFAULT);
44      }
45  
46      /**
47       * Reset the default values of QuantityFormat to their original values.
48       */
49      public static void resetDefaults()
50      {
51          DEFAULT = new QuantityFormatContext();
52      }
53  
54      /**
55       * Use closest SI prefix. E.g., turn 20400 m into "20.4 km".
56       * @return QuantityFormat object for fluent design
57       */
58      public QuantityFormat setScaleSiPrefixes()
59      {
60          this.ctx.scaleSiPrefixes = true;
61          return this;
62      }
63  
64      /**
65       * Use closest SI prefix. E.g., turn 20400 m into "20.4 km".
66       * @param minPrefixPower minimum 10th power to use SI prefixes for
67       * @param maxPrefixPower maximum 10th power to use SI prefixes for
68       * @return QuantityFormat object for fluent design
69       */
70      public QuantityFormat setScaleSiPrefixes(final int minPrefixPower, final int maxPrefixPower)
71      {
72          this.ctx.scaleSiPrefixes = true;
73          this.ctx.minimumPrefixPower = minPrefixPower;
74          this.ctx.maximumPrefixPower = maxPrefixPower;
75          return this;
76      }
77  
78  }