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