package org.djunits.value.vfloat.scalar;
import java.util.regex.Matcher;
import javax.annotation.Generated;
import org.djunits.Throw;
import org.djunits.unit.*;
import org.djunits.unit.si.SIDimensions;
import org.djunits.unit.util.UnitRuntimeException;
import org.djunits.value.util.ValueUtil;
import org.djunits.value.vdouble.scalar.Dimensionless;
import org.djunits.value.vdouble.scalar.SIScalar;
import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRel;
import org.djunits.value.vdouble.scalar.base.DoubleScalar;
import org.djunits.value.vfloat.scalar.base.AbstractFloatScalarRel;
import org.djunits.value.vfloat.scalar.base.FloatScalar;
/**
* Easy access methods for the generic Relative SI FloatScalar.
*
* Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.
* All rights reserved.
* BSD-style license. See DJUNITS License.
*
* @author Alexander Verbraeck
* @author Peter Knoppers
*/
@Generated(value = "GenerateDJUNIT")
public class FloatSIScalar extends AbstractFloatScalarRel
{
/** */
private static final long serialVersionUID = 20150901L;
/**
* Construct SI scalar.
* @param value float; the float value
* @param unit SIUnit; unit for the float value
*/
public FloatSIScalar(final float value, final SIUnit unit)
{
super(value, unit);
}
/**
* Construct SI scalar.
* @param value FloatSIScalar; Scalar from which to construct this instance
*/
public FloatSIScalar(final FloatSIScalar value)
{
super(value);
}
/** {@inheritDoc} */
@Override
public final FloatSIScalar instantiateRel(final float value, final SIUnit unit)
{
return new FloatSIScalar(value, unit);
}
/**
* Construct SI scalar.
* @param value float; the float value in SI units
* @param unit SIUnit; the unit to use for the SI scalar
* @return FloatSIScalar; the new scalar with the SI value
*/
public static final FloatSIScalar instantiateSI(final float value, final SIUnit unit)
{
return new FloatSIScalar(value, unit);
}
/**
* Interpolate between two values.
* @param zero FloatSIScalar; the low value
* @param one FloatSIScalar; the high value
* @param ratio float; the ratio between 0 and 1, inclusive
* @return FloatSIScalar; a Scalar at the ratio between
*/
public static FloatSIScalar interpolate(final FloatSIScalar zero, final FloatSIScalar one, final float ratio)
{
return new FloatSIScalar(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
}
/**
* Return the maximum value of two relative scalars.
* @param r1 FloatSIScalar; the first scalar
* @param r2 FloatSIScalar; the second scalar
* @return FloatSIScalar; the maximum value of two relative scalars
*/
public static FloatSIScalar max(final FloatSIScalar r1, final FloatSIScalar r2)
{
return r1.gt(r2) ? r1 : r2;
}
/**
* Return the maximum value of more than two relative scalars.
* @param r1 FloatSIScalar; the first scalar
* @param r2 FloatSIScalar; the second scalar
* @param rn FloatSIScalar...; the other scalars
* @return FloatSIScalar; the maximum value of more than two relative scalars
*/
public static FloatSIScalar max(final FloatSIScalar r1, final FloatSIScalar r2, final FloatSIScalar... rn)
{
FloatSIScalar maxr = r1.gt(r2) ? r1 : r2;
for (FloatSIScalar r : rn)
{
if (r.gt(maxr))
{
maxr = r;
}
}
return maxr;
}
/**
* Return the minimum value of two relative scalars.
* @param r1 FloatSIScalar; the first scalar
* @param r2 FloatSIScalar; the second scalar
* @return FloatSIScalar; the minimum value of two relative scalars
*/
public static FloatSIScalar min(final FloatSIScalar r1, final FloatSIScalar r2)
{
return r1.lt(r2) ? r1 : r2;
}
/**
* Return the minimum value of more than two relative scalars.
* @param r1 FloatSIScalar; the first scalar
* @param r2 FloatSIScalar; the second scalar
* @param rn FloatSIScalar...; the other scalars
* @return FloatSIScalar; the minimum value of more than two relative scalars
*/
public static FloatSIScalar min(final FloatSIScalar r1, final FloatSIScalar r2, final FloatSIScalar... rn)
{
FloatSIScalar minr = r1.lt(r2) ? r1 : r2;
for (FloatSIScalar r : rn)
{
if (r.lt(minr))
{
minr = r;
}
}
return minr;
}
/** {@inheritDoc} */
@Override
public FloatSIScalar reciprocal()
{
return FloatScalar.divide(FloatDimensionless.ONE, this);
}
/**
* Returns an FloatSIScalar representation of a textual representation of a value with a unit. The String representation that can
* be parsed is the float value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
* required, between the value and the unit.
* @param text String; the textual representation to parse into a FloatSIScalar
* @return FloatSIScalar; 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 FloatSIScalar valueOf(final String text)
{
Throw.whenNull(text, "Error parsing FloatSIScalar: unitString is null");
Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatSIScalar: empty unitString");
Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
if (matcher.find())
{
int index = matcher.end();
try
{
String unitString = text.substring(index).trim();
String valueString = text.substring(0, index).trim();
SIUnit unit = Unit.lookupOrCreateUnitWithSIDimensions(SIDimensions.of(unitString));
if (unit != null)
{
float d = Float.parseFloat(valueString);
return new FloatSIScalar(d, unit);
}
}
catch (Exception exception)
{
throw new IllegalArgumentException("Error parsing FloatSIScalar from " + text, exception);
}
}
throw new IllegalArgumentException("Error parsing FloatSIScalar from " + text);
}
/**
* Returns an FloatSIScalar based on a value and the textual representation of the unit.
* @param value float; the value to use
* @param unitString String; the textual representation of the unit
* @return FloatSIScalar; 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 FloatSIScalar of(final float value, final String unitString)
{
Throw.whenNull(unitString, "Error parsing FloatSIScalar: unitString is null");
Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatSIScalar: empty unitString");
try
{
SIUnit unit = Unit.lookupOrCreateUnitWithSIDimensions(SIDimensions.of(unitString));
if (unit != null)
{
return new FloatSIScalar(value, unit);
}
}
catch (Exception exception)
{
throw new IllegalArgumentException("Error parsing SIUnit from " + unitString, exception);
}
throw new IllegalArgumentException("Error parsing FloatSIScalar with unit " + unitString);
}
/**********************************************************************************/
/******************************** 'CAST AS' METHODS *******************************/
/**********************************************************************************/
/**
* Return the current scalar transformed to a scalar in the given unit. Of course the SI dimensionality has to match,
* otherwise the scalar cannot be transformed. The compiler will check the alignment between the return value and the unit.
* @param displayUnit KU; the unit in which the scalar needs to be expressed
* @return S; the scalar that has been transformed into the right scalar type and unit
* @param the unit type
* @param the scalar type
*/
public final , S extends AbstractFloatScalarRel> S as(final U displayUnit)
{
Throw.when(!(getDisplayUnit().getQuantity().getSiDimensions().equals(displayUnit.getQuantity().getSiDimensions())),
UnitRuntimeException.class, "FloatSIScalar with unit %s cannot be converted to a scalar with unit %s", getDisplayUnit(),
displayUnit);
S result = FloatScalar.instantiate(this.si, displayUnit.getStandardUnit());
result.setDisplayUnit(displayUnit);
return result;
}
%%ASMETHODS%%
}