public final class NumericalValue extends AbstractTerm
The unit is optional and is any keyword directly following the number value, such as px, em, or ms. The sign is also optional,
and is only defined if explicitly included in the source. In other words, in "5px" the sign will not be NumericalValue.Sign.POSITIVE
but an empty Optional
.
To dynamically create a NumericalValue
use on of the constructor methods, for example:
NumericalValue number = NumericalValue.of(10, "px");
NumericalValueParser
Modifier and Type | Class and Description |
---|---|
static class |
NumericalValue.Sign
Represents the sign of the number (+/-)
|
Constructor and Description |
---|
NumericalValue(double value)
Constructs a new
NumericalValue instance (used for dynamically created Syntax units). |
NumericalValue(int value)
Constructs a new
NumericalValue instance (used for dynamically created Syntax units). |
NumericalValue(int line,
int column,
String raw)
Constructs a new
NumericalValue instance with the given raw value. |
Modifier and Type | Method and Description |
---|---|
NumericalValue |
copy()
For implementations: do not copy comments or orphaned comments, instead be sure to call
AbstractSyntax.copiedFrom(Syntax) on the
new copy. |
double |
doubleValue()
Gets the numerical value as a double.
|
Optional<NumericalValue.Sign> |
explicitSign()
Gets the explicit sign of the number.
|
NumericalValue |
explicitSign(NumericalValue.Sign sign)
Sets the explicit sign of the number.
|
int |
intValue()
Gets the numerical value as an integer.
|
boolean |
isNegative()
Gets whether a sign is present and if the sign is negative.
|
static NumericalValue |
of(double doubleValue)
Creates a new
NumericalValue instance with the given double value. |
static NumericalValue |
of(double doubleValue,
String unit)
Creates a new
NumericalValue instance with the given double value and unit. |
static NumericalValue |
of(int value)
Creates a new
NumericalValue instance with the given integer value. |
static NumericalValue |
of(int integerValue,
String unit)
Creates a new
NumericalValue instance with the given integer value and unit. |
static NumericalValue |
of(String value)
Creates a new
NumericalValue instance with the given raw value. |
static NumericalValue |
of(String raw,
String unit)
Creates a new
NumericalValue instance with the given raw value. |
String |
textualValue()
Returns the sign (if explicitly present), the number, and the unit (if present).
|
Optional<String> |
unit()
Gets the unit.
|
NumericalValue |
unit(String unit)
Sets the unit, e.g., px or em.
|
String |
value()
Gets the numerical value as a string.
|
NumericalValue |
value(double value)
Sets the numerical value with a decimal point.
|
NumericalValue |
value(int value)
Sets the numerical value.
|
void |
write(StyleWriter writer,
StyleAppendable appendable)
Outputs this
Writable 's string representation. |
declaration, isTerm, self
append, destroy, group, group, isDestroyed, isFirst, isLast, isWritable, next, parent, prepend, previous, replaceWith, unlink
annotate, annotateUnlessPresent, annotation, annotations, column, comment, comment, comments, comments, comments, copiedFrom, equals, hasAnnotation, hasAnnotation, hashCode, hasSourcePosition, id, line, orphanedComments, orphanedComments, orphanedComments, propagateBroadcast, shouldBreakBroadcast, status, status, toString, toString, writesOwnComments, writesOwnOrphanedComments
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
append, destroy, group, group, isDestroyed, isFirst, isLast, next, parent, prepend, previous, replaceWith, unlink
annotate, annotateUnlessPresent, annotation, annotations, column, comment, comment, comments, comments, comments, hasAnnotation, hasAnnotation, hasSourcePosition, id, line, orphanedComments, orphanedComments, orphanedComments, toString, writesOwnComments, writesOwnOrphanedComments
isWritable
propagateBroadcast, shouldBreakBroadcast, status, status
public NumericalValue(int line, int column, String raw)
NumericalValue
instance with the given raw value.line
- The line number.column
- The column number.raw
- The number. Must not include the sign or the unit.public NumericalValue(int value)
NumericalValue
instance (used for dynamically created Syntax
units).value
- The numerical value.public NumericalValue(double value)
NumericalValue
instance (used for dynamically created Syntax
units).
Please note that large integer or floating-point values may result in unexpected precision and/or rounding errors.
value
- The numerical value.public NumericalValue value(int value)
value
- The new numerical value.public NumericalValue value(double value)
Please note that large integer or floating-point values may result in unexpected precision and/or rounding errors.
value
- The new numerical value.public String value()
Note that if the value is negative, it will not be shown here, see explicitSign()
, or for mathematical operations
see intValue()
or doubleValue()
.
For math operations, doubleValue()
is available instead.
public double doubleValue()
Note that this may result in an exception if the current string value is too large for a double. This performs a calculation so cache the result if using more than once.
public int intValue()
doubleValue()
instead unless you are ok with
discarding any present decimal value. This performs a calculation so cache the result if using more than once.public NumericalValue unit(String unit)
unit
- The unit.public Optional<String> unit()
Optional
if not present.public NumericalValue explicitSign(NumericalValue.Sign sign)
sign
- The sign.public Optional<NumericalValue.Sign> explicitSign()
Optional
if not present.public boolean isNegative()
public String textualValue()
public void write(StyleWriter writer, StyleAppendable appendable) throws IOException
Writable
Writable
's string representation.
Important notes for implementation:
Do not use the StyleWriter
in an attempt to write direct content (Strings, chars, etc...). Use the StyleAppendable
.
The StyleWriter
should be used to make decisions based on writer settings (e.g., compressed vs. verbose output
mode), as well as for writing inner or child Writable
s. Do not call the this method method directly on inner
or child Writable
s! That would bypass any overrides that are set on the StyleWriter
. Use StyleWriter.writeInner(Writable, StyleAppendable)
instead.
writer
- Writer to use for output settings and for writing inner Writable
s.appendable
- Append direct content to this StyleAppendable
.IOException
- If an I/O error occurs.public NumericalValue copy()
AbstractSyntax
AbstractSyntax.copiedFrom(Syntax)
on the
new copy.copy
in interface PropertyValueMember
copy
in interface Term
copy
in interface Syntax
copy
in class AbstractSyntax
public static NumericalValue of(int value)
value
- The value.NumericalValue
instance.public static NumericalValue of(double doubleValue)
NumericalValue
instance with the given double value.
Please note that large integer or floating-point values may result in unexpected precision and/or rounding errors.
Example:
NumericalValue.of(10.5)
doubleValue
- The floating-point value.NumericalValue
instance.public static NumericalValue of(String value)
NumericalValue
instance with the given raw value. Generally this should not be preferred unless using
one of the other constructor methods would result in precision errors.
Example:
NumericalValue.of("1000000000.0000000009")
value
- The raw numerical value. Must not contain the sign or unit.NumericalValue
instance.public static NumericalValue of(int integerValue, String unit)
NumericalValue
instance with the given integer value and unit.
Example:
NumericalValue.of(10, "px")
integerValue
- The integer value.unit
- The unit, e.g., px or em.NumericalValue
instance.public static NumericalValue of(double doubleValue, String unit)
NumericalValue
instance with the given double value and unit.
Please note that large integer or floating-point values may result in unexpected precision and/or rounding errors.
Example:
NumericalValue.of(10.5, "px")
doubleValue
- The floating-point value.unit
- The unit, e.g., px or em.NumericalValue
instance.public static NumericalValue of(String raw, String unit)
NumericalValue
instance with the given raw value. Generally this should not be preferred unless using
one of the other constructor methods would result in precision errors.
Example:
NumericalValue.of("1000000000.0000000009", "px")
raw
- The raw numerical value. Must not contain the sign or unit.unit
- The unit, e.g., px or em.NumericalValue
instance.Copyright (c) 2019, Salesforce.com, Inc. All rights reserved. Licensed under the BSD 3-Clause license. For full license text, see the LICENSE file in the repository.