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

javax.money.convert.ExchangeRate Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.1
Show newest version
/*
 * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
 * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
 * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
 * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
 * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
 * BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
 * API ("Specification") Copyright (c) 2012-2014, Credit Suisse All rights
 * reserved.
 */
package javax.money.convert;

import javax.money.CurrencySupplier;
import javax.money.CurrencyUnit;
import javax.money.NumberValue;
import java.io.Serializable;
import java.util.List;

/**
 * This class models an exchange rate, which defines the factor the numeric value of a base amount in some currency
 * 'A' must be multiplied
 * to get the corresponding amount in the terminating currency 'B'. Hereby
 * 
    *
  • an exchange rate always models one rate from a base (source) to a term * (target) {@link CurrencyUnit}.
  • *
  • an exchange rate is always bound to a rate type, which typically matches * the data source of the conversion data, e.g. different credit card providers * may use different rates for the same conversion.
  • *
  • an exchange rate may restrict its validity. In most of the use cases a * rates' validity will be well defined, but it is also possible that the data * provider is not able to support the rate's validity, leaving it undefined-
  • *
  • an exchange rate has a provider, which is responsible for defining the * rate. A provider hereby may be, but must not be the same as the rate's data * source.
  • *
  • an exchange rate can be a direct rate, where its factor is * represented by a single conversion step. Or it can model a derived * rate, where multiple conversion steps are required to define the overall * base/term conversion. In case of derived rates the chained rates define the * overall factor, by multiplying the individual chain rate factors. Of course, * this also requires that each subsequent rate's base currency in the chain * does match the previous term currency (and vice versa):
  • *
  • Whereas the factor should be directly implied by the format rate chain * for derived rates, this is obviously not the case for the validity range, * since rates can have a undefined validity range. Nevertheless in many cases * also the validity range can (but must not) be derived from the rate chain.
  • *
  • Finally a conversion rate is always unidirectional. There might be cases * where the reciprocal value of {@link #getFactor()}} matches the correct reverse * rate. But in most use cases the reverse rate either has a different rate (not * equal to the reciprocal value), or might not be defined at all. Therefore for * reversing a ExchangeRate one must access an {@link ExchangeRateProvider} and * query for the reverse rate.
  • *
*

* The class also implements {@link Comparable} to allow sorting of multiple * exchange rates using the following sorting order; *

    *
  • Exchange rate type
  • *
  • Exchange rate provider
  • *
  • base currency
  • *
  • term currency
  • *
*

* Finally ExchangeRate is modeled as an immutable and thread safe type. Also * exchange rates are {@link Serializable}, hereby serializing in the following * form and order: *

    *
  • The base {@link CurrencyUnit} *
  • The target {@link CurrencyUnit} *
  • The factor (NumberValue) *
  • The {@link ConversionContext} *
  • The rate chain *
*

*

Implementation Specification

*

Implementations of this interface *

    *
  • must be Comparable(with {@code ExchangeRate})
  • *
  • must implement equals/hashCode considering #getBaseCurrency, #getCurrency, * #getFactor and #getContext.
  • *
  • should be thread-safe
  • *
  • should be serializable
  • *
  • should provide a fluent builder API for constructing new rate instances easily.
  • *
*

* * @author Werner Keil * @author Anatole Tresch * @see Wikipedia: * Exchange Rate (Quotations) */ public interface ExchangeRate extends CurrencySupplier { /** * Access the {@link ConversionContext} of {@link ExchangeRate}. * * @return the conversion context, never null. */ ConversionContext getContext(); /** * Get the base (source) {@link CurrencyUnit}. * * @return the base {@link CurrencyUnit}. */ CurrencyUnit getBaseCurrency(); /** * Get the term (target) {@link CurrencyUnit}. * * @return the term {@link CurrencyUnit}. */ @Override CurrencyUnit getCurrency(); /** * Access the rate's bid factor. * * @return the bid factor for this exchange rate, or {@code null}. */ NumberValue getFactor(); /** * Access the chain of exchange rates. * * @return the chain of rates, in case of a derived rate, this may be * several instances. For a direct exchange rate, this equals to * new ExchangeRate[]{this}. */ List getExchangeRateChain(); /** * Allows to evaluate if this exchange rate is a derived exchange rate. * Derived exchange rates are defined by an ordered list of subconversions * with intermediate steps, whereas a direct conversion is possible in one * steps. *

* This method always returns {@code true}, if the chain contains more than * one rate. Direct rates, have also a chain, but with exact one rate. * * @return true, if the exchange rate is derived. */ default boolean isDerived() { return getExchangeRateChain().size() > 1; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy