Top Description Fields Constructors Methods
org.javamoney.moneta.convert

pack-priv Class DefaultExchangeRate

extends Object
implements ExchangeRate, Serializable, Comparable<ExchangeRate>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable, java.io.Serializable, javax.money.convert.ExchangeRate, javax.money.CurrencySupplier
Imports
java.io.Serializable, java.math.BigDecimal, java.util.ArrayList, .List, .Objects, javax.money.CurrencyUnit, .NumberValue, javax.money.convert.ConversionContext, .ExchangeRate

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

The class also implements Comparable to allow sorting of multiple exchange rates using the following sorting order;

Finally ExchangeRate is modeled as an immutable and thread safe type. Also exchange rates are java.io.Serializable, hereby serializing in the following form and order:

Authors
Werner Keil, Anatole Tresch
See Also
Wikipedia: Exchange Rate (Quotations)

Field Summary

Modifier and TypeField and Description
private final CurrencyUnit
base

The base currency.

private final List<ExchangeRate>
chain

The full chain, at least one instance long.

private final ConversionContext
private final NumberValue
factor

The conversion factor.

private static final long
serialVersionUID

serialVersionUID.

private final CurrencyUnit
term

The terminating currency.

Constructor Summary

AccessConstructor and Description
pack-priv
DefaultExchangeRate(ExchangeRateBuilder
The Builder, never null.
builder
)

Creates a new instance with a custom chain of exchange rate type, e.g. or creating derived rates.

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(ExchangeRate o)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public final CurrencyUnit
public final ConversionContext
public final CurrencyUnit
public final List<ExchangeRate>

Returns:

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}.
getExchangeRateChain
()

Implements javax.money.convert.ExchangeRate.getExchangeRateChain.

Access the chain of exchange rates.

public final NumberValue

Returns:

the bid factor for this exchange rate, or null.
getFactor
()

Implements javax.money.convert.ExchangeRate.getFactor.

Access the rate's bid factor.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public final boolean

Returns:

true, if the exchange rate is derived.
isDerived
()

Overrides default javax.money.convert.ExchangeRate.isDerived.

Allows to evaluate if this exchange rate is a derived exchange rate.

private void
setExchangeRateChain(List<ExchangeRate>
the chain to set.
chain
)

Internal method to set the rate chain, which also ensure that the chain passed, when not null, contains valid elements.

public ExchangeRateBuilder

Returns:

a new ExchangeRateBuilder, never null.
toBuilder
()

Create a ExchangeRateBuilder based on the current rate instance.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

baseback to summary
private final CurrencyUnit base

The base currency.

chainback to summary
private final List<ExchangeRate> chain

The full chain, at least one instance long.

conversionContextback to summary
private final ConversionContext conversionContext

The javax.money.convert.ConversionContext

factorback to summary
private final NumberValue factor

The conversion factor.

serialVersionUIDback to summary
private static final long serialVersionUID

serialVersionUID.

termback to summary
private final CurrencyUnit term

The terminating currency.

Constructor Detail

DefaultExchangeRateback to summary
pack-priv DefaultExchangeRate(ExchangeRateBuilder builder)

Creates a new instance with a custom chain of exchange rate type, e.g. or creating derived rates.

Parameters
builder:ExchangeRateBuilder

The Builder, never null.

Method Detail

compareToback to summary
public int compareTo(ExchangeRate o)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getBaseCurrencyback to summary
public final CurrencyUnit getBaseCurrency()

Implements javax.money.convert.ExchangeRate.getBaseCurrency.

Get the base (source) javax.money.CurrencyUnit.

Returns:CurrencyUnit

the base javax.money.CurrencyUnit.

Annotations
@Override
getContextback to summary
public final ConversionContext getContext()

Implements javax.money.convert.ExchangeRate.getContext.

Access the javax.money.convert.ConversionContext of javax.money.convert.ExchangeRate.

Returns:ConversionContext

the conversion context, never null.

Annotations
@Override
getCurrencyback to summary
public final CurrencyUnit getCurrency()

Implements javax.money.convert.ExchangeRate.getCurrency.

Get the term (target) javax.money.CurrencyUnit.

Returns:CurrencyUnit

the term javax.money.CurrencyUnit.

Annotations
@Override
getExchangeRateChainback to summary
public final List<ExchangeRate> getExchangeRateChain()

Implements javax.money.convert.ExchangeRate.getExchangeRateChain.

Access the chain of exchange rates.

Returns:List<ExchangeRate>

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}.

Annotations
@Override
getFactorback to summary
public final NumberValue getFactor()

Implements javax.money.convert.ExchangeRate.getFactor.

Access the rate's bid factor.

Returns:NumberValue

the bid factor for this exchange rate, or null.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
isDerivedback to summary
public final boolean isDerived()

Overrides default javax.money.convert.ExchangeRate.isDerived.

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 true, if the chain contains more than one rate. Direct rates, have also a chain, but with exact one rate.

Returns:boolean

true, if the exchange rate is derived.

Annotations
@Override
setExchangeRateChainback to summary
private void setExchangeRateChain(List<ExchangeRate> chain)

Internal method to set the rate chain, which also ensure that the chain passed, when not null, contains valid elements.

Parameters
chain:List<ExchangeRate>

the chain to set.

toBuilderback to summary
public ExchangeRateBuilder toBuilder()

Create a ExchangeRateBuilder based on the current rate instance.

Returns:ExchangeRateBuilder

a new ExchangeRateBuilder, never null.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override