001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.format.number.money; 018 019import java.util.Locale; 020import javax.money.MonetaryAmount; 021import javax.money.format.MonetaryAmountFormat; 022import javax.money.format.MonetaryFormats; 023 024import org.springframework.format.Formatter; 025 026/** 027 * Formatter for JSR-354 {@link javax.money.MonetaryAmount} values, 028 * delegating to {@link javax.money.format.MonetaryAmountFormat#format} 029 * and {@link javax.money.format.MonetaryAmountFormat#parse}. 030 * 031 * @author Juergen Hoeller 032 * @since 4.2 033 * @see #getMonetaryAmountFormat 034 */ 035public class MonetaryAmountFormatter implements Formatter<MonetaryAmount> { 036 037 private String formatName; 038 039 040 /** 041 * Create a locale-driven MonetaryAmountFormatter. 042 */ 043 public MonetaryAmountFormatter() { 044 } 045 046 /** 047 * Create a new MonetaryAmountFormatter for the given format name. 048 * @param formatName the format name, to be resolved by the JSR-354 049 * provider at runtime 050 */ 051 public MonetaryAmountFormatter(String formatName) { 052 this.formatName = formatName; 053 } 054 055 056 /** 057 * Specify the format name, to be resolved by the JSR-354 provider 058 * at runtime. 059 * <p>Default is none, obtaining a {@link MonetaryAmountFormat} 060 * based on the current locale. 061 */ 062 public void setFormatName(String formatName) { 063 this.formatName = formatName; 064 } 065 066 067 @Override 068 public String print(MonetaryAmount object, Locale locale) { 069 return getMonetaryAmountFormat(locale).format(object); 070 } 071 072 @Override 073 public MonetaryAmount parse(String text, Locale locale) { 074 return getMonetaryAmountFormat(locale).parse(text); 075 } 076 077 078 /** 079 * Obtain a MonetaryAmountFormat for the given locale. 080 * <p>The default implementation simply calls 081 * {@link javax.money.format.MonetaryFormats#getAmountFormat} 082 * with either the configured format name or the given locale. 083 * @param locale the current locale 084 * @return the MonetaryAmountFormat (never {@code null}) 085 * @see #setFormatName 086 */ 087 protected MonetaryAmountFormat getMonetaryAmountFormat(Locale locale) { 088 if (this.formatName != null) { 089 return MonetaryFormats.getAmountFormat(this.formatName); 090 } 091 else { 092 return MonetaryFormats.getAmountFormat(locale); 093 } 094 } 095 096}