org.javamoney.moneta.spi.AbstractRateProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moneta-bp Show documentation
Show all versions of moneta-bp Show documentation
JSR 354 provides an API for representing, transporting, and performing comprehensive calculations with
Money and Currency.
This module implements JSR 354.
/*
* 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.spi;
import javax.money.convert.ConversionContext;
import javax.money.convert.ConversionQuery;
import javax.money.convert.CurrencyConversion;
import javax.money.convert.ExchangeRate;
import javax.money.convert.ProviderContext;
import javax.money.convert.RateType;
import org.javamoney.moneta.spi.base.BaseExchangeRateProvider;
import javax.money.NumberValue;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Objects;
import java.util.logging.Logger;
/**
* Abstract base class for {@link javax.money.convert.ExchangeRateProvider} implementations.
*
* @author Anatole Tresch
* @author Werner Keil
*/
public abstract class AbstractRateProvider extends BaseExchangeRateProvider {
/**
* The logger used.
*/
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
/**
* The {@link javax.money.convert.ConversionContext} of this provider.
*/
private final ProviderContext providerContext;
/**
* Constructor.
*
* @param providerContext the {@link ProviderContext}, not null.
*/
public AbstractRateProvider(ProviderContext providerContext) {
this.providerContext = Objects.requireNonNull(providerContext);
}
/*
* (non-Javadoc)
*
* @see org.javamoney.bp.api.convert.spi.ExchangeRateProviderSpi#getExchangeRateType
* ()
*/
@Override
public ProviderContext getContext() {
return providerContext;
}
@Override
public abstract ExchangeRate getExchangeRate(ConversionQuery conversionQuery);
@Override
public CurrencyConversion getCurrencyConversion(ConversionQuery conversionQuery) {
if (getContext().getRateTypes().size() == 1) {
return new LazyBoundCurrencyConversion(conversionQuery, this, ConversionContext
.of(getContext().getProviderName(), getContext().getRateTypes().iterator().next()));
}
return new LazyBoundCurrencyConversion(conversionQuery, this,
ConversionContext.of(getContext().getProviderName(), RateType.ANY));
}
/**
* A protected helper method to multiply 2 {@link NumberValue} types.
* If either of the values is null
an {@link ArithmeticException} is thrown.
*
* @param multiplicand the first value to be multiplied
* @param multiplier the second value to be multiplied
* @return the result of the multiplication as {@link NumberValue}
*/
protected static NumberValue multiply(NumberValue multiplicand, NumberValue multiplier) {
if (multiplicand==null) {
throw new ArithmeticException("The multiplicand cannot be null");
}
if (multiplier==null) {
throw new ArithmeticException("The multiplier cannot be null");
}
return new DefaultNumberValue(
multiplicand.numberValueExact(BigDecimal.class).multiply(multiplier.numberValue(BigDecimal.class)));
}
/**
* A protected helper method to divide 2 {@link NumberValue} types.
* If either of the values is null
an {@link ArithmeticException} is thrown.
*
* @param dividend the first value to be divided
* @param divisor the value to be divided by
* @return the result of the division as {@link NumberValue}
*/
protected static NumberValue divide(NumberValue dividend, NumberValue divisor) {
if (dividend==null) {
throw new ArithmeticException("The dividend cannot be null");
}
if (divisor==null) {
throw new ArithmeticException("The divisor cannot be null");
}
return new DefaultNumberValue(
dividend.numberValueExact(BigDecimal.class).divide(divisor.numberValue(BigDecimal.class),
RoundingMode.HALF_EVEN));
}
/**
* A protected helper method to divide 2 {@link NumberValue} types.
* If either of the values is null
an {@link ArithmeticException} is thrown.
*
* @param dividend the first value to be divided
* @param divisor the value to be divided by
* @param context the {@link MathContext} to use
* @return the result of the division as {@link NumberValue}
*/
protected static NumberValue divide(NumberValue dividend, NumberValue divisor, MathContext context) {
if (dividend==null) {
throw new ArithmeticException("The dividend cannot be null");
}
if (divisor==null) {
throw new ArithmeticException("The divisor cannot be null");
}
return new DefaultNumberValue(
dividend.numberValueExact(BigDecimal.class).divide(divisor.numberValue(BigDecimal.class), context));
}
protected String formatLocalDate(Calendar calendar){
return calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) +
"-" + calendar.get(Calendar.DAY_OF_MONTH);
}
}