Speed.java
package org.djunits.quantity;
import org.djunits.quantity.def.Quantity;
import org.djunits.unit.AbstractUnit;
import org.djunits.unit.UnitInterface;
import org.djunits.unit.UnitRuntimeException;
import org.djunits.unit.Unitless;
import org.djunits.unit.scale.IdentityScale;
import org.djunits.unit.scale.LinearScale;
import org.djunits.unit.scale.Scale;
import org.djunits.unit.si.SIPrefix;
import org.djunits.unit.si.SIUnit;
import org.djunits.unit.system.UnitSystem;
/**
* Speed is the rate of change of position over time.
* <p>
* Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
* for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
* distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
* @author Alexander Verbraeck
*/
public class Speed extends Quantity<Speed>
{
/** Constant with value zero. */
public static final Speed ZERO = ofSi(0.0);
/** Constant with value one. */
public static final Speed ONE = ofSi(1.0);
/** Constant with value NaN. */
@SuppressWarnings("checkstyle:constantname")
public static final Speed NaN = ofSi(Double.NaN);
/** Constant with value POSITIVE_INFINITY. */
public static final Speed POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
/** Constant with value NEGATIVE_INFINITY. */
public static final Speed NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
/** Constant with value MAX_VALUE. */
public static final Speed POS_MAXVALUE = ofSi(Double.MAX_VALUE);
/** Constant with value -MAX_VALUE. */
public static final Speed NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
/** */
private static final long serialVersionUID = 600L;
/**
* Instantiate a Speed quantity with an SI or base value and a display unit.
* @param value the quantity value expressed in the SI or base unit
* @param displayUnit the display unit to use
* @param useSi use SI value when true, use value in unit when false
*/
public Speed(final double value, final Speed.Unit displayUnit, final boolean useSi)
{
super(value, displayUnit, useSi);
}
/**
* Instantiate a Speed quantity expressed in the given unit.
* @param valueInUnit the quantity value expressed in the given unit
* @param unit the unit of the value, also acts as the display unit
*/
public Speed(final double valueInUnit, final Speed.Unit unit)
{
this(valueInUnit, unit, false);
}
/**
* Return a Speed instance based on an SI value.
* @param si the si value
* @return the Speed instance based on an SI value
*/
public static Speed ofSi(final double si)
{
return new Speed(si, Speed.Unit.SI, true);
}
/**
* Instantiate a Speed quantity with an SI or base value and a display unit.
* @param siValue the quantity value expressed in the SI or base unit
* @param displayUnit the display unit to use
* @return the Speed instance based on an SI value with the given display unit
*/
public static Speed ofSi(final double siValue, final Speed.Unit displayUnit)
{
return new Speed(siValue, displayUnit, true);
}
@Override
public Speed instantiateSi(final double siValue, final UnitInterface<Speed> displayUnit)
{
return new Speed(siValue, (Speed.Unit) displayUnit, true);
}
/**
* Returns a Speed representation of a textual representation of a value with a unit. The String representation that can be
* parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
* but not required, between the value and the unit.
* @param text the textual representation to parse into a Speed
* @return the Scalar representation of the value in its unit
* @throws IllegalArgumentException when the text cannot be parsed
* @throws NullPointerException when the text argument is null
*/
public static Speed valueOf(final String text)
{
return Quantity.valueOf(text, ZERO);
}
/**
* Returns a Speed based on a value expressed in the unit.
* @param valueInUnit the value, expressed in the given unit
* @param unit the unit of the value, also acts as the display unit
* @return ab Speed representation of the value in its unit
*/
public static Speed of(final double valueInUnit, final Speed.Unit unit)
{
return new Speed(valueInUnit, unit);
}
/**
* Returns a Speed based on a value and the textual representation of the unit, which can be localized.
* @param valueInUnit the value, expressed in the unit as given by unitString
* @param unitString the textual representation of the unit
* @return the Scalar representation of the value in its unit
* @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
* @throws NullPointerException when the unitString argument is null
*/
public static Speed of(final double valueInUnit, final String unitString)
{
return Quantity.of(valueInUnit, unitString, ZERO);
}
@Override
public Speed.Unit getDisplayUnit()
{
return (Speed.Unit) super.getDisplayUnit();
}
/**
* Calculate the division of Speed and Speed, which results in a Dimensionless scalar.
* @param v scalar
* @return scalar as a division of Speed and Speed
*/
public Dimensionless divide(final Speed v)
{
return new Dimensionless(this.si() / v.si(), Unitless.BASE);
}
/**
* Calculate the multiplication of Speed and Area, which results in a FlowVolume scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and Area
*/
public FlowVolume multiply(final Area v)
{
return new FlowVolume(this.si() * v.si(), FlowVolume.Unit.SI);
}
/**
* Calculate the multiplication of Speed and Force, which results in a Power scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and Force
*/
public Power multiply(final Force v)
{
return new Power(this.si() * v.si(), Power.Unit.SI);
}
/**
* Calculate the multiplication of Speed and Frequency, which results in a Acceleration scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and Frequency
*/
public Acceleration multiply(final Frequency v)
{
return new Acceleration(this.si() * v.si(), Acceleration.Unit.SI);
}
/**
* Calculate the division of Speed and Length, which results in a Frequency scalar.
* @param v scalar
* @return scalar as a division of Speed and Length
*/
public Frequency divide(final Length v)
{
return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
}
/**
* Calculate the division of Speed and Frequency, which results in a Length scalar.
* @param v scalar
* @return scalar as a division of Speed and Frequency
*/
public Length divide(final Frequency v)
{
return new Length(this.si() / v.si(), Length.Unit.SI);
}
/**
* Calculate the multiplication of Speed and LinearObjectDensity, which results in a Frequency scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and LinearObjectDensity
*/
public Frequency multiply(final LinearObjectDensity v)
{
return new Frequency(this.si() * v.si(), Frequency.Unit.SI);
}
/**
* Calculate the multiplication of Speed and Duration, which results in a Length scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and Duration
*/
public Length multiply(final Duration v)
{
return new Length(this.si() * v.si(), Length.Unit.SI);
}
/**
* Calculate the division of Speed and Duration, which results in a Acceleration scalar.
* @param v scalar
* @return scalar as a division of Speed and Duration
*/
public Acceleration divide(final Duration v)
{
return new Acceleration(this.si() / v.si(), Acceleration.Unit.SI);
}
/**
* Calculate the division of Speed and Acceleration, which results in a Duration scalar.
* @param v scalar
* @return scalar as a division of Speed and Acceleration
*/
public Duration divide(final Acceleration v)
{
return new Duration(this.si() / v.si(), Duration.Unit.SI);
}
/**
* Calculate the multiplication of Speed and FlowMass, which results in a Force scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and FlowMass
*/
public Force multiply(final FlowMass v)
{
return new Force(this.si() * v.si(), Force.Unit.SI);
}
/**
* Calculate the multiplication of Speed and Mass, which results in a Momentum scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and Mass
*/
public Momentum multiply(final Mass v)
{
return new Momentum(this.si() * v.si(), Momentum.Unit.SI);
}
/**
* Calculate the multiplication of Speed and Momentum, which results in a Energy scalar.
* @param v scalar
* @return scalar as a multiplication of Speed and Momentum
*/
public Energy multiply(final Momentum v)
{
return new Energy(this.si() * v.si(), Energy.Unit.SI);
}
/******************************************************************************************************/
/********************************************** UNIT CLASS ********************************************/
/******************************************************************************************************/
/**
* Speed.Unit encodes the units of the rate of change of a position over time.
* <p>
* Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
* See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
* distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
* @author Alexander Verbraeck
*/
@SuppressWarnings("checkstyle:constantname")
public static class Unit extends AbstractUnit<Speed>
{
/** The dimensions of Speed: m/s. */
public static final SIUnit SI_UNIT = SIUnit.of("m/s");
/** Meter per second. */
public static final Speed.Unit m_s =
new Speed.Unit("m/s", "m/s", "meter per second", IdentityScale.SCALE, UnitSystem.SI_DERIVED, null);
/** The SI or BASE unit. */
public static final Speed.Unit SI = m_s;
/** m/h. */
public static final Speed.Unit m_h =
new Speed.Unit("m/h", "m/h", "meter per hour", new LinearScale(1.0, 3600.0), UnitSystem.SI_ACCEPTED, null);
/** km/s. */
public static final Speed.Unit km_s =
new Speed.Unit("km/s", "km/s", "kilometer per second", new LinearScale(1000.0), UnitSystem.SI_ACCEPTED, null);
/** km/h. */
public static final Speed.Unit km_h = new Speed.Unit("km/h", "km/h", "kilometer per hour",
new LinearScale(1000.0, 3600.0), UnitSystem.SI_ACCEPTED, null);
/** in/s. */
public static final Speed.Unit in_s = new Speed.Unit("in/s", "in/s", "inch per second",
new LinearScale(Length.Unit.CONST_IN), UnitSystem.IMPERIAL, null);
/** in/min. */
public static final Speed.Unit in_min = new Speed.Unit("in/min", "in/min", "inch per minute",
new LinearScale(Length.Unit.CONST_IN, 60.0), UnitSystem.IMPERIAL, null);
/** in/h. */
public static final Speed.Unit in_h = new Speed.Unit("in/h", "in/h", "inch per hour",
new LinearScale(Length.Unit.CONST_IN, 3600.0), UnitSystem.IMPERIAL, null);
/** ft/s. */
public static final Speed.Unit ft_s = new Speed.Unit("ft/s", "ft/s", "foot per second",
new LinearScale(Length.Unit.CONST_FT), UnitSystem.IMPERIAL, null);
/** ft/min. */
public static final Speed.Unit ft_min = new Speed.Unit("ft/min", "ft/min", "foot per minute",
new LinearScale(Length.Unit.CONST_FT, 60.0), UnitSystem.IMPERIAL, null);
/** ft/h. */
public static final Speed.Unit ft_h = new Speed.Unit("ft/h", "ft/h", "foot per hour",
new LinearScale(Length.Unit.CONST_FT, 3600.0), UnitSystem.IMPERIAL, null);
/** mi/s. */
public static final Speed.Unit mi_s = new Speed.Unit("mi/s", "mi/s", "mile per second",
new LinearScale(Length.Unit.CONST_MI), UnitSystem.IMPERIAL, null);
/** mi/min. */
public static final Speed.Unit mi_min = new Speed.Unit("mi/min", "mi/min", "mile per minute",
new LinearScale(Length.Unit.CONST_MI, 60.0), UnitSystem.IMPERIAL, null);
/** mi/h. */
public static final Speed.Unit mi_h = new Speed.Unit("mi/h", "mi/h", "mile per hour",
new LinearScale(Length.Unit.CONST_MI, 3600.0), UnitSystem.IMPERIAL, null);
/** knot = Nautical Mile per hour. */
public static final Speed.Unit kt = new Speed.Unit("kt", "knot", Length.Unit.CONST_NM / 3600.0, UnitSystem.OTHER);
/**
* Create a new Speed unit.
* @param id the id or main abbreviation of the unit
* @param name the full name of the unit
* @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
* @param unitSystem the unit system such as SI or IMPERIAL
*/
public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
{
super(id, name, scaleFactorToBaseUnit, unitSystem);
}
/**
* Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
* @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
* @param displayAbbreviation the display abbreviation of the unit
* @param name the full name of the unit
* @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
* @param unitSystem unit system, e.g. SI or Imperial
* @param siPrefix the SI Prefix of this unit
*/
public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
final UnitSystem unitSystem, final SIPrefix siPrefix)
{
super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
}
@Override
public SIUnit siUnit()
{
return SI_UNIT;
}
@Override
public Unit getBaseUnit()
{
return SI;
}
@Override
public Speed ofSi(final double si, final UnitInterface<Speed> displayUnit)
{
return new Speed(si, (Unit) displayUnit, true);
}
@Override
public Speed.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
final UnitSystem unitSystem)
{
return (Speed.Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
}
@Override
public Speed.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
{
if (getScale() instanceof LinearScale ls)
{
return new Speed.Unit(textualAbbreviation, displayAbbreviation, name,
new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
}
throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
}
}
}