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.SIUnit;
7   import org.djunits.unit.system.UnitSystem;
8   
9   /**
10   * Unitless encodes a unit without dimensions, e.g., to encode a constant.
11   * <p>
12   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
14   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
15   * @author Alexander Verbraeck
16   */
17  public class Unitless extends AbstractUnit<Unitless, Dimensionless>
18  {
19      /** The dimensions of the dimensionless quantity: 1 [rad, sr, kg, m, s, A, K, mol, cd]. */
20      public static final SIUnit SI_UNIT = new SIUnit(new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0});
21  
22      /** The SI or BASE unit. */
23      public static final Unitless BASE = new Unitless(" ", " ", 1.0, UnitSystem.OTHER);
24  
25      /**
26       * Create a new Dimensionless unit.
27       * @param id the id or main abbreviation of the unit
28       * @param name the full name of the unit
29       * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
30       * @param unitSystem the unit system such as SI or IMPERIAL
31       */
32      public Unitless(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
33      {
34          super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
35      }
36  
37      /**
38       * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
39       * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
40       * @param displayAbbreviation the display abbreviation of the unit
41       * @param name the full name of the unit
42       * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
43       * @param unitSystem unit system, e.g. SI or Imperial
44       */
45      public Unitless(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
46              final UnitSystem unitSystem)
47      {
48          super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
49      }
50  
51      @Override
52      public SIUnit siUnit()
53      {
54          return SI_UNIT;
55      }
56  
57      @Override
58      public Unitless getBaseUnit()
59      {
60          return BASE;
61      }
62  
63      @Override
64      public Dimensionless ofSi(final double si)
65      {
66          return Dimensionless.ofSi(si);
67      }
68  
69      @Override
70      public Unitless deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
71              final double scaleFactor, final UnitSystem unitSystem)
72      {
73          if (getScale() instanceof LinearScale ls)
74          {
75              return new Unitless(textualAbbreviation, displayAbbreviation, name,
76                      new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
77          }
78          throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
79      }
80  
81  }