View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.AbstractUnit;
5   import org.djunits.unit.UnitInterface;
6   import org.djunits.unit.UnitRuntimeException;
7   import org.djunits.unit.Unitless;
8   import org.djunits.unit.scale.LinearScale;
9   import org.djunits.unit.scale.Scale;
10  import org.djunits.unit.si.SIPrefix;
11  import org.djunits.unit.si.SIPrefixes;
12  import org.djunits.unit.si.SIUnit;
13  import org.djunits.unit.system.UnitSystem;
14  
15  /**
16   * Absorbed dose is the energy deposited by ionizing radiation per unit mass, measured in grays (Gy).
17   * <p>
18   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
19   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
20   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
21   * @author Alexander Verbraeck
22   */
23  public class AbsorbedDose extends Quantity<AbsorbedDose>
24  {
25      /** Constant with value zero. */
26      public static final AbsorbedDose ZERO = ofSi(0.0);
27  
28      /** Constant with value one. */
29      public static final AbsorbedDose ONE = ofSi(1.0);
30  
31      /** Constant with value NaN. */
32      @SuppressWarnings("checkstyle:constantname")
33      public static final AbsorbedDose NaN = ofSi(Double.NaN);
34  
35      /** Constant with value POSITIVE_INFINITY. */
36      public static final AbsorbedDose POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
37  
38      /** Constant with value NEGATIVE_INFINITY. */
39      public static final AbsorbedDose NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
40  
41      /** Constant with value MAX_VALUE. */
42      public static final AbsorbedDose POS_MAXVALUE = ofSi(Double.MAX_VALUE);
43  
44      /** Constant with value -MAX_VALUE. */
45      public static final AbsorbedDose NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
46  
47      /** */
48      private static final long serialVersionUID = 600L;
49  
50      /**
51       * Instantiate a AbsorbedDose quantity with an SI or base value and a display unit.
52       * @param value the quantity value expressed in the SI or base unit
53       * @param displayUnit the display unit to use
54       * @param useSi use SI value when true, use value in unit when false
55       */
56      public AbsorbedDose(final double value, final AbsorbedDose.Unit displayUnit, final boolean useSi)
57      {
58          super(value, displayUnit, useSi);
59      }
60  
61      /**
62       * Instantiate a AbsorbedDose quantity expressed in the given unit.
63       * @param valueInUnit the quantity value expressed in the given unit
64       * @param unit the unit of the value, also acts as the display unit
65       */
66      public AbsorbedDose(final double valueInUnit, final AbsorbedDose.Unit unit)
67      {
68          this(valueInUnit, unit, false);
69      }
70  
71      /**
72       * Return a AbsorbedDose instance based on an SI value.
73       * @param si the si value
74       * @return the AbsorbedDose instance based on an SI value
75       */
76      public static AbsorbedDose ofSi(final double si)
77      {
78          return new AbsorbedDose(si, AbsorbedDose.Unit.SI, true);
79      }
80  
81      /**
82       * Instantiate a AbsorbedDose quantity with an SI or base value and a display unit.
83       * @param siValue the quantity value expressed in the SI or base unit
84       * @param displayUnit the display unit to use
85       * @return the AbsorbedDose instance based on an SI value with the given display unit
86       */
87      public static AbsorbedDose ofSi(final double siValue, final AbsorbedDose.Unit displayUnit)
88      {
89          return new AbsorbedDose(siValue, displayUnit, true);
90      }
91  
92      @Override
93      public AbsorbedDose instantiateSi(final double siValue, final UnitInterface<AbsorbedDose> displayUnit)
94      {
95          return new AbsorbedDose(siValue, (AbsorbedDose.Unit) displayUnit, true);
96      }
97  
98      /**
99       * Returns a AbsorbedDose representation of a textual representation of a value with a unit. The String representation that
100      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
101      * allowed, but not required, between the value and the unit.
102      * @param text the textual representation to parse into a AbsorbedDose
103      * @return the Scalar representation of the value in its unit
104      * @throws IllegalArgumentException when the text cannot be parsed
105      * @throws NullPointerException when the text argument is null
106      */
107     public static AbsorbedDose valueOf(final String text)
108     {
109         return Quantity.valueOf(text, ZERO);
110     }
111 
112     /**
113      * Returns a AbsorbedDose based on a value expressed in the unit.
114      * @param valueInUnit the value, expressed in the given unit
115      * @param unit the unit of the value, also acts as the display unit
116      * @return ab AbsorbedDose representation of the value in its unit
117      */
118     public static AbsorbedDose of(final double valueInUnit, final AbsorbedDose.Unit unit)
119     {
120         return new AbsorbedDose(valueInUnit, unit);
121     }
122 
123     /**
124      * Returns a AbsorbedDose based on a value and the textual representation of the unit, which can be localized.
125      * @param valueInUnit the value, expressed in the unit as given by unitString
126      * @param unitString the textual representation of the unit
127      * @return the Scalar representation of the value in its unit
128      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
129      * @throws NullPointerException when the unitString argument is null
130      */
131     public static AbsorbedDose of(final double valueInUnit, final String unitString)
132     {
133         return Quantity.of(valueInUnit, unitString, ZERO);
134     }
135 
136     @Override
137     public AbsorbedDose.Unit getDisplayUnit()
138     {
139         return (AbsorbedDose.Unit) super.getDisplayUnit();
140     }
141 
142     /**
143      * Calculate the division of AbsorbedDose and AbsorbedDose, which results in a Dimensionless quantity.
144      * @param v quantity
145      * @return quantity as a division of AbsorbedDose and AbsorbedDose
146      */
147     public Dimensionless divide(final AbsorbedDose v)
148     {
149         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
150     }
151 
152     /******************************************************************************************************/
153     /********************************************** UNIT CLASS ********************************************/
154     /******************************************************************************************************/
155 
156     /**
157      * AbsorbedDose.Unit encodes the units of absorbed dose (of ionizing radiation).
158      * <p>
159      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
160      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
161      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
162      * @author Alexander Verbraeck
163      */
164     @SuppressWarnings("checkstyle:constantname")
165     public static class Unit extends AbstractUnit<AbsorbedDose>
166     {
167         /** The dimensions of the absorbed dose: m2/s2. */
168         public static final SIUnit SI_UNIT = SIUnit.of("m2/s2");
169 
170         /** Gray. */
171         public static final AbsorbedDose.Unit Gy = new AbsorbedDose.Unit("Gy", "gray", 1.0, UnitSystem.SI_DERIVED);
172 
173         /** The SI or BASE unit. */
174         public static final AbsorbedDose.Unit SI = Gy;
175 
176         /** mGy. */
177         public static final AbsorbedDose.Unit mGy =
178                 Gy.deriveUnit("mGy", "mGy", "milligray", 1.0E-3, UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefix("m"));
179 
180         /** &#181;Gy. */
181         public static final AbsorbedDose.Unit muGy =
182                 Gy.deriveUnit("muGy", "\u03BCGy", "microgray", 1.0E-6, UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefix("mu"));
183 
184         /** erg/g. */
185         public static final AbsorbedDose.Unit erg_g = new AbsorbedDose.Unit("erg/g", "erg per gram", 1.0E-4, UnitSystem.CGS);
186 
187         /** rad. */
188         public static final AbsorbedDose.Unit rad = new AbsorbedDose.Unit("rad", "rad", 1.0E-2, UnitSystem.CGS);
189 
190         /**
191          * Create a new AbsorbedDose unit.
192          * @param id the id or main abbreviation of the unit
193          * @param name the full name of the unit
194          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
195          * @param unitSystem the unit system such as SI or IMPERIAL
196          */
197         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
198         {
199             super(id, name, scaleFactorToBaseUnit, unitSystem);
200         }
201 
202         /**
203          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
204          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
205          * @param displayAbbreviation the display abbreviation of the unit
206          * @param name the full name of the unit
207          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
208          * @param unitSystem unit system, e.g. SI or Imperial
209          * @param siPrefix the SI Prefix of this unit
210          */
211         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
212                 final UnitSystem unitSystem, final SIPrefix siPrefix)
213         {
214             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
215         }
216 
217         @Override
218         public SIUnit siUnit()
219         {
220             return SI_UNIT;
221         }
222 
223         @Override
224         public Unit getBaseUnit()
225         {
226             return SI;
227         }
228 
229         @Override
230         public AbsorbedDose ofSi(final double si, final UnitInterface<AbsorbedDose> displayUnit)
231         {
232             return new AbsorbedDose(si, (Unit) displayUnit, true);
233         }
234 
235         @Override
236         public AbsorbedDose.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
237                 final UnitSystem unitSystem)
238         {
239             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
240         }
241 
242         @Override
243         public AbsorbedDose.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation,
244                 final String name, final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
245         {
246             if (getScale() instanceof LinearScale ls)
247             {
248                 return new AbsorbedDose.Unit(textualAbbreviation, displayAbbreviation, name,
249                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
250             }
251             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
252         }
253 
254     }
255 
256 }