Package com.irurueta.units
Class MeasureFormatter<M extends Measurement<U>,U extends Enum<?>>
java.lang.Object
com.irurueta.units.MeasureFormatter<M,U>
- Type Parameters:
M- type of measurement (i.e. Distance or Surface).U- type of unit (i.e. DistanceUnit or SurfaceUnit).
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
AccelerationFormatter,AngleFormatter,AngularAccelerationFormatter,AngularSpeedFormatter,DistanceFormatter,FrequencyFormatter,MagneticFluxDensityFormatter,SpeedFormatter,SurfaceFormatter,TemperatureFormatter,TimeFormatter,VolumeFormatter,WeightFormatter
public abstract class MeasureFormatter<M extends Measurement<U>,U extends Enum<?>>
extends Object
implements Cloneable
Base class to format and parse a given measure using its value and unit.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault pattern to format values and units together into a single stringprivate MessageFormatInternal string formatter.private LocaleInternal locale.(package private) NumberFormatInternal string formatter.private StringPattern to format values and unit together into a single string. {0} corresponds to the value, {1} corresponds to the unit part. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected MeasureFormatter<M,U> clone()booleanDetermines if two measure formatters are equal by comparing all of its internal parameters.abstract UFinds measure unit from within a measurement string representation.Formats provided measurement value and unit into a string representation.format(double value, U unit, StringBuffer toAppendTo, FieldPosition pos) Formats provided measurement value and unit into a string representation and appends the result into provided string buffer.Formats provided measurement value and unit into a string representation.format(Number value, U unit, StringBuffer toAppendTo, FieldPosition pos) Formats provided measurement value and unit into a string representation and appends the result into provided string buffer.Formats provided measurement into a string representation.format(M measurement, StringBuffer toAppendTo, FieldPosition pos) Formats provided measurement into a string representation and appends the result into provided string buffer.formatAndConvert(double value, U unit) Formats and converts provided measurement value and unit using unit system assigned to locale of this instance (if no locale has been provided it is assumed that the system default locale is used).formatAndConvert(double value, U unit, UnitSystem system) Formats and converts provided measurement value and unit using provided unit system.formatAndConvert(Number value, U unit) Formats and converts provided measurement value and unit using unit system assigned to locale of this instance (if no locale has been provided it is assumed that the system default locale is used).abstract StringformatAndConvert(Number value, U unit, UnitSystem system) Formats and converts provided measurement value and unit using provided unit system.formatAndConvert(M measurement) Formats and converts provided measurement using unit system assigned to locale of this instance (if no locale has been provided it is assumed that the system default locale is used).formatAndConvert(M measurement, UnitSystem unitSystem) Formats and converts provided measurement using provided unit system.static Locale[]Returns available locales for this formatter.Gets locale assigned to this instance.intReturns maximum fraction digits to be shown when formatting a measure.intReturns maximum integer digits to be shown when formatting a measure.intReturns minimum fraction digits to be shown when formatting a measure.intReturns minimum integer digits to be shown when formatting a measure.Returns rounding mode to be used when formatting a measure.abstract StringgetUnitSymbol(U unit) Obtains measure unit symbol.Returns unit system this instance will use based on its assigned locale.abstract UnitSystemgetUnitSystem(String source) Gets unit system for detected unit into provided string representation of a measurement.Obtains pattern to format values and unit together into a single stringinthashCode()Hash code generated for this instance.(package private) MeasureFormatter<M,U> internalClone(MeasureFormatter<M, U> copy) Internal method to clone this measure formatter.(package private) MinternalParse(String source, M measure) Internal method to parse a string into a measure.booleanIndicates if grouping is used when formatting a measure.booleanisImperialUnit(String source) Indicates whether provided string representation of a measurement contains an imperial system unit.booleanisMetricUnit(String source) Indicates whether provided string representation of a measurement contains a metric system unit.booleanIndicates if only integer values are parsed.booleanisValidMeasurement(String source) Indicates whether provided string representation is a valid measurement representation or not.booleanisValidUnit(String source) Indicates whether provided string representation contains a valid measurement unit or not.abstract MParses a string into a measure.voidsetGroupingUsed(boolean newValue) Sets if grouping is used when formatting a measure.voidsetMaximumFractionDigits(int newValue) Sets maximum fraction digits to use when formatting a measure.voidsetMaximumIntegerDigits(int newValue) Sets maximum integer digits to use when formatting a measure.voidsetMinimumFractionDigits(int newValue) Sets minimum fraction digits to use when formatting a measure.voidsetMinimumIntegerDigits(int newValue) Sets minimum integer digits to use when formatting a measure.voidsetParseIntegerOnly(boolean value) Specifies whether only integer values are parsed or not.voidsetRoundingMode(RoundingMode roundingMode) Specifies rounding mode to use when formatting a measure.voidsetValueAndUnitFormatPattern(String valueAndUnitFormatPattern) Sets pattern to format values and unit together into a single string
-
Field Details
-
DEFAULT_VALUE_AND_UNIT_FORMAT_PATTERN
Default pattern to format values and units together into a single string. {0} corresponds to the value, {1} corresponds to the unit part.- See Also:
-
numberFormat
NumberFormat numberFormatInternal string formatter. -
format
Internal string formatter. -
locale
Internal locale. -
valueAndUnitFormatPattern
Pattern to format values and unit together into a single string. {0} corresponds to the value, {1} corresponds to the unit part.
-
-
Constructor Details
-
MeasureFormatter
MeasureFormatter()Constructor. -
MeasureFormatter
MeasureFormatter(Locale locale) Constructor with locale.- Parameters:
locale- locale.- Throws:
IllegalArgumentException- if locale is null.
-
-
Method Details
-
equals
Determines if two measure formatters are equal by comparing all of its internal parameters. -
hashCode
public int hashCode()Hash code generated for this instance. Hash codes can be internally used by some collections to coarsely compare objects. -
format
Formats provided measurement value and unit into a string representation.- Parameters:
value- a measurement value.unit- a measurement unit.- Returns:
- string representation of provided measurement value and unit.
-
format
Formats provided measurement value and unit into a string representation and appends the result into provided string buffer.- Parameters:
value- a measurement value.unit- a measurement unit.toAppendTo- buffer to append the result to.pos- field position where result will be appended.- Returns:
- provided string buffer where result is appended.
-
format
Formats provided measurement value and unit into a string representation.- Parameters:
value- a measurement value.unit- a measurement unit.- Returns:
- string representation of provided measurement value and unit.
-
format
Formats provided measurement value and unit into a string representation and appends the result into provided string buffer.- Parameters:
value- a measurement value.unit- a measurement unit.toAppendTo- buffer to append the result to.pos- field position where result will be appended.- Returns:
- provided string buffer where result is appended.
-
format
Formats provided measurement into a string representation.- Parameters:
measurement- a measurement.- Returns:
- string representation of provided measurement.
-
format
Formats provided measurement into a string representation and appends the result into provided string buffer.- Parameters:
measurement- a measurement.toAppendTo- buffer to append the result to.pos- field position where result will be appended.- Returns:
- provided string buffer where result is appended.
-
formatAndConvert
Formats and converts provided measurement value and unit using unit system assigned to locale of this instance (if no locale has been provided it is assumed that the system default locale is used). If provided value is too large for provided unit, this method will convert it to a more appropriate unit.- Parameters:
value- a measurement value.unit- a measurement unit.- Returns:
- a string representation of measurement value and unit.
-
formatAndConvert
Formats and converts provided measurement value and unit using unit system assigned to locale of this instance (if no locale has been provided it is assumed that the system default locale is used). If provided value is too large for provided unit, this method will convert it to a more appropriate unit.- Parameters:
value- a measurement value.unit- a measurement unit.- Returns:
- a string representation of measurement value and unit.
-
formatAndConvert
Formats and converts provided measurement using unit system assigned to locale of this instance (if no locale has been provided it is assumed that the system default locale is used). If provided measurement value is too large for its unit, this method will convert it to a more appropriate unit.- Parameters:
measurement- measurement to be formatted.- Returns:
- a string representation of measurement value and unit.
-
formatAndConvert
Formats and converts provided measurement value and unit using provided unit system. If provided value is too large for provided unit, this method will convert it to a more appropriate unit using provided unit system (either metric or imperial).- Parameters:
value- a measurement value.unit- a measurement unit.system- system unit to convert measurement to.- Returns:
- a string representation of measurement value and unit.
-
formatAndConvert
Formats and converts provided measurement value and unit using provided unit system. If provided value is too large for provided unit, this method will convert it to a more appropriate unit using provided unit system (either metric or imperial).- Parameters:
value- a measurement value.unit- a measurement unit.system- system unit to convert measurement to.- Returns:
- a string representation of measurement value and unit.
-
formatAndConvert
Formats and converts provided measurement using provided unit system. If provided measurement value is too large for its unit, this method will convert it to a more appropriate unit using provided unit system.- Parameters:
measurement- a measurement to be formatted.unitSystem- system unit to convert measurement to.- Returns:
- a string representation of measurement value and unit.
-
getAvailableLocales
Returns available locales for this formatter.- Returns:
- available locales.
-
getLocale
Gets locale assigned to this instance. Locale determines number format and unit system (metric or imperial) if not specified.- Returns:
- a locale.
-
getMaximumFractionDigits
public int getMaximumFractionDigits()Returns maximum fraction digits to be shown when formatting a measure.- Returns:
- maximum fraction digits.
-
setMaximumFractionDigits
public void setMaximumFractionDigits(int newValue) Sets maximum fraction digits to use when formatting a measure.- Parameters:
newValue- maximum fraction digits to be set.
-
getMaximumIntegerDigits
public int getMaximumIntegerDigits()Returns maximum integer digits to be shown when formatting a measure.- Returns:
- maximum integer digits.
-
setMaximumIntegerDigits
public void setMaximumIntegerDigits(int newValue) Sets maximum integer digits to use when formatting a measure.- Parameters:
newValue- maximum integer digits to be set.
-
getMinimumFractionDigits
public int getMinimumFractionDigits()Returns minimum fraction digits to be shown when formatting a measure.- Returns:
- minimum fraction digits.
-
setMinimumFractionDigits
public void setMinimumFractionDigits(int newValue) Sets minimum fraction digits to use when formatting a measure.- Parameters:
newValue- minimum fraction digits to be set.
-
getMinimumIntegerDigits
public int getMinimumIntegerDigits()Returns minimum integer digits to be shown when formatting a measure.- Returns:
- minimum integer digits.
-
setMinimumIntegerDigits
public void setMinimumIntegerDigits(int newValue) Sets minimum integer digits to use when formatting a measure.- Parameters:
newValue- minimum integer digits to be set.
-
getRoundingMode
Returns rounding mode to be used when formatting a measure.- Returns:
- rounding mode to be used when formatting a measure.
-
setRoundingMode
Specifies rounding mode to use when formatting a measure.- Parameters:
roundingMode- rounding mode to be set.
-
isGroupingUsed
public boolean isGroupingUsed()Indicates if grouping is used when formatting a measure.- Returns:
- true if grouping is used, false otherwise.
-
setGroupingUsed
public void setGroupingUsed(boolean newValue) Sets if grouping is used when formatting a measure.- Parameters:
newValue- true if grouping is enabled, false otherwise.
-
isParseIntegerOnly
public boolean isParseIntegerOnly()Indicates if only integer values are parsed.- Returns:
- true if only integer values are parsed, false otherwise.
-
setParseIntegerOnly
public void setParseIntegerOnly(boolean value) Specifies whether only integer values are parsed or not.- Parameters:
value- if true only integer values will be parsed.
-
getValueAndUnitFormatPattern
Obtains pattern to format values and unit together into a single string. {0} corresponds to the value, {1} corresponds to the unit part.- Returns:
- pattern to format values and unit together.
-
setValueAndUnitFormatPattern
Sets pattern to format values and unit together into a single string. {0} corresponds to the value, {1} corresponds to the unit part.- Parameters:
valueAndUnitFormatPattern- pattern to format values and unit together.- Throws:
IllegalArgumentException- if provided pattern is null.
-
getUnitSystem
Returns unit system this instance will use based on its assigned locale. Notice that if no locale was assigned, then the default system locale will be used.- Returns:
- unit system this instance will use.
-
isValidUnit
Indicates whether provided string representation contains a valid measurement unit or not.- Parameters:
source- a string measurement representation to be checked.- Returns:
- true if provided string has a valid (i.e. recognized) unit, false otherwise.
-
isValidMeasurement
Indicates whether provided string representation is a valid measurement representation or not.- Parameters:
source- a string measurement representation to be checked.- Returns:
- true if provided string representation is valid (contains valid value and unit), false otherwise.
-
isMetricUnit
Indicates whether provided string representation of a measurement contains a metric system unit.- Parameters:
source- a measurement string representation to be checked.- Returns:
- true if found unit is metric, false otherwise or if unit cannot be determined.
-
isImperialUnit
Indicates whether provided string representation of a measurement contains an imperial system unit.- Parameters:
source- a measurement string representation to be checked.- Returns:
- true if found unit is imperial, false otherwise or if unit cannot be determined.
-
getUnitSystem
Gets unit system for detected unit into provided string representation of a measurement.- Parameters:
source- a measurement string representation to be checked.- Returns:
- a unit system (either metric or imperial) or null if unit cannot be determined.
-
parse
Parses a string into a measure.- Parameters:
source- text to be parsed.- Returns:
- a measure containing measure value and unit obtained from parsed text.
- Throws:
ParseException- if parsing failed.UnknownUnitException- if unit cannot be determined.
-
findUnit
Finds measure unit from within a measurement string representation.- Parameters:
source- a measurement string representation.- Returns:
- a measure unit or null if none can be determined.
-
getUnitSymbol
Obtains measure unit symbol.- Parameters:
unit- a measure unit.- Returns:
- measure unit symbol.
-
internalParse
Internal method to parse a string into a measure.- Parameters:
source- text to be parsed.measure- a measure to be initialized with parsed contents.- Returns:
- provided measure.
- Throws:
ParseException- if parsing failed.UnknownUnitException- if unit cannot be determined.
-
internalClone
Internal method to clone this measure formatter.- Parameters:
copy- an instantiated copy of a measure formatter that needs to be initialized.- Returns:
- provided copy.
-
clone
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-