Package com.pdftools.geometry.units
Class Length
- java.lang.Object
-
- com.pdftools.geometry.units.Length
-
- All Implemented Interfaces:
java.lang.Comparable<Length>
public class Length extends java.lang.Object implements java.lang.Comparable<Length>
Class that represents a measurable length. Provides conversion between units, parsing of string representations of lengths, and basic mathematical operations.
The unit used in PDF documents is
Units.POINT
, which is also used internally by theLength
object to store length values. For that reason, when converting units to and from the internally used unit, minor numerical differences may occur due to floating-point arithmetic.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Length.Units
-
Constructor Summary
Constructors Constructor Description Length(double value, Length.Units unit)
Constructs a newly allocatedLength
object using the specified value and unit.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Length
abs(Length length)
Length
add(Length addend)
int
compareTo(Length o)
Length
divide(double divisor)
double
divide(Length divisor)
boolean
equals(java.lang.Object obj)
int
hashCode()
static Length
max(Length lhs, Length rhs)
static Length
min(Length lhs, Length rhs)
Length
multiply(double multiplicand)
static Length
parse(java.lang.String value)
Creates aLength
object by parsing a String representation of a length with its unit.static Length[]
parseArray(java.lang.String value)
Creates an array ofLength
by parsing a String representation of lengths with units.static Length[]
parseArray(java.lang.String value, int size)
Creates an array ofLength
by parsing a String representation of lengths with units.Length
subtract(Length subtrahend)
double
to(Length.Units unit)
Get the numerical length value of the specified unit.java.lang.String
toString()
Creates a string representation with an associated suitable metric unit, "m", "cm" or "mm".java.lang.String
toString(Length.Units unit)
Creates a string representation with the specified unit.
-
-
-
Constructor Detail
-
Length
public Length(double value, Length.Units unit)
Constructs a newly allocatedLength
object using the specified value and unit.- Parameters:
value
- the numerical value.unit
- the unit.
-
-
Method Detail
-
to
public double to(Length.Units unit)
Get the numerical length value of the specified unit.- Parameters:
unit
- the unit.- Returns:
-
parse
public static Length parse(java.lang.String value)
Creates aLength
object by parsing a String representation of a length with its unit.- Parameters:
value
- Value-unit pair of the form "<value><unit>". Examples: "12.3cm" or "23.9mm". Allowed units are "um", "mm", "cm", "m", "km", "pt", and "in".- Throws:
java.lang.IllegalArgumentException
- if unit given invalue
is invalid.
-
toString
public java.lang.String toString()
Creates a string representation with an associated suitable metric unit, "m", "cm" or "mm".- Overrides:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(Length.Units unit)
Creates a string representation with the specified unit.
-
parseArray
public static Length[] parseArray(java.lang.String value, int size)
Creates an array ofLength
by parsing a String representation of lengths with units.- Parameters:
value
- A group of value-unit pairs of the form "<value1><unit1> <value2><unit2>...". Example: "12.3cm 23.9mm 0.25in" etc. Allowed units are "um", "mm", "cm", "m", "km", "pt" and "in".size
- Number of value-unit pairs represented by thevalue
parameter.- Throws:
java.lang.IllegalArgumentException
- if parametersize
doesn't fit the number of values invalue
.java.lang.IllegalArgumentException
- if a unit is invalid.
-
parseArray
public static Length[] parseArray(java.lang.String value)
Creates an array ofLength
by parsing a String representation of lengths with units.- Parameters:
value
- A group of value-unit pairs of the form "<value1><unit1> <value2><unit2>...". Example: "12.3cm 23.9mm 0.25in" etc. Allowed units are "um", "mm", "cm", "m", "km", "pt" and "in".- Throws:
java.lang.IllegalArgumentException
- if a unit is invalid.
-
multiply
public Length multiply(double multiplicand)
-
divide
public Length divide(double divisor)
-
divide
public double divide(Length divisor)
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-