All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javamoney.moneta.function.MonetaryRoundedFactory Maven / Gradle / Ivy

Go to download

JSR 354 provides an API for representing, transporting, and performing comprehensive calculations with Money and Currency. This module implements JSR 354.

There is a newer version: 1.4.1
Show newest version
/*
  Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy of
  the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  License for the specific language governing permissions and limitations under
  the License.
 */
package org.javamoney.moneta.function;

import static java.util.Objects.requireNonNull;

import java.math.MathContext;
import java.math.RoundingMode;

import javax.money.CurrencyUnit;
import javax.money.MonetaryAmount;
import javax.money.MonetaryOperator;

/**
 * this interface is used to create {@link org.javamoney.moneta.RoundedMoney} using the {@link MonetaryOperator} as rounding.
 * @see MonetaryRoundedFactory#of(MathContext)
 * @see MonetaryRoundedFactory#of(MonetaryOperator)
 * @see MonetaryRoundedFactory#withRoundingMode(RoundingMode)
 * @author Otavio Santana
 * @since 1.0.1
 */
public abstract class MonetaryRoundedFactory {

	/**
	 * return the {@link MonetaryOperator} as rounding operator
	 * @return the rounding operator
	 */
	abstract MonetaryOperator getRoundingOperator();

	/**
	 * Create a {@link MonetaryAmount} with {@link Number}, {@link CurrencyUnit} and
	 * the {@link MonetaryOperator} as rounding operator given in this factory with the
	 * {@link MonetaryRoundedFactory#getRoundingOperator()}. The implementation will {@link org.javamoney.moneta.RoundedMoney}
	 * @param number the amount, not null.
	 * @param currencyUnit the currency, not null.
	 * @return the {@link MonetaryAmount} from number and {@link CurrencyUnit}
	 */
	abstract  MonetaryAmount create(Number number, CurrencyUnit currencyUnit);

	/**
	 * Create a factory to {@link MonetaryRoundedFactoryBuilder} with this factory is possible make
	 * a custom {@link MonetaryOperator} as rounding operator, setting the precision, scale or both.
	 * @param roundingMode the rounding mode, not null.
	 * @see ScaleRoundedOperator
	 * @see PrecisionContextRoundedOperator
	 * @see PrecisionScaleRoundedOperator
	 * @see RoundingMode
	 * @return the builder to set scale, precision or both
	 * @throws NullPointerException if roundingMode is null
	 */
	static MonetaryRoundedFactoryBuilder withRoundingMode(RoundingMode roundingMode) {
		return new MonetaryRoundedFactoryBuilder(requireNonNull(roundingMode));
	}

	/**
	 * Create the {@link MonetaryRoundedFactory} using the {@link PrecisionContextRoundedOperator} as rounding operator.
	 * @param mathContext the mathContext that will be used to create the {@link PrecisionContextRoundedOperator}
	 * @see PrecisionContextRoundedOperator#of(MathContext)
	 * @see PrecisionContextRoundedOperator
	 * @return the factory using the MathContextRoundedOperator
	 * @throws NullPointerException if mathContext is null
	 */
	public static MonetaryRoundedFactory of(MathContext mathContext) {
		return new DefaultMonetaryRoundedFactory(PrecisionContextRoundedOperator.of(requireNonNull(mathContext)));
	}

	/**
	 * Create the {@link MonetaryRoundedFactory} using a custom {@link MonetaryOperator} as rounding operator.
	 * @param roundingOperator a custom {@link MonetaryOperator} that will be used in this factory
	 * @return the factory using the MathContextRoundedOperator
	 * @throws NullPointerException if roundingOperator is null
	 */
	public static MonetaryRoundedFactory of(MonetaryOperator roundingOperator) {
		return new DefaultMonetaryRoundedFactory(requireNonNull(roundingOperator));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy