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