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 Details

    • DEFAULT_VALUE_AND_UNIT_FORMAT_PATTERN

      public static final String 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 numberFormat
      Internal string formatter.
    • format

      private MessageFormat format
      Internal string formatter.
    • locale

      private Locale locale
      Internal locale.
    • valueAndUnitFormatPattern

      private String 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

      public boolean equals(Object obj)
      Determines if two measure formatters are equal by comparing all of its internal parameters.
      Overrides:
      equals in class Object
      Parameters:
      obj - another object to compare.
      Returns:
      true if provided object is assumed to be equal to this instance.
    • hashCode

      public int hashCode()
      Hash code generated for this instance. Hash codes can be internally used by some collections to coarsely compare objects.
      Overrides:
      hashCode in class Object
      Returns:
      hash code.
    • format

      public String format(Number value, U unit)
      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

      public StringBuffer 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.
      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

      public String format(double value, U unit)
      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

      public StringBuffer 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.
      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

      public String format(M measurement)
      Formats provided measurement into a string representation.
      Parameters:
      measurement - a measurement.
      Returns:
      string representation of provided measurement.
    • format

      public StringBuffer format(M measurement, StringBuffer toAppendTo, FieldPosition pos)
      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

      public String 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). 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

      public String 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). 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

      public String 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). 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

      public abstract String formatAndConvert(Number value, U unit, UnitSystem system)
      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

      public String formatAndConvert(double value, U unit, UnitSystem system)
      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

      public String formatAndConvert(M measurement, UnitSystem unitSystem)
      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

      public static Locale[] getAvailableLocales()
      Returns available locales for this formatter.
      Returns:
      available locales.
    • getLocale

      public Locale 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

      public RoundingMode getRoundingMode()
      Returns rounding mode to be used when formatting a measure.
      Returns:
      rounding mode to be used when formatting a measure.
    • setRoundingMode

      public void setRoundingMode(RoundingMode roundingMode)
      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

      public String 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

      public void setValueAndUnitFormatPattern(String valueAndUnitFormatPattern)
      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

      public UnitSystem 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

      public boolean isValidUnit(String source)
      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

      public boolean isValidMeasurement(String source)
      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

      public boolean isMetricUnit(String source)
      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

      public boolean isImperialUnit(String source)
      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

      public abstract UnitSystem getUnitSystem(String source)
      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

      public abstract M parse(String source) throws ParseException, UnknownUnitException
      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

      public abstract U findUnit(String source)
      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

      public abstract String getUnitSymbol(U unit)
      Obtains measure unit symbol.
      Parameters:
      unit - a measure unit.
      Returns:
      measure unit symbol.
    • internalParse

      M internalParse(String source, M measure) throws ParseException, UnknownUnitException
      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

      MeasureFormatter<M,U> internalClone(MeasureFormatter<M,U> copy)
      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

      protected MeasureFormatter<M,U> clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException