View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.Unitless;
5   import org.djunits.unit.Units;
6   import org.djunits.unit.si.SIUnit;
7   
8   /**
9    * Dimensionless quantity.
10   * <p>
11   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
13   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
14   * @author Alexander Verbraeck
15   */
16  public class Dimensionless extends Quantity<Dimensionless>
17  {
18      /** Constant with value zero. */
19      public static final Dimensionless ZERO = Dimensionless.ofSi(0.0);
20  
21      /** Constant with value one. */
22      public static final Dimensionless ONE = Dimensionless.ofSi(1.0);
23  
24      /** Constant with value NaN. */
25      @SuppressWarnings("checkstyle:constantname")
26      public static final Dimensionless NaN = Dimensionless.ofSi(Double.NaN);
27  
28      /** Constant with value POSITIVE_INFINITY. */
29      public static final Dimensionless POSITIVE_INFINITY = Dimensionless.ofSi(Double.POSITIVE_INFINITY);
30  
31      /** Constant with value NEGATIVE_INFINITY. */
32      public static final Dimensionless NEGATIVE_INFINITY = Dimensionless.ofSi(Double.NEGATIVE_INFINITY);
33  
34      /** Constant with value MAX_VALUE. */
35      public static final Dimensionless POS_MAXVALUE = Dimensionless.ofSi(Double.MAX_VALUE);
36  
37      /** Constant with value -MAX_VALUE. */
38      public static final Dimensionless NEG_MAXVALUE = Dimensionless.ofSi(-Double.MAX_VALUE);
39  
40      /** */
41      private static final long serialVersionUID = 600L;
42  
43      /**
44       * Instantiate a Dimensionless quantity with a unit.
45       * @param valueInUnit the value, expressed in the unit
46       * @param unit the unit in which the value is expressed
47       */
48      public Dimensionless(final double valueInUnit, final Unitless unit)
49      {
50          super(valueInUnit, unit);
51      }
52  
53      /**
54       * Instantiate a Dimensionless quantity with a unit, expressed as a String.
55       * @param valueInUnit the value, expressed in the unit
56       * @param abbreviation the String abbreviation of the unit in which the value is expressed
57       */
58      public Dimensionless(final double valueInUnit, final String abbreviation)
59      {
60          this(valueInUnit, Units.resolve(Unitless.class, abbreviation));
61      }
62  
63      /**
64       * Construct Dimensionless quantity.
65       * @param value Scalar from which to construct this instance
66       */
67      public Dimensionless(final Dimensionless value)
68      {
69          super(value.si(), Unitless.BASE);
70          setDisplayUnit(value.getDisplayUnit());
71      }
72  
73      /**
74       * Return a Dimensionless instance based on an SI value.
75       * @param si the si value
76       * @return the Dimensionless instance based on an SI value
77       */
78      public static Dimensionless ofSi(final double si)
79      {
80          return new Dimensionless(si, Unitless.BASE);
81      }
82  
83      @Override
84      public Dimensionless instantiateSi(final double si)
85      {
86          return ofSi(si);
87      }
88  
89      @Override
90      public SIUnit siUnit()
91      {
92          return Unitless.SI_UNIT;
93      }
94  
95      /**
96       * Returns a Dimensionless representation of a textual representation of a value with a unit. The String representation that
97       * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
98       * allowed, but not required, between the value and the unit.
99       * @param text the textual representation to parse into a Dimensionless
100      * @return the Scalar representation of the value in its unit
101      * @throws IllegalArgumentException when the text cannot be parsed
102      * @throws NullPointerException when the text argument is null
103      */
104     public static Dimensionless valueOf(final String text)
105     {
106         return Quantity.valueOf(text, ZERO);
107     }
108 
109     /**
110      * Returns a Dimensionless based on a value and the textual representation of the unit, which can be localized.
111      * @param valueInUnit the value, expressed in the unit as given by unitString
112      * @param unitString the textual representation of the unit
113      * @return the Scalar representation of the value in its unit
114      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
115      * @throws NullPointerException when the unitString argument is null
116      */
117     public static Dimensionless of(final double valueInUnit, final String unitString)
118     {
119         return Quantity.of(valueInUnit, unitString, ZERO);
120     }
121 
122     @Override
123     public Unitless getDisplayUnit()
124     {
125         return (Unitless) super.getDisplayUnit();
126     }
127 
128     /**
129      * Calculate the division of Dimensionless and Dimensionless, which results in a Dimensionless quantity.
130      * @param v quantity
131      * @return quantity as a division of Dimensionless and Dimensionless
132      */
133     public final Dimensionless divide(final Dimensionless v)
134     {
135         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
136     }
137 
138     /**
139      * Calculate the division of Dimensionless and Length, which results in a LinearObjectDensity scalar.
140      * @param v scalar
141      * @return scalar as a division of Dimensionless and Length
142      */
143     public final LinearObjectDensity divide(final Length v)
144     {
145         return new LinearObjectDensity(this.si() / v.si(), LinearObjectDensity.Unit.SI);
146     }
147 
148     /**
149      * Calculate the division of Dimensionless and LinearObjectDensity, which results in a Length scalar.
150      * @param v scalar
151      * @return scalar as a division of Dimensionless and LinearObjectDensity
152      */
153     public final Length divide(final LinearObjectDensity v)
154     {
155         return new Length(this.si() / v.si(), Length.Unit.SI);
156     }
157 
158     /**
159      * Calculate the division of Dimensionless and Duration, which results in a Frequency scalar.
160      * @param v scalar
161      * @return scalar as a division of Dimensionless and Duration
162      */
163     public final Frequency divide(final Duration v)
164     {
165         return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
166     }
167 
168     /**
169      * Calculate the division of Dimensionless and Frequency, which results in a Duration scalar.
170      * @param v scalar
171      * @return scalar as a division of Dimensionless and Frequency
172      */
173     public final Duration divide(final Frequency v)
174     {
175         return new Duration(this.si() / v.si(), Duration.Unit.SI);
176     }
177 
178     /**
179      * Calculate the division of Dimensionless and ElectricalConductance, which results in a ElectricalResistance scalar.
180      * @param v scalar
181      * @return scalar as a division of Dimensionless and ElectricalConductance
182      */
183     public final ElectricalResistance divide(final ElectricalConductance v)
184     {
185         return new ElectricalResistance(this.si() / v.si(), ElectricalResistance.Unit.SI);
186     }
187 
188     /**
189      * Calculate the division of Dimensionless and ElectricalResistance, which results in a ElectricalConductance scalar.
190      * @param v scalar
191      * @return scalar as a division of Dimensionless and ElectricalResistance
192      */
193     public final ElectricalConductance divide(final ElectricalResistance v)
194     {
195         return new ElectricalConductance(this.si() / v.si(), ElectricalConductance.Unit.SI);
196     }
197 
198     @Override
199     public Dimensionless reciprocal()
200     {
201         return Dimensionless.ofSi(1.0 / this.si());
202     }
203 
204 }