001/*
002 * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE CONDITION THAT YOU
003 * ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT. PLEASE READ THE TERMS AND CONDITIONS OF THIS
004 * AGREEMENT CAREFULLY. BY DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF
005 * THE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE" BUTTON AT THE
006 * BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency API ("Specification") Copyright
007 * (c) 2012-2013, Credit Suisse All rights reserved.
008 */
009package org.javamoney.moneta.spi.base;
010
011import javax.money.MonetaryAmount;
012import javax.money.MonetaryAmountFactory;
013import javax.money.MonetaryContext;
014import javax.money.Monetary;
015
016/**
017 * Factory for {@link javax.money.MonetaryAmount} instances for a given type. It can be accessed, by
018 * <ul>
019 * <li>calling {@link javax.money.MonetaryAmount#getFactory()}, returning a {@link BaseMonetaryAmountFactory}
020 * creating amounts of the same implementation type, which also provided the factory instance.</li>
021 * <li>calling {@link javax.money.Monetary#getAmountFactory(Class)} accessing a
022 * {@link BaseMonetaryAmountFactory} for a concrete type <code>Class<T></code>.</li>
023 * <li>calling {@link javax.money.Monetary#getDefaultAmountFactory()} accessing a default
024 * {@link BaseMonetaryAmountFactory}.
025 * </ul>
026 * <p>
027 * Implementations of this interface allow to get {@link javax.money.MonetaryAmount} instances providing
028 * different data as required:
029 * <ul>
030 * <li>the {@link javax.money.CurrencyUnit}, or the corresponding currency code (must be solvable by
031 * {@link javax.money.Monetary}).</li>
032 * <li>the number part</li>
033 * <li>the {@link javax.money.MonetaryContext}</li>
034 * <li>by passing any {@link javax.money.MonetaryAmount} instance, it is possible to convert an arbitrary amount
035 * implementation to the implementation provided by this factory. If the current factory cannot
036 * support the precision/scale as required by the current {@link javax.money.NumberValue} a
037 * {@link javax.money.MonetaryException} must be thrown.</li>
038 * </ul>
039 * If not defined a default {@link javax.money.MonetaryContext} is used, which can also be configured by adding
040 * configuration to a file {@code /javamoney.properties} to the classpath.
041 * <p>
042 * Hereby the entries. e.g. for a class {@code MyMoney} should start with {@code a.b.MyMoney.ctx}. The entries valid
043 * must be documented
044 * on the according implementation class, where the following entries are defined for all implementation types
045 * (example below given for a class {@code a.b.MyMoney}:
046 * <ul>
047 * <li>{@code a.b.MyMoney.ctx.precision} to define the maximal supported precision.</li>
048 * <li>{@code a.b.MyMoney.ctx.maxScale} to define the maximal supported scale.</li>
049 * <li>{@code a.b.MyMoney.ctx.fixedScale} to define the scale to be fixed (constant).</li>
050 * </ul>
051 * <p>
052 * <h2>Implementation specification</h2> Instances of this interface are <b>not</b> required to be
053 * thread-safe!
054 *
055 * @author Anatole Tresch
056 * @author Werner Keil
057 * @version 0.6.1
058 */
059public abstract class BaseMonetaryAmountFactory<T extends MonetaryAmount> implements MonetaryAmountFactory<T> {
060
061    /**
062     * Sets the {@link javax.money.CurrencyUnit} to be used.
063     *
064     * @param currencyCode the currencyCode of the currency to be used, not {@code null}. The currency code
065     *                     will be resolved using {@link javax.money.Monetary#getCurrency(String, String...)}.
066     * @return This factory instance, for chaining.
067     * @throws javax.money.UnknownCurrencyException if the {@code currencyCode} is not resolvable.
068     * @throws javax.money.UnknownCurrencyException if the {@code currencyCode} is not resolvable.
069     */
070    public MonetaryAmountFactory<T> setCurrency(String currencyCode) {
071        return setCurrency(Monetary.getCurrency(currencyCode));
072    }
073
074    /**
075     * Uses an arbitrary {@link javax.money.MonetaryAmount} to initialize this factory. Properties reused are:
076     * <ul>
077     * <li>CurrencyUnit,</li>
078     * <li>Number value,</li>
079     * <li>MonetaryContext.</li>
080     * </ul>
081     *
082     * @param amount the amount to be used, not {@code null}.
083     * @return this factory instance, for chaining.
084     * @throws javax.money.MonetaryException when the {@link javax.money.MonetaryContext} implied by {@code amount.getContext()}
085     *                           exceeds the capabilities supported by this factory type.
086     */
087    public MonetaryAmountFactory<T> setAmount(MonetaryAmount amount) {
088        setCurrency(amount.getCurrency());
089        setNumber(amount.getNumber());
090        setContext(amount.getContext());
091        return this;
092    }
093
094    /**
095     * Returns the maximal {@link javax.money.MonetaryContext} supported, for requests that exceed these maximal
096     * capabilities, an {@link ArithmeticException} must be thrown.
097     *
098     * @return the maximal {@link javax.money.MonetaryContext} supported, never {@code null}
099     */
100    public MonetaryContext getMaximalMonetaryContext() {
101        return getDefaultMonetaryContext();
102    }
103
104}