humanize.measure.MeasureFormatProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of humanize-ucum Show documentation
Show all versions of humanize-ucum Show documentation
Unified Code for Units of Measure support (JSR-275)
package humanize.measure;
import humanize.spi.FormatProvider;
import humanize.text.FormatFactory;
import java.text.Format;
import java.text.NumberFormat;
import java.util.Locale;
import javax.measure.MeasureFormat;
import javax.measure.unit.UnitFormat;
/**
*
* Provides a JSR-275 measure formatter. Supports "standard" sub-format.
*
*
* Examples:
*
*
*
* // == "100 kg"
* Humanize.format("{0, measure}", Measure.valueOf(100, SI.GRAM.times(1000)));
*
* Humanize.format("{0, measure, standard}", measure);
*
* // With a locale
*
* MessageFormat esFormat = Humanize.messageFormatInstance("{0, measure}", new Locale("es"));
* esFormat.render(Measure.valueOf(1000, SI.GRAM.times(1000)));
*
*
*
*/
public class MeasureFormatProvider implements FormatProvider {
@Override
public FormatFactory getFactory() {
return new FormatFactory() {
@Override
public Format getFormat(String name, String args, Locale locale) {
if (args != null && "standard".equalsIgnoreCase(args)) {
return MeasureFormat.getStandard();
} else {
return MeasureFormat.getInstance(NumberFormat.getInstance(locale), UnitFormat.getInstance(locale));
}
}
};
}
@Override
public String getFormatName() {
return "measure";
}
}