001/**
002 * Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016package org.javamoney.moneta.internal;
017
018import java.io.Serializable;
019import java.util.Currency;
020import java.util.Objects;
021
022import javax.money.CurrencyContext;
023import javax.money.CurrencyContextBuilder;
024import javax.money.CurrencyUnit;
025
026/**
027 * Default implementation of a {@link CurrencyUnit} based on the using the JDK's
028 * {@link Currency}.
029 *
030 * @author Anatole Tresch
031 * @author Werner Keil
032 * @version 0.6
033 */
034public final class JDKCurrencyAdapter implements CurrencyUnit, Serializable, Comparable<CurrencyUnit> {
035
036    /**
037     * serialVersionUID.
038     */
039    private static final long serialVersionUID = -798486953910548549L;
040
041    /**
042     * JDK currency instance.
043     */
044    private final Currency baseCurrency;
045
046    private final CurrencyContext context = CurrencyContextBuilder.of(Currency.class.getName()).build();
047
048    /**
049     * Private constructor, uses a {@link java.util.Currency} for creating new instances.
050     *
051     * @param currency the Currency instance, not {@code null}.
052     */
053    JDKCurrencyAdapter(Currency currency) {
054        this.baseCurrency = currency;
055    }
056
057    /**
058     * Gets the unique currency code, the effective code depends on the
059     * currency.
060     * <p>
061     * Since each currency is identified by this code, the currency code is
062     * required to be defined for every {@link CurrencyUnit} and not
063     * {@code null} or empty.
064     * <p>
065     * For ISO codes the 3-letter ISO code should be returned. For non ISO
066     * currencies no constraints are defined.
067     *
068     * @return the currency code, never {@code null}. For ISO-4217 this this
069     * will be the three letter ISO-4217 code. However, alternate
070     * currencies can have different codes. Also there is no constraint
071     * about the formatting of alternate codes, despite they fact that
072     * the currency codes must be unique.
073     * @see javax.money.CurrencyUnit#getCurrencyCode()
074     */
075    public String getCurrencyCode() {
076        return baseCurrency.getCurrencyCode();
077    }
078
079    /**
080     * Gets a numeric currency code. Within the ISO-4217 name space, this equals
081     * to the ISO numeric code. In other currency name spaces this number may be
082     * different, or even undefined (-1).
083     * <p>
084     * The numeric code is an optional alternative to the standard currency
085     * code. If defined, the numeric code is required to be unique.
086     * <p>
087     * This method matches the API of <type>java.util.Currency</type>.
088     *
089     * @return the numeric currency code
090     * @see CurrencyUnit#getNumericCode()
091     */
092    public int getNumericCode() {
093        return baseCurrency.getNumericCode();
094    }
095
096    /**
097     * Gets the number of fractional digits typically used by this currency.
098     * <p>
099     * Different currencies have different numbers of fractional digits by
100     * default. * For example, 'GBP' has 2 fractional digits, but 'JPY' has
101     * zero. * virtual currencies or those with no applicable fractional are
102     * indicated by -1. *
103     * <p>
104     * This method matches the API of <type>java.util.Currency</type>.
105     *
106     * @return the fractional digits, from 0 to 9 (normally 0, 2 or 3), or 0 for
107     * pseudo-currencies.
108     */
109    public int getDefaultFractionDigits() {
110        return baseCurrency.getDefaultFractionDigits();
111    }
112
113    @Override
114    public CurrencyContext getContext() {
115        return context;
116    }
117
118    /**
119     * Compares two instances, based on {@link #getCurrencyCode()}.
120     *
121     * @param currency The instance to be compared with.
122     * @see java.lang.Comparable#compareTo(java.lang.Object)
123     */
124    @Override
125    public int compareTo(CurrencyUnit currency) {
126        Objects.requireNonNull(currency);
127        return getCurrencyCode().compareTo(currency.getCurrencyCode());
128    }
129
130    /*
131     * (non-Javadoc)
132     *
133     * @see java.lang.Object#hashCode()
134     */
135    @Override
136    public int hashCode() {
137        return Objects.hashCode(baseCurrency);
138    }
139
140    /*
141     * (non-Javadoc)
142     *
143     * @see java.lang.Object#equals(java.lang.Object)
144     */
145    @Override
146    public boolean equals(Object obj) {
147        if (obj == this) {
148            return true;
149        }
150        if (obj instanceof CurrencyUnit) {
151            CurrencyUnit other = (CurrencyUnit) obj;
152            return Objects.equals(getCurrencyCode(), other.getCurrencyCode());
153        }
154        return false;
155    }
156
157    /**
158     * Returns {@link #getCurrencyCode()}
159     *
160     * @see java.lang.Object#toString()
161     */
162    @Override
163    public String toString() {
164        return baseCurrency.toString();
165    }
166
167}