public class NumberWithUnit extends Object implements Serializable
A class that combines a value with a unit.
This class can be used to work with values that have an associated unit. For
instance spaces in a layout can be defined in arbitrary units like pixels,
dialog units, inches or centimeters. To support this, an instance of this
class stores a value (as a floating point number) and a reference to a
Unit
object. This Unit
object is also used
to perform conversions of the stored value to the default unit pixels.
Instances of this class are immutable. Once created, they cannot be changed any more. Thus they can be shared between multiple threads.
Modifier and Type | Field and Description |
---|---|
static NumberWithUnit |
ZERO
Constant for the special value zero.
|
Constructor and Description |
---|
NumberWithUnit(double val,
Unit unit)
Creates a new instance of
NumberWithUnit and initializes it. |
NumberWithUnit(int val)
Creates a new instance of
NumberWithUnit with the given
value and the unit pixels. |
NumberWithUnit(String s)
Creates a new instance of
NumberWithUnit and initializes it from
the given string representation. |
Modifier and Type | Method and Description |
---|---|
void |
buildUnitString(StringBuilder buf)
Appends a string representation of this number and its unit to the given
string buffer.
|
boolean |
equals(Object obj)
Compares two objects.
|
Unit |
getUnit()
Returns the unit of this number.
|
double |
getValue()
Returns the numeric value.
|
int |
hashCode()
Returns a hash code for this object.
|
static NumberWithUnit |
nonNull(NumberWithUnit n)
A convenience method for performing checks for null values.
|
int |
toPixel(UnitSizeHandler handler,
Object comp,
boolean y)
Converts this number into a pixel value.
|
String |
toString()
Returns a string representation of this object.
|
String |
toUnitString()
Returns a string representation for the stored value and the unit.
|
public static final NumberWithUnit ZERO
public NumberWithUnit(int val)
NumberWithUnit
with the given
value and the unit pixels.val
- the valuepublic NumberWithUnit(double val, Unit unit)
NumberWithUnit
and initializes it.val
- the numeric valueunit
- the unit (must not be null )public NumberWithUnit(String s)
NumberWithUnit
and initializes it from
the given string representation. The string passed to this method must
start with a valid double number. Then after optional whitespace the name
of the unit must follow. If no unit is provided, pixel is assumed. Valid
strings are for instance "10", "10cm", "10.5 cm".s
- the string to be parsed (must not be null)IllegalArgumentException
- if the passed in string is not a valid
unit stringtoUnitString()
public final double getValue()
public final Unit getUnit()
public int toPixel(UnitSizeHandler handler, Object comp, boolean y)
Unit
object.handler
- the size handlercomp
- the componenty
- flag for X or Y directionUnit.toPixel(double, UnitSizeHandler, Object, boolean)
public void buildUnitString(StringBuilder buf)
toUnitString()
, but the string is directly
appended to the given buffer.buf
- the string buffer (must not be null)IllegalArgumentException
- if the buffer is nullpublic String toUnitString()
public String toString()
public int hashCode()
public boolean equals(Object obj)
public static NumberWithUnit nonNull(NumberWithUnit n)
n
- the source numberCopyright © 2016 The JGUIraffe Team. All rights reserved.