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-2023 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      static
20      {
21          // make sure all predefined unit types get registered before we start using SIScalars.
22          try
23          {
24              Class.forName("org.djunits.unit.util.UNITS");
25          }
26          catch (ClassNotFoundException exception)
27          {
28              exception.printStackTrace();
29          }
30      }
31  
32      /**
33       * Instantiate an SI unit 'of' a String.
34       * @param siString String; the SI string, e.g., "kgm/s2"
35       * @return the SIUnit based on the SI dimensionality
36       * @throws UnitException when the SI string is not according to the rules
37       */
38      public static SIUnit of(final String siString) throws UnitException
39      {
40          return Unit.lookupOrCreateUnitWithSIDimensions(SIDimensions.of(siString));
41      }
42  
43      /**
44       * Instantiate an SI unit 'of' a SIDimensions.
45       * @param siDimensions SIDimensions; the SI dimensions
46       * @return the SIUnit based on the SI dimensionality
47       * @throws UnitException when the SI string is not according to the rules
48       */
49      public static SIUnit of(final SIDimensions siDimensions) throws UnitException
50      {
51          return Unit.lookupOrCreateUnitWithSIDimensions(siDimensions);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public String toString()
57      {
58          return getQuantity().getSiDimensions().toString(true, false);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      @SuppressWarnings("checkstyle:designforextension")
64      public int hashCode()
65      {
66          return getQuantity().getSiDimensions().hashCode();
67      }
68  
69      /** {@inheritDoc} */
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  }