View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.si.SIDimensions;
4   import org.djunits.unit.util.UnitException;
5   import org.djutils.logger.CategoryLogger;
6   
7   /**
8    * SIUnit describes a unit with arbitrary SI dimensions for which no predefined unit exists.
9    * <p>
10   * Copyright (c) 2019-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>
12   * </p>
13   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
14   */
15  public class SIUnit extends Unit<SIUnit>
16  {
17      /** */
18      private static final long serialVersionUID = 20190829L;
19  
20      /** the SI Unit for dimensionless. */
21      public static final SIUnit DIMLESS = SIUnit.of(SIDimensions.DIMLESS);
22  
23      static
24      {
25          // make sure all predefined unit types get registered before we start using SIScalars.
26          try
27          {
28              Class.forName("org.djunits.unit.util.UNITS");
29          }
30          catch (ClassNotFoundException exception)
31          {
32              CategoryLogger.always().error("Could not find class org.djunits.unit.util.UNITS for initializing SIUnit");
33          }
34      }
35  
36      /**
37       * Instantiate an SI unit 'of' a String.
38       * @param siString the SI string, e.g., "kgm/s2"
39       * @return the SIUnit based on the SI dimensionality
40       * @throws UnitException when the SI string is not according to the rules
41       */
42      public static SIUnit of(final String siString) throws UnitException
43      {
44          return Unit.lookupOrCreateUnitWithSIDimensions(SIDimensions.of(siString));
45      }
46  
47      /**
48       * Instantiate an SI unit 'of' a SIDimensions.
49       * @param siDimensions the SI dimensions
50       * @return the SIUnit based on the SI dimensionality
51       */
52      public static SIUnit of(final SIDimensions siDimensions)
53      {
54          return Unit.lookupOrCreateUnitWithSIDimensions(siDimensions);
55      }
56  
57      @Override
58      public String toString()
59      {
60          return getQuantity().getSiDimensions().toString(true, false);
61      }
62  
63      @Override
64      @SuppressWarnings("checkstyle:designforextension")
65      public int hashCode()
66      {
67          return getQuantity().getSiDimensions().hashCode();
68      }
69  
70      @Override
71      @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
72      public boolean equals(final Object obj)
73      {
74          if (this == obj)
75              return true;
76          if (obj == null)
77              return false;
78          if (getClass() != obj.getClass())
79              return false;
80          SIUnit other = (SIUnit) obj;
81          if (!getQuantity().getSiDimensions().equals(other.getQuantity().getSiDimensions()))
82              return false;
83          return true;
84      }
85  
86  }