public final class PhysicalLength extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<PhysicalLength>
This is an immutable class.
| Modifier and Type | Field and Description |
|---|---|
static PhysicalLength |
NULL
Null length.
|
static java.lang.String |
UNIT_FORMAT
The format for printing units.
|
| Constructor and Description |
|---|
PhysicalLength(double value,
LengthUnit unit)
Create a new physical length.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(PhysicalLength o)
Compares this object with the specified object for order.
|
PhysicalLength |
dividedBy(double divider)
Get a divided part of this physical length.
|
double |
dividedBy(PhysicalLength physicalLength)
Get the scaling between two physical lengths.
|
boolean |
equals(java.lang.Object o)
Is this length equal to another one?
|
boolean |
equals(PhysicalLength other,
PhysicalLength epsilon)
Is this length nearly equal to another one?
|
static PhysicalLength |
fromString(java.lang.String descr)
Decodes a physical length description in text form,using the default locale.
|
static PhysicalLength |
fromString(java.lang.String descr,
java.util.Locale locale)
Decodes a physical length description in text form.
|
static PhysicalLength |
ft(double feet)
Factory method for lengths in foot.
|
double |
getLength()
Get the length.
|
double |
getLength(LengthUnit targetUnit)
Get the length in an arbitrary unit.
|
double |
getLengthInMeter()
Get the length in meter.
|
LengthUnit |
getUnit()
Get the length unit.
|
int |
hashCode()
Getr a useful hashcode.
|
static PhysicalLength |
in(double inches)
Factory method for lengths in inches.
|
PhysicalLength |
in(LengthUnit targetUnit)
Get this length in another unit.
|
static PhysicalLength |
m(double meter)
Factory method for lengths in meter.
|
static void |
main(java.lang.String[] args) |
PhysicalLength |
minus(PhysicalLength another)
Get the difference of this physical length and another.
|
static PhysicalLength |
mm(double millimeter)
Factory method for lengths in millimeter.
|
PhysicalLength |
plus(PhysicalLength another)
Get the sum of this physical length and another.
|
PhysicalLength |
times(double scale)
Get a scaled physical length.
|
java.lang.String |
toString()
Return a string representation of this length.
|
java.lang.String |
toString(LengthUnit targetUnit)
Return a string representation of this length using the given unit.
|
java.lang.String |
toString(LengthUnit targetUnit,
java.util.Locale locale)
Return a string representation of this length using the given unit.
|
java.lang.String |
toString(java.util.Locale locale)
Return a string representation of this length.
|
@NotNull public static final java.lang.String UNIT_FORMAT
@NotNull public static final PhysicalLength NULL
public PhysicalLength(double value,
@NotNull
LengthUnit unit)
value - length valueunit - length unitpublic LengthUnit getUnit()
public double getLengthInMeter()
public double getLength()
unitpublic double getLength(@NotNull LengthUnit targetUnit)
targetUnit - length unit for return valuepublic java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String toString(@NotNull LengthUnit targetUnit)
targetUnit - length unitpublic java.lang.String toString(@NotNull java.util.Locale locale)
locale - locale to use for number outputpublic java.lang.String toString(@NotNull LengthUnit targetUnit, @NotNull java.util.Locale locale)
targetUnit - length unitlocale - locale to use for number outputpublic boolean equals(@NotNull PhysicalLength other, @NotNull PhysicalLength epsilon)
other - length unit to compare toepsilon - the maximal error allowedtrue if the lengths are nearly equal, otherwise falsepublic boolean equals(java.lang.Object o)
equals in class java.lang.Objecto - object to compare totrue if the lengths are equals, otherwise falsepublic int hashCode()
hashCode in class java.lang.Object@NotNull public PhysicalLength times(double scale)
scale - scaling factor@NotNull public PhysicalLength dividedBy(double divider)
divider - dividerpublic double dividedBy(@NotNull PhysicalLength physicalLength)
physicalLength - other length@NotNull public PhysicalLength plus(@NotNull PhysicalLength another)
another - another physical length@NotNull public PhysicalLength minus(@NotNull PhysicalLength another)
another - another physical length@NotNull public PhysicalLength in(@NotNull LengthUnit targetUnit)
targetUnit - other unitpublic int compareTo(@NotNull PhysicalLength o)
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)
The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.
Finally, the implementor must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
compareTo in interface java.lang.Comparable<PhysicalLength>o - the object to be compared.java.lang.NullPointerException - if the specified object is nulljava.lang.ClassCastException - if the specified object's type prevents it
from being compared to this object.@NotNull public static PhysicalLength fromString(@NotNull java.lang.String descr, @Nullable java.util.Locale locale) throws IllegalPhysicalLengthFormatException
descr - text description containing a physical lengthlocale - locale to use for number decodingIllegalPhysicalLengthFormatException - if the string does not contain a valid physical lengthjava.lang.NullPointerException - if descr is null@NotNull public static PhysicalLength fromString(@NotNull java.lang.String descr) throws IllegalPhysicalLengthFormatException
descr - text description containing a physical lengthIllegalPhysicalLengthFormatException - if the string does not contain a valid physical lengthjava.lang.NullPointerException - if descr is null@NotNull public static PhysicalLength in(double inches)
inches - number of inches@NotNull public static PhysicalLength ft(double feet)
feet - number of feet@NotNull public static PhysicalLength m(double meter)
meter - number of meters@NotNull public static PhysicalLength mm(double millimeter)
millimeter - number of millimeterspublic static void main(java.lang.String[] args)