001/* 002 * Copyright 2002-2017 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; 020 021import javax.money.MonetaryAmount; 022import javax.money.format.MonetaryAmountFormat; 023import javax.money.format.MonetaryFormats; 024 025import org.springframework.format.Formatter; 026import org.springframework.lang.Nullable; 027 028/** 029 * Formatter for JSR-354 {@link javax.money.MonetaryAmount} values, 030 * delegating to {@link javax.money.format.MonetaryAmountFormat#format} 031 * and {@link javax.money.format.MonetaryAmountFormat#parse}. 032 * 033 * @author Juergen Hoeller 034 * @since 4.2 035 * @see #getMonetaryAmountFormat 036 */ 037public class MonetaryAmountFormatter implements Formatter<MonetaryAmount> { 038 039 @Nullable 040 private String formatName; 041 042 043 /** 044 * Create a locale-driven MonetaryAmountFormatter. 045 */ 046 public MonetaryAmountFormatter() { 047 } 048 049 /** 050 * Create a new MonetaryAmountFormatter for the given format name. 051 * @param formatName the format name, to be resolved by the JSR-354 052 * provider at runtime 053 */ 054 public MonetaryAmountFormatter(String formatName) { 055 this.formatName = formatName; 056 } 057 058 059 /** 060 * Specify the format name, to be resolved by the JSR-354 provider 061 * at runtime. 062 * <p>Default is none, obtaining a {@link MonetaryAmountFormat} 063 * based on the current locale. 064 */ 065 public void setFormatName(String formatName) { 066 this.formatName = formatName; 067 } 068 069 070 @Override 071 public String print(MonetaryAmount object, Locale locale) { 072 return getMonetaryAmountFormat(locale).format(object); 073 } 074 075 @Override 076 public MonetaryAmount parse(String text, Locale locale) { 077 return getMonetaryAmountFormat(locale).parse(text); 078 } 079 080 081 /** 082 * Obtain a MonetaryAmountFormat for the given locale. 083 * <p>The default implementation simply calls 084 * {@link javax.money.format.MonetaryFormats#getAmountFormat} 085 * with either the configured format name or the given locale. 086 * @param locale the current locale 087 * @return the MonetaryAmountFormat (never {@code null}) 088 * @see #setFormatName 089 */ 090 protected MonetaryAmountFormat getMonetaryAmountFormat(Locale locale) { 091 if (this.formatName != null) { 092 return MonetaryFormats.getAmountFormat(this.formatName); 093 } 094 else { 095 return MonetaryFormats.getAmountFormat(locale); 096 } 097 } 098 099}