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.function;
017
018import static java.util.Objects.requireNonNull;
019
020import java.math.MathContext;
021import java.math.RoundingMode;
022
023import javax.money.CurrencyUnit;
024import javax.money.MonetaryAmount;
025import javax.money.MonetaryOperator;
026
027/**
028 * this interface is used to create {@link org.javamoney.moneta.RoundedMoney} using the {@link MonetaryOperator} as rounding.
029 * @see MonetaryRoundedFactory#of(MathContext)
030 * @see MonetaryRoundedFactory#of(MonetaryOperator)
031 * @see MonetaryRoundedFactory#withRoundingMode(RoundingMode)
032 * @author Otavio Santana
033 * @since 1.0.1
034 */
035public abstract class MonetaryRoundedFactory {
036
037        /**
038         * return the {@link MonetaryOperator} as rounding operator
039         * @return the rounding operator
040         */
041        abstract MonetaryOperator getRoundingOperator();
042
043        /**
044         * Create a {@link MonetaryAmount} with {@link Number}, {@link CurrencyUnit} and
045         * the {@link MonetaryOperator} as rounding operator given in this factory with the
046         * {@link MonetaryRoundedFactory#getRoundingOperator()}. The implementation will {@link org.javamoney.moneta.RoundedMoney}
047         * @param number the amount, not null.
048         * @param currencyUnit the currency, not null.
049         * @return the {@link MonetaryAmount} from number and {@link CurrencyUnit}
050         */
051        abstract  MonetaryAmount create(Number number, CurrencyUnit currencyUnit);
052
053        /**
054         * Create a factory to {@link MonetaryRoundedFactoryBuilder} with this factory is possible make
055         * a custom {@link MonetaryOperator} as rounding operator, setting the precision, scale or both.
056         * @param roundingMode the rounding mode, not null.
057         * @see ScaleRoundedOperator
058         * @see PrecisionContextRoundedOperator
059         * @see PrecisionScaleRoundedOperator
060         * @see RoundingMode
061         * @return the builder to set scale, precision or both
062         * @throws NullPointerException if roundingMode is null
063         */
064        static MonetaryRoundedFactoryBuilder withRoundingMode(RoundingMode roundingMode) {
065                return new MonetaryRoundedFactoryBuilder(requireNonNull(roundingMode));
066        }
067
068        /**
069         * Create the {@link MonetaryRoundedFactory} using the {@link PrecisionContextRoundedOperator} as rounding operator.
070         * @param mathContext the mathContext that will be used to create the {@link PrecisionContextRoundedOperator}
071         * @see PrecisionContextRoundedOperator#of(MathContext)
072         * @see PrecisionContextRoundedOperator
073         * @return the factory using the MathContextRoundedOperator
074         * @throws NullPointerException if mathContext is null
075         */
076        public static MonetaryRoundedFactory of(MathContext mathContext) {
077                return new DefaultMonetaryRoundedFactory(PrecisionContextRoundedOperator.of(requireNonNull(mathContext)));
078        }
079
080        /**
081         * Create the {@link MonetaryRoundedFactory} using a custom {@link MonetaryOperator} as rounding operator.
082         * @param roundingOperator a custom {@link MonetaryOperator} that will be used in this factory
083         * @return the factory using the MathContextRoundedOperator
084         * @throws NullPointerException if roundingOperator is null
085         */
086        public static MonetaryRoundedFactory of(MonetaryOperator roundingOperator) {
087                return new DefaultMonetaryRoundedFactory(requireNonNull(roundingOperator));
088        }
089}