View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.quantity.Dimensionless;
4   import org.djunits.unit.scale.LinearScale;
5   import org.djunits.unit.scale.Scale;
6   import org.djunits.unit.si.SIPrefix;
7   import org.djunits.unit.si.SIUnit;
8   import org.djunits.unit.system.UnitSystem;
9   
10  /**
11   * Unitless encodes a unit without dimensions, e.g., to encode a constant.
12   * <p>
13   * Copyright (c) 2025-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  public class Unitless extends AbstractUnit<Dimensionless>
19  {
20      /** The dimensions of the dimensionless quantity: 1 [rad, sr, kg, m, s, A, K, mol, cd]. */
21      public static final SIUnit SI_UNIT = new SIUnit(new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0});
22  
23      /** The SI or BASE unit. */
24      public static final Unitless BASE = new Unitless(" ", " ", 1.0, UnitSystem.OTHER);
25  
26      /**
27       * Create a new Dimensionless unit.
28       * @param id the id or main abbreviation of the unit
29       * @param name the full name of the unit
30       * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
31       * @param unitSystem the unit system such as SI or IMPERIAL
32       */
33      public Unitless(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
34      {
35          super(id, name, scaleFactorToBaseUnit, unitSystem);
36      }
37  
38      /**
39       * Create a new unit, with textual abbreviation(s) and a display abbreviation.
40       * @param textualAbbreviation the textual abbreviation of the unit, which also serves as the id
41       * @param displayAbbreviation the display abbreviation of the unit
42       * @param name the full name of the unit
43       * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
44       * @param unitSystem unit system, e.g. SI or Imperial
45       * @param siPrefix the SI Prefix of this unit, can be null
46       */
47      public Unitless(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
48              final UnitSystem unitSystem, final SIPrefix siPrefix)
49      {
50          super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
51      }
52  
53      @Override
54      public SIUnit siUnit()
55      {
56          return SI_UNIT;
57      }
58  
59      @Override
60      public Unitless getBaseUnit()
61      {
62          return BASE;
63      }
64  
65      @Override
66      public Dimensionless ofSi(final double si, final UnitInterface<Dimensionless> displayUnit)
67      {
68          return new Dimensionless(si, (Unitless) displayUnit);
69      }
70  
71      @Override
72      public Unitless deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
73              final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
74      {
75          if (getScale() instanceof LinearScale ls)
76          {
77              return new Unitless(textualAbbreviation, displayAbbreviation, name,
78                      new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
79          }
80          throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
81      }
82  
83  }