001/*
002  Copyright (c) 2012, 2014, 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 java.text.DecimalFormat;
025import java.text.DecimalFormatSymbols;
026import java.text.NumberFormat;
027import java.util.Currency;
028import java.util.Locale;
029
030/**
031 * Builder to {@link MonetaryAmountFormat}.
032 * @see  MonetaryAmountDecimalFormatBuilder#newInstance
033 * @see  MonetaryAmountDecimalFormatBuilder#of
034 * @since 1.0.1
035 */
036public class MonetaryAmountDecimalFormatBuilder {
037
038    private DecimalFormat decimalFormat;
039
040    private CurrencyUnit currencyUnit;
041
042    private Locale locale;
043
044    private MonetaryAmountProducer producer;
045
046    private MonetaryAmountDecimalFormatBuilder() {
047    }
048
049    /**
050     * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with default {@link Locale}.
051     * @see NumberFormat#getCurrencyInstance()
052     * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder}
053     */
054    public static MonetaryAmountDecimalFormatBuilder newInstance() {
055        MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder();
056        builder.decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance();
057        return builder;
058    }
059
060    /**
061     * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with {@link Locale} set from parameter.
062     * @param locale the target locale
063     * @see NumberFormat#getCurrencyInstance(Locale)
064     * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder}
065     */
066    public static MonetaryAmountDecimalFormatBuilder of(Locale locale) {
067        MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder();
068        builder.decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
069        builder.locale = locale;
070        return builder;
071    }
072
073    /**
074     * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with default {@link Locale} and pattern to format the {@link javax.money.MonetaryAmount}.
075     * @param  pattern the pattern to be used.
076     * @see DecimalFormat
077     * @see DecimalFormat#DecimalFormat(String)
078     * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder}
079     */
080    public static MonetaryAmountDecimalFormatBuilder of(String pattern) {
081        MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder();
082        builder.decimalFormat = new DecimalFormat(pattern);
083        return builder;
084    }
085
086    /**
087     * Creates a new instance of {@link MonetaryAmountDecimalFormatBuilder} with {@link Locale} set from parameter and pattern to format the {@link javax.money.MonetaryAmount}.
088     * @param  pattern the pattern to be used.
089     * @param  locale the target locale
090     * @see DecimalFormat
091     * @see DecimalFormat#DecimalFormat(String)
092     * @return a new instance of {@link MonetaryAmountDecimalFormatBuilder}
093     */
094    public static MonetaryAmountDecimalFormatBuilder of(String pattern, Locale locale) {
095        MonetaryAmountDecimalFormatBuilder builder = new MonetaryAmountDecimalFormatBuilder();
096        builder.decimalFormat = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(locale));
097        builder.locale = locale;
098        return builder;
099    }
100
101    /**
102     * Sets the {@link CurrencyUnit}
103     * @param currencyUnit the target currency
104     * @return the {@link MonetaryAmountDecimalFormatBuilder}
105     */
106    public MonetaryAmountDecimalFormatBuilder withCurrencyUnit(CurrencyUnit currencyUnit) {
107        this.currencyUnit = currencyUnit;
108        return this;
109    }
110
111    /**
112     * Sets the {@link MonetaryAmountProducer}
113     * @param producer the producer
114     * @return the {@link MonetaryAmountDecimalFormatBuilder}
115     */
116    public MonetaryAmountDecimalFormatBuilder withProducer(MonetaryAmountProducer producer) {
117        this.producer = producer;
118        return this;
119    }
120
121    /**
122     * Creates the {@link MonetaryAmountFormat}
123     * If @{link Locale} didn't set the default value is {@link Locale#getDefault()}
124     * If @{link MonetaryAmountProducer} didn't set the default value is {@link MoneyProducer}
125     * If @{link CurrencyUnit} didn't set the default value is a currency from {@link Locale}
126     * @return {@link MonetaryAmountFormat}
127     */
128    public MonetaryAmountFormat build() {
129        if (locale == null) {
130            locale = Locale.getDefault();
131        }
132        if (decimalFormat == null) {
133            decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
134        }
135        if (currencyUnit == null) {
136            currencyUnit = Monetary.getCurrency(locale);
137        }
138        if (producer == null) {
139            producer = new MoneyProducer();
140        }
141        decimalFormat.setCurrency(Currency.getInstance(currencyUnit.getCurrencyCode()));
142        return new MonetaryAmountDecimalFormat(decimalFormat, producer, currencyUnit);
143    }
144
145}