001package org.consensusj.exchange;
002
003import javax.money.CurrencyUnit;
004import javax.money.Monetary;
005import javax.money.convert.ExchangeRate;
006
007/**
008 * Currency Pair using JavaMoney CurrencyUnit type
009 */
010public class CurrencyUnitPair implements Comparable<CurrencyUnitPair> {
011
012    final private CurrencyUnit base;
013    final private CurrencyUnit target;
014
015    /**
016     * @param pair A string of the form "base/target"
017     */
018    public CurrencyUnitPair(String pair) {
019        this(Monetary.getCurrency(pair.split("/")[0]), Monetary.getCurrency(pair.split("/")[1]));
020    }
021
022    /*
023     * @param base base currency as a JavaMoney currency code
024     * @param target base currency as a JavaMoney currency code
025     */
026    public CurrencyUnitPair(String base, String target) {
027        this(Monetary.getCurrency(base), Monetary.getCurrency(target));
028    }
029
030    /*
031     * @param base base currency as a JavaMoney CurrencyUnit
032     * @param target base currency as a JavaMoney CurrencyUnit
033     */
034    public CurrencyUnitPair(CurrencyUnit base, CurrencyUnit target) {
035        this.base = base;
036        this.target = target;
037    }
038
039    /**
040     * @param rate specifies the base and target currencies
041     */
042    public CurrencyUnitPair(ExchangeRate rate) {
043        this(rate.getBaseCurrency(), rate.getCurrency());
044    }
045
046    /**
047     * Get base CurrencyUnit
048     * @return base CurrencyUnit
049     */
050    public CurrencyUnit getBase() {
051        return base;
052    }
053
054    /**
055     * Get target CurrencyUnit
056     * @return target CurrencyUnit
057     */
058    public CurrencyUnit getTarget() {
059        return target;
060    }
061
062    @Override
063    public String toString() {
064
065        return base.getCurrencyCode() + "/" + target.getCurrencyCode();
066    }
067
068    @Override
069    public int hashCode() {
070
071        final int prime = 31;
072        int result = 1;
073        result = prime * result + ((base == null) ? 0 : base.hashCode());
074        result = prime * result + ((target == null) ? 0 : target.hashCode());
075        return result;
076    }
077
078    @Override
079    public boolean equals(Object obj) {
080
081        if (this == obj) {
082            return true;
083        }
084        if (obj == null) {
085            return false;
086        }
087        if (getClass() != obj.getClass()) {
088            return false;
089        }
090        CurrencyUnitPair other = (CurrencyUnitPair) obj;
091        if (base == null) {
092            if (other.base != null) {
093                return false;
094            }
095        } else if (!base.equals(other.base)) {
096            return false;
097        }
098        if (target == null) {
099            if (other.target != null) {
100                return false;
101            }
102        } else if (!target.equals(other.target)) {
103            return false;
104        }
105        return true;
106    }
107
108    @Override
109    public int compareTo(CurrencyUnitPair o) {
110
111        return (base.compareTo(o.base) << 16) + target.compareTo(o.target);
112    }
113}