public final class UnitSpan extends Object implements Comparable<UnitSpan>
Instances of this class are immutable. All change operations
return a new instance of this UnitSpan class.
| Modifier and Type | Class and Description |
|---|---|
static class |
UnitSpan.ParseData
A class returned by the validatingParse() method.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
CM_250K
Unit code for 250,000 units per cm.
|
static int |
INCHES_1M
Unit code for 1,000,000 units per inch.
|
static int |
INCHES_72K
Unit code for 72,000 units per inch.
|
static int |
MILLIPOINT
Unit code for 1,000 units per point (72,000 units per inch).
|
static int |
MM_25K
Unit code for 25,000 units per mm.
|
static int |
PICA_PT_10K
Deprecated.
Unit deprecated. The unit is not accurate and does not actually
handle picas.
|
static int |
PICA_PT_1K
Deprecated.
Unit deprecated in favour of
POINTS_1K. |
static int |
PICAS_12K
Unit code for 12,000 units per pica (72,000 units per inch).
|
static int |
POINTS_1K
Unit code for 1,000 units per point (72,000 units per inch).
|
static int |
UNIT_MASK |
static int |
UNIT_UNKNOWN
Unit code for units unknown.
|
static int |
UNITS_CM_250K |
static int |
UNITS_INCHES_1M |
static int |
UNITS_INCHES_72K |
static int |
UNITS_MILLIPOINT |
static int |
UNITS_MM_25K |
static int |
UNITS_PICAS_12K |
static int |
UNITS_POINTS_1K |
static UnitSpan |
ZERO |
| Constructor and Description |
|---|
UnitSpan()
Instantiates a
UnitSpan with the value 0 and
default units INCHES_72K. |
UnitSpan(double dValue,
int eUnits)
Instantiates a
UnitSpan with the given double value and units. |
UnitSpan(int nValue)
Instantiates a
UnitSpan with the given int value and unknown units. |
UnitSpan(int eUnits,
int nValue)
Instantiates a
UnitSpan with the given units and optional
value. |
UnitSpan(int eNewUnits,
int eOldUnits,
int nOldValue)
Instantiates a
UnitSpan with the given units and a value
equal to the given value after it has been converted from it's old units
to the new units. |
UnitSpan(String sText)
Instantiates a
UnitSpan with a value and unit code parsed
from the given text. |
UnitSpan(String sText,
int eDefaultUnits,
boolean bDefaultValuePerUnit)
Instantiates a
UnitSpan with a value and unit code parsed
from the given text. |
UnitSpan(UnitSpan source)
Deprecated.
UnitSpan is immutable, so there is no need to copy an instance.
|
| Modifier and Type | Method and Description |
|---|---|
UnitSpan |
abs()
Returns a
UnitSpan representing
the absolute value of this UnitSpan. |
UnitSpan |
add(UnitSpan add)
Returns a
UnitSpan representing the
addition of this object and the given UnitSpan. |
UnitSpan |
changeUnits(int eUnits)
Returns a
UnitSpan representing the
change of units of this object to the given unit code. |
int |
compareTo(UnitSpan compare)
Compares this object to the given
UnitSpan. |
static int |
convertUnit(int eNewUnits,
int eOldUnits,
int nValue) |
static int |
defaultUnits()
Gets the default unit code for all
UnitSpans. |
UnitSpan |
divide(int nDivisor)
Returns a
UnitSpan representing the
division of this object's value by the given int value. |
double |
divide(UnitSpan divisor)
Returns a
UnitSpan representing the
division of this object's value by the given UnitSpan. |
boolean |
equals(Object object)
Determines if this object is equal to the given
Object. |
UnitSpan |
grid(UnitSpan grid)
Returns a
UnitSpan representing this
UnitSpan's value snaped to the nearest
grid coordinate of the given UnitSpan. |
boolean |
gt(UnitSpan compare)
Determines if this object is greater than the given
UnitSpan. |
boolean |
gte(UnitSpan compare)
Determines if this object is greater than or equal to the given.
|
int |
hashCode() |
boolean |
lt(UnitSpan compare)
Determines if this object is less than the given
UnitSpan. |
boolean |
lte(UnitSpan compare)
Determines if this object is less than or equal to the given
UnitSpan. |
static boolean |
match(UnitSpan u1,
UnitSpan u2)
Compares two
UnitSpan(s) for equality, allowing for null arguments. |
static String |
measurementValidate(String sText,
boolean negValid) |
UnitSpan |
multiply(double dScale)
Returns a
UnitSpan representing the
multiplication of this object's value by the given double value. |
UnitSpan |
multiply(int nScale)
Returns a
UnitSpan representing the
multiplication of this object's value by the given int value. |
UnitSpan |
round(UnitSpan round)
Returns a
UnitSpan representing the
rounding of this object's value to the given UnitSpan. |
static int |
stringToUnit(String sUnit,
int eDefaultUnits)
Converts the given unit string to a unit code.
|
UnitSpan |
subtract(UnitSpan subtract)
Returns a
UnitSpan representing the
subtraction of this object's value from the given UnitSpan. |
String |
text(int nPrecision,
boolean bTruncate,
boolean bValuePerUnits)
Returns a
String containing this object's value converted
followed by its units, formatted according to the given arguments. |
String |
toString()
Returns a
String containing this object's value
followed by its units. |
int |
units()
Gets this object's units.
|
int |
unitsPerInch()
Gets the units per inch for this object's unit code.
|
static int |
unitsPerInch(int eUnit)
Gets the units per inch for the given unit code.
|
static String |
unitToString(int eUnit)
Converts the given unit code to a string.
|
static int |
unitToValue(double dValue,
int eUnit)
Converts a unit measure to a unit value; e.g.
|
static UnitSpan.ParseData |
validatingParse(String sText,
int eDefaultUnits,
boolean bAllowNegative,
boolean bForceValuePerUnit,
boolean bAllowPercent)
Parses a value with strict validation.
|
int |
value()
Gets this object's value.
|
int |
valueAsUnit(int eUnits)
Gets this object's value converted to the given unit code.
|
static double |
valueToUnit(int nValue,
int eUnit)
Converts the given value to the given unit code.
|
static UnitSpan |
zero()
The zero unit span.
|
public static final int INCHES_1M
public static final int CM_250K
public static final int INCHES_72K
public static final int MM_25K
public static final int POINTS_1K
public static final int PICA_PT_1K
POINTS_1K.public static final int MILLIPOINT
public static final int PICA_PT_10K
public static final int PICAS_12K
public static final int UNIT_MASK
public static final int UNIT_UNKNOWN
public static final int UNITS_CM_250K
public static final int UNITS_INCHES_1M
public static final int UNITS_INCHES_72K
public static final int UNITS_MILLIPOINT
public static final int UNITS_MM_25K
public static final int UNITS_POINTS_1K
public static final int UNITS_PICAS_12K
public static final UnitSpan ZERO
public UnitSpan()
UnitSpan with the value 0 and
default units INCHES_72K.zero()public UnitSpan(double dValue,
int eUnits)
UnitSpan with the given double value and units.dValue - the value of the unit span.eUnits - the unit code of the unit span.public UnitSpan(int nValue)
UnitSpan with the given int value and unknown units.
The unit code will be UNIT_UNKNOWN.nValue - the value of the unit span.public UnitSpan(int eUnits,
int nValue)
UnitSpan with the given units and optional
value.eUnits - the unit code of the unit span.nValue - the value of the unit span.public UnitSpan(int eNewUnits,
int eOldUnits,
int nOldValue)
UnitSpan with the given units and a value
equal to the given value after it has been converted from it's old units
to the new units.eNewUnits - the unit code of the unit span to which the value will
be converted.eOldUnits - the unit code from which the value will be converted.nOldValue - the value to be converted and set as this object's value.public UnitSpan(String sText)
UnitSpan with a value and unit code parsed
from the given text.sText - text containing a numeric value possibly followed by a unit
string of "in", "inches", "cm", "centimeters", "pt", "points",
"picas", "mm", "millimeters", "mp", or "millipoints".
If no unit is specified, this object's unit will
default to UNIT_UNKNOWN.public UnitSpan(String sText, int eDefaultUnits, boolean bDefaultValuePerUnit)
UnitSpan with a value and unit code parsed
from the given text.sText - text containing a numeric value possibly followed by a unit
string of "in", "inches", "cm", "centimeters", "pt", "points",
"picas", "mm", "millimeters", "mp" or "millipoints".eDefaultUnits - unit to use if the string contains no unit specification.bDefaultValuePerUnit - a default interpretation of the value if no unit string is
found.public UnitSpan(UnitSpan source)
UnitSpan from the given UnitSpan.source - the UnitSpan to copy to this object.public UnitSpan changeUnits(int eUnits)
UnitSpan representing the
change of units of this object to the given unit code.eUnits - the new unit code.public static int convertUnit(int eNewUnits,
int eOldUnits,
int nValue)
public static int defaultUnits()
UnitSpans.INCHES_72K.public static int stringToUnit(String sUnit, int eDefaultUnits)
sUnit - the string to be converted.eDefaultUnits - the default unit code.public static int unitsPerInch(int eUnit)
public static String unitToString(int eUnit)
eUnit - the unit code to be converted.public static int unitToValue(double dValue,
int eUnit)
public static double valueToUnit(int nValue,
int eUnit)
nValue - the unit value to be converted.eUnit - the unit code to be converted to.public static UnitSpan zero()
public UnitSpan abs()
UnitSpan representing
the absolute value of this UnitSpan.public UnitSpan add(UnitSpan add)
UnitSpan representing the
addition of this object and the given UnitSpan.
The given UnitSpan's value is converted
to this object's units for the operation.add - the UnitSpan to add.public UnitSpan divide(int nDivisor)
UnitSpan representing the
division of this object's value by the given int value.
The resulting value is rounded to 0 decimal places.nDivisor - the divisor.public double divide(UnitSpan divisor)
UnitSpan representing the
division of this object's value by the given UnitSpan.
The given UnitSpan's value is converted to this object's
units for the operation.divisor - the divisor.public boolean equals(Object object)
Object.
Comparisons with instances of non-UnitSpan objects are never equal.public UnitSpan grid(UnitSpan grid)
UnitSpan representing this
UnitSpan's value snaped to the nearest
grid coordinate of the given UnitSpan.
The given UnitSpan's value is converted to this object's
units for the calculation.
Positive grid values will "move" this object's value to the left (or down), and negative grid values will "move" this object's value to the right (or up).
The algorithm used to "snap" this object's value is (<y/x> ∗ x) where y is this value, x is the grid size, and <> is the floor function; this is best explained with examples:
grid = 5, pt = 7 and (<7/5> * 5) = 1 * 5 = 5 (moved to left) grid = 5, pt = -7 and (<-7/5> * * 5) = -2 * 5 = -10 (moved to left) grid = -5, pt = 7 and (<7/-5> * -5) = -2 * -5 = 10 (moved to right) grid = -5, pt = * -7 and (<-7/-5> * -5) = 1 * -5 = -5 (moved to right)
grid - the grid coordinatepublic boolean gt(UnitSpan compare)
UnitSpan.
The given UnitSpan's value is converted to this object's
units for the comparison.compare - the UnitSpan to compare.public boolean gte(UnitSpan compare)
UnitSpan. The given UnitSpan's value is
converted to this object's units for the comparison.compare - the UnitSpan to compare.public boolean lt(UnitSpan compare)
UnitSpan.
The given UnitSpan's value is converted to this object's
units for the comparison.compare - the UnitSpan to compare.public boolean lte(UnitSpan compare)
UnitSpan. The given UnitSpan's value is
converted to this object's units for the comparison.compare - the UnitSpan to compare.public UnitSpan multiply(double dScale)
UnitSpan representing the
multiplication of this object's value by the given double value.
The resulting value is rounded to 0 decimal places.dScale - the multiplier.public UnitSpan multiply(int nScale)
UnitSpan representing the
multiplication of this object's value by the given int value.nScale - the multiplier.public UnitSpan round(UnitSpan round)
UnitSpan representing the
rounding of this object's value to the given UnitSpan.
Positive and negative round values have the same effect.
This value is rounded by adding one to the quotient (subtracting one for negative quotients) of [y / |x|] if the remainder of [y / |x|] is >= [x/2] or <= [-x/2] where y is this object's value, and x is the round value. Again this is best explained with examples:
rnd = 5, pt = 7, quo = 1, rem = 2, rem / rnd < 0.5 and pt = quo * rnd = 5 rnd = 5, pt = 8, quo = 1, rem = 3, rem / rnd > 0.5 and pt = (quo + 1) * rnd = 10 rnd = 5, pt = -7, quo = -1, rem = 2, rem / rnd < 0.5 and pt = quo * rnd = -5 rnd = 5, pt = -8, quo = -1, rem = 2, rem / rnd > 0.5 and pt = (quo - 1) * rnd = -10
round - the rounding coordinate.public UnitSpan subtract(UnitSpan subtract)
UnitSpan representing the
subtraction of this object's value from the given UnitSpan.
The given
UnitSpan's value is converted to this object's units for
the operation.subtract - the UnitSpan to subtract.public String text(int nPrecision, boolean bTruncate, boolean bValuePerUnits)
String containing this object's value converted
followed by its units, formatted according to the given arguments.nPrecision - the number of decimal places to which the value will be
calculated.bTruncate - If false, the decimal portion will be padded out with 0's up
to the number specified by nPrecision. If true, '0'
placeholders will be suppressed.bValuePerUnits - indicates the number should be expressed in quantity per unit,
i.e., lines per inch.public String toString()
String containing this object's value
followed by its units.public int units()
public int unitsPerInch()
public static UnitSpan.ParseData validatingParse(String sText, int eDefaultUnits, boolean bAllowNegative, boolean bForceValuePerUnit, boolean bAllowPercent)
Given a string representing a value with optional unit specification, this method parses the string content and returns results.
The caller can invoke this method to parse specifically for supported UnitSpan types, or it can call it as a more general-purpose value and unit parser for caller-recognized types. It can also use a single call for both purposes.
The method returns flag indicating whether the parse succeeded or failed. The parse will fail if the string is not in the general format of a value with optional units. If the string does fit the format, but the unit type is not recognized, the call succeeds, and the structure indicates that the caller may choose to validate the units. Alternatively, the caller may treat this situation as an error if it is expecting only UnitSpan units.
Values in the structure are populated as described below. These values are relevant only if the parse succeeds. If the string contains recognized UnitSpan units, only two fields in the structure are relevant:
If the units are not recognized or are expressed as a percentage (and the parse otherwise succeeds), the caller may need to look at all fields in the structure.
sText - Text to parse.eDefaultUnits - (optional) Default units to use if the string
does not contain a unit specification. The default is UNIT_UNKNOWN,
which cause the result of DefaultUnit() to be used.bAllowNegative - (optional) True if negative values are
allowed. If false, a negative value will be considered an error.
The default is false.bForceValuePerUnit - (optional) True if the result is to be
treated as value per unit even if the slash character is not present.
The default is false.bAllowPercent - (optional) True if the percent character is to
be allowed as a unit type. The default is false.public int value()
public int valueAsUnit(int eUnits)
eUnits - the unit code.public static boolean match(UnitSpan u1, UnitSpan u2)
UnitSpan(s) for equality, allowing for null arguments.u1 - the first UnitSpan to compare.u2 - the second UnitSpan to compare.public int compareTo(UnitSpan compare)
UnitSpan.
The given UnitSpan's value is converted to this object's
units for the comparison.compareTo in interface Comparable<UnitSpan>compare - the UnitSpan to compare.Copyright © 2010 - 2020 Adobe. All Rights Reserved