下面列出了org.springframework.core.convert.Property#org.springframework.format.annotation.NumberFormat 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
String pattern = resolveEmbeddedValue(annotation.pattern());
if (StringUtils.hasLength(pattern)) {
return new NumberStyleFormatter(pattern);
}
else {
Style style = annotation.style();
if (style == Style.CURRENCY) {
return new CurrencyStyleFormatter();
}
else if (style == Style.PERCENT) {
return new PercentStyleFormatter();
}
else {
return new NumberStyleFormatter();
}
}
}
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
String pattern = resolveEmbeddedValue(annotation.pattern());
if (StringUtils.hasLength(pattern)) {
return new PatternDecoratingFormatter(pattern);
}
else {
Style style = annotation.style();
if (style == Style.NUMBER) {
return new NumberDecoratingFormatter(new NumberStyleFormatter());
}
else if (style == Style.PERCENT) {
return new NumberDecoratingFormatter(new PercentStyleFormatter());
}
else {
return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
}
}
}
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
String pattern = resolveEmbeddedValue(annotation.pattern());
if (StringUtils.hasLength(pattern)) {
return new NumberStyleFormatter(pattern);
}
else {
Style style = annotation.style();
if (style == Style.CURRENCY) {
return new CurrencyStyleFormatter();
}
else if (style == Style.PERCENT) {
return new PercentStyleFormatter();
}
else {
return new NumberStyleFormatter();
}
}
}
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
String pattern = resolveEmbeddedValue(annotation.pattern());
if (StringUtils.hasLength(pattern)) {
return new PatternDecoratingFormatter(pattern);
}
else {
Style style = annotation.style();
if (style == Style.NUMBER) {
return new NumberDecoratingFormatter(new NumberStyleFormatter());
}
else if (style == Style.PERCENT) {
return new NumberDecoratingFormatter(new PercentStyleFormatter());
}
else {
return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
}
}
}
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
if (StringUtils.hasLength(annotation.pattern())) {
return new NumberStyleFormatter(resolveEmbeddedValue(annotation.pattern()));
}
else {
Style style = annotation.style();
if (style == Style.CURRENCY) {
return new CurrencyStyleFormatter();
}
else if (style == Style.PERCENT) {
return new PercentStyleFormatter();
}
else {
return new NumberStyleFormatter();
}
}
}
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
if (StringUtils.hasLength(annotation.pattern())) {
return new PatternDecoratingFormatter(resolveEmbeddedValue(annotation.pattern()));
}
else {
Style style = annotation.style();
if (style == Style.NUMBER) {
return new NumberDecoratingFormatter(new NumberStyleFormatter());
}
else if (style == Style.PERCENT) {
return new NumberDecoratingFormatter(new PercentStyleFormatter());
}
else {
return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
}
}
}
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
if (StringUtils.hasLength(annotation.pattern())) {
return new NumberStyleFormatter(resolveEmbeddedValue(annotation.pattern()));
}
else {
Style style = annotation.style();
if (style == Style.CURRENCY) {
return new CurrencyStyleFormatter();
}
else if (style == Style.PERCENT) {
return new PercentStyleFormatter();
}
else {
return new NumberStyleFormatter();
}
}
}
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
if (StringUtils.hasLength(annotation.pattern())) {
return new PatternDecoratingFormatter(resolveEmbeddedValue(annotation.pattern()));
}
else {
Style style = annotation.style();
if (style == Style.NUMBER) {
return new NumberDecoratingFormatter(new NumberStyleFormatter());
}
else if (style == Style.PERCENT) {
return new NumberDecoratingFormatter(new PercentStyleFormatter());
}
else {
return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
}
}
}
/**
* Get a formatter for class and property name
* @param clazz the class
* @param propertyName the property name
* @return the formatter or null if none
*/
public static Formatter<?> getFormatter(Class<?> clazz, String propertyName) {
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, propertyName);
if (pd != null) {
NumberFormat format = getAnnotation(pd, NumberFormat.class);
if (format != null) {
return (Formatter<?>) numberFormatFactory.getPrinter(format, pd.getPropertyType());
}
PeriodFormat periodFormat = getAnnotation(pd, PeriodFormat.class);
if (periodFormat != null)
return new PeriodFormatter();
}
return null;
}
@Override
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Printer<MonetaryAmount> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@NumberFormat(style=Style.PERCENT)
public BigDecimal getFormattable() {
return new BigDecimal(".25");
}
@Override
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Printer<MonetaryAmount> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@NumberFormat(style=Style.PERCENT)
public BigDecimal getFormattable() {
return new BigDecimal(".25");
}
@RequestMapping(method = RequestMethod.GET)
String getTest(@RequestParam("amount") @NumberFormat(
pattern = CUSTOM_PATTERN) BigDecimal amount);
@Override
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Printer<MonetaryAmount> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Printer<MonetaryAmount> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
}
@NumberFormat(style=Style.PERCENT)
public BigDecimal getFormattable() {
return new BigDecimal(".25");
}