001/** 002 * Copyright (c) 2012, 2015, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://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, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016package org.javamoney.moneta.format; 017 018import org.javamoney.moneta.function.MonetaryAmountProducer; 019import org.javamoney.moneta.function.MoneyProducer; 020 021import javax.money.CurrencyUnit; 022import javax.money.Monetary; 023import javax.money.format.MonetaryAmountFormat; 024import javax.money.format.MonetaryFormats; 025import java.text.DecimalFormat; 026import java.text.DecimalFormatSymbols; 027import java.text.NumberFormat; 028import java.util.Currency; 029import java.util.Locale; 030import java.util.Objects; 031 032/** 033 * Builder to {@link MonetaryAmountFormat}. 034 * @see {@Link MonetaryAmountDecimalFormatBuilder#newInstance} 035 * @see {@Link MonetaryAmountDecimalFormatBuilder#of} 036 * @since 1.0.1 037 */ 038public class MonetaryAmountDecimalFormatBuilder { 039 040 private DecimalFormat decimalFormat; 041 042 private CurrencyUnit currencyUnit; 043 044 private Locale locale; 045 046 private MonetaryAmountProducer producer; 047 048 private MonetaryAmountDecimalFormatBuilder() { 049 } 050 051 /** 052 * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with default {@link Locale}. 053 * @see {@link NumberFormat#getCurrencyInstance()} 054 * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder} 055 */ 056 public static MonetaryAmountDecimalFormatBuilder newInstance() { 057 MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder(); 058 builder.decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(); 059 return builder; 060 } 061 062 /** 063 * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with {@link Locale} set from parameter. 064 * @param locale 065 * @see {@link NumberFormat#getCurrencyInstance(Locale)} 066 * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder} 067 */ 068 public static MonetaryAmountDecimalFormatBuilder of(Locale locale) { 069 MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder(); 070 builder.decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(locale); 071 builder.locale = locale; 072 return builder; 073 } 074 075 /** 076 * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with default {@link Locale} and pattern to format the {@link javax.money.MonetaryAmount}. 077 * @param pattern 078 * @see {@link DecimalFormat} 079 * @see {@link DecimalFormat#DecimalFormat(String)} 080 * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder} 081 */ 082 public static MonetaryAmountDecimalFormatBuilder of(String pattern) { 083 MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder(); 084 builder.decimalFormat = new DecimalFormat(pattern); 085 return builder; 086 } 087 088 /** 089 * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with {@link Locale} set from parameter and pattern to format the {@link javax.money.MonetaryAmount}. 090 * @param pattern 091 * @param locale 092 * @see {@link DecimalFormat} 093 * @see {@link DecimalFormat#DecimalFormat(String)} 094 * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder} 095 */ 096 public static MonetaryAmountDecimalFormatBuilder of(String pattern, Locale locale) { 097 MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder(); 098 builder.decimalFormat = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(locale)); 099 builder.locale = locale; 100 return builder; 101 } 102 103 /** 104 * Sets the {@link CurrencyUnit} 105 * @param currencyUnit 106 * @return the {@link MonetaryAmountDecimalFormatBuilder} 107 */ 108 public MonetaryAmountDecimalFormatBuilder withCurrencyUnit(CurrencyUnit currencyUnit) { 109 this.currencyUnit = currencyUnit; 110 return this; 111 } 112 113 /** 114 * Sets the {@link MonetaryAmountProducer} 115 * @param producer 116 * @return the {@link MonetaryAmountDecimalFormatBuilder} 117 */ 118 public MonetaryAmountDecimalFormatBuilder withProducer(MonetaryAmountProducer producer) { 119 this.producer = producer; 120 return this; 121 } 122 123 /** 124 * Creates the {@link MonetaryAmountFormat} 125 * If @{link Locale} didn't set the default value is {@link Locale#getDefault()} 126 * If @{link MonetaryAmountProducer} didn't set the default value is {@link MoneyProducer} 127 * If @{link CurrencyUnit} didn't set the default value is a currency from {@link Locale} 128 * @return {@link MonetaryAmountFormat} 129 */ 130 public MonetaryAmountFormat build() { 131 if (Objects.isNull(locale)) { 132 locale = Locale.getDefault(); 133 } 134 if (Objects.isNull(decimalFormat)) { 135 decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(locale); 136 } 137 if (Objects.isNull(currencyUnit)) { 138 currencyUnit = Monetary.getCurrency(locale); 139 } 140 if (Objects.isNull(producer)) { 141 producer = new MoneyProducer(); 142 } 143 decimalFormat.setCurrency(Currency.getInstance(currencyUnit.getCurrencyCode())); 144 return new MonetaryAmountDecimalFormat(decimalFormat, producer, currencyUnit); 145 } 146 147}