All Downloads are FREE. Search and download functionalities are using the official Maven repository.

humanize.measure.MeasureFormatProvider Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
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"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy