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.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 {@link MonetaryRoundedFactory#of(MathContext)}
030 * @see {@link MonetaryRoundedFactory#of(MonetaryOperator)}
031 * @see {@link MonetaryRoundedFactory#withRoundingMode(RoundingMode)}
032 * @author Otavio Santana
033 * @since 1.0.1
034 */
035public interface MonetaryRoundedFactory {
036
037        /**
038         * return the {@link MonetaryOperator} as rounding operator
039         * @return the rounding operator
040         */
041        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 RoundedMoney}
047         * @param number
048         * @param currencyUnit
049         * @return the {@link MonetaryAmount} from number and {@link CurrencyUnit}
050         */
051        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
057         * @see {@link ScaleRoundedOperator}
058         * @see {@link PrecisionContextRoundedOperator}
059         * @see {@link PrecisionScaleRoundedOperator}
060         * @see {@link 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 {@link PrecisionContextRoundedOperator#of(MathContext)}
072         * @see {@link PrecisionContextRoundedOperator}
073         * @return the factory using the MathContextRoundedOperator
074         * @throws NullPointerException if mathContext is null
075         */
076        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        static MonetaryRoundedFactory of(MonetaryOperator roundingOperator) {
087                return new DefaultMonetaryRoundedFactory(requireNonNull(roundingOperator));
088        }
089}