Class ValueBase<D extends Dimension,I extends DimensionedValue<D>>
- Type Parameters:
D
- The dimension.I
- The implementing type.
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<DimensionedValue<D>>
,Formattable
,DimensionedValue<D>
- Direct Known Subclasses:
AreaValue
,DataSizeValue
,EnergyValue
,ForceValue
,LengthValue
,MassValue
,PowerValue
,PressureValue
,SpeedValue
,TemperatureValue
,TimeValue
,VolumeValue
An abstract base implementation for the interface
DimensionedValue
that is intended as the base for concrete implementations of value
types.
The validator
argument of the constructors is an instance of
BiPredicate
that takes the
unit
with that the instance is initialised, and the value, converted to the base
unit. It returns true
if the given combination is valid, otherwise
it returns false
.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: ValueBase.java 1105 2024-02-28 12:58:46Z tquadrat $
- Since:
- 0.0.4
- See Also:
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.value.api.ValueBase"
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final BiPredicate
The default validator.private D
The unit for the value.private final BiPredicate
<D, BigDecimal> The validation function.private final BigDecimal
The numerical value for this instance.private static final long
The serial version UID for objects of this class: -2075489505691464486L.Fields inherited from interface org.tquadrat.foundation.value.api.DimensionedValue
MATH_CONTEXT
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ValueBase
(D unit, String value, BiPredicate<D, BigDecimal> validator) Creates a newValueBase
instance.protected
ValueBase
(D unit, BigDecimal value, BiPredicate<D, BigDecimal> validator) Creates a newValueBase
instance.protected
ValueBase
(D unit, N value, BiPredicate<D, BigDecimal> validator) Creates a newValueBase
instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal BigDecimal
Returns the base value (this value, converted to the base unit).clone()
Creates a new copy of this value.final I
copy()
Creates a new copy of this value.final I
Creates a new copy of this value.final boolean
final D
getUnit()
Returns the unit for the value.final int
hashCode()
final void
Applies another unit for the value.final String
toString()
Returns the String representation for this valueprivate final BigDecimal
validate
(BigDecimal value) Validates the given value, based on the type of the dimension.
-
Field Details
-
Constructor Details
-
ValueBase
Creates a newValueBase
instance.- Parameters:
unit
- The unit.value
- The value; only absolute (positive) values are allowed, a sign will be stripped.validator
- The validator function; can benull
.
-
ValueBase
protected ValueBase(D unit, String value, BiPredicate<D, BigDecimal> validator) throws NumberFormatExceptionCreates a newValueBase
instance.- Parameters:
unit
- The unit.value
- The value; it must be possible to parse the given String into aBigDecimal
.validator
- The validator function.- Throws:
NumberFormatException
- The provided value cannot be converted into aBigDecimal
.
-
ValueBase
Creates a newValueBase
instance.- Type Parameters:
N
- The type ofvalue
.- Parameters:
unit
- The unit.value
- The value.validator
- The validator function.
-
-
Method Details
-
baseValue
Returns the base value (this value, converted to the base unit).
According to the result, this is the same as calling
.convert( baseUnit() );
- Specified by:
baseValue
in interfaceDimensionedValue<D extends Dimension>
- Returns:
- The numerical value as for the base unit.
- See Also:
-
clone
Creates a new copy of this value. -
copy
Creates a new copy of this value.- Specified by:
copy
in interfaceDimensionedValue<D extends Dimension>
- Returns:
- The copy.
- See Also:
-
copy
Creates a new copy of this value.- Specified by:
copy
in interfaceDimensionedValue<D extends Dimension>
- Parameters:
unit
- The unit for the new copy.- Returns:
- The copy.
- See Also:
-
equals
Two instances of a class implementing this interface are equals if they are of the same class and if their values, converted to the base dimension, are equals.
-
getUnit
Returns the unit for the value.- Specified by:
getUnit
in interfaceDimensionedValue<D extends Dimension>
- Returns:
- The unit.
-
hashCode
The hash code is based on the base value and base unit only.
-
setUnit
Applies another unit for the value. This does not affect the results ofDimensionedValue.equals(Object)
,DimensionedValue.hashCode()
} andDimensionedValue.compareTo(DimensionedValue)
, nor that ofDimensionedValue.baseValue()
.- Specified by:
setUnit
in interfaceDimensionedValue<D extends Dimension>
- Parameters:
dimension
- The new unit.
-
toString
Returns the String representation for this value; usually, this is in the format
<numerical value> <unit symbol>
like "
4.5 m
".The precision for the mantissa is provided by the unit.
If more control over the output format is required, see
DimensionedValue.toString(int, int)
. -
validate
Validates the given value, based on the type of the dimension.
This method will be called by the constructors always with the base unit value just before the value will be initialised.
- Parameters:
value
- The value in base units.- Returns:
- The value.
- Throws:
IllegalArgumentException
- The validation failed.
-