001/*
002 * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE CONDITION THAT YOU
003 * ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT. PLEASE READ THE TERMS AND CONDITIONS OF THIS
004 * AGREEMENT CAREFULLY. BY DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF
005 * THE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE" BUTTON AT THE
006 * BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency API ("Specification") Copyright
007 * (c) 2012-2013, Credit Suisse All rights reserved.
008 */
009package org.javamoney.moneta.spi.base;
010
011import javax.money.MonetaryException;
012import javax.money.format.AmountFormatQuery;
013import javax.money.format.AmountFormatQueryBuilder;
014import javax.money.format.MonetaryAmountFormat;
015import javax.money.spi.MonetaryFormatsSingletonSpi;
016import java.util.Collection;
017import java.util.Locale;
018
019/**
020 * This interface models the singleton functionality of {@link javax.money.format.MonetaryFormats}.
021 * <p>
022 * Implementations of this interface must be thread-safe.
023 *
024 * @author Anatole Tresch
025 * @author Werner Keil
026 */
027public abstract class BaseMonetaryFormatsSingletonSpi implements MonetaryFormatsSingletonSpi{
028
029    /**
030     * Access an {@link javax.money.format.MonetaryAmountFormat} given a {@link javax.money.format
031     * .AmountFormatQuery}.
032     *
033     * @param formatQuery The format query defining the requirements of the formatter.
034     * @return the corresponding {@link javax.money.format.MonetaryAmountFormat}
035     * @throws javax.money.MonetaryException if no registered {@link javax.money.spi
036     *                                       .MonetaryAmountFormatProviderSpi} can provide a
037     *                                       corresponding {@link javax.money.format.MonetaryAmountFormat} instance.
038     */
039    public MonetaryAmountFormat getAmountFormat(AmountFormatQuery formatQuery) {
040        Collection<MonetaryAmountFormat> formats = getAmountFormats(formatQuery);
041        if (formats.isEmpty()) {
042            throw new MonetaryException("No MonetaryAmountFormat for AmountFormatQuery " + formatQuery);
043        }
044        return formats.iterator().next();
045    }
046
047    /**
048     * Checks if a {@link javax.money.format.MonetaryAmountFormat} is available given a {@link javax.money.format
049     * .AmountFormatQuery}.
050     *
051     * @param formatQuery The format query defining the requirements of the formatter.
052     * @return true, if a t least one {@link javax.money.format.MonetaryAmountFormat} is matching the query.
053     */
054    public boolean isAvailable(AmountFormatQuery formatQuery) {
055        return !getAmountFormats(formatQuery).isEmpty();
056    }
057
058    /**
059     * Checks if a {@link javax.money.format.MonetaryAmountFormat} is available given a {@link javax.money.format
060     * .AmountFormatQuery}.
061     *
062     * @param locale    the target {@link java.util.Locale}, not {@code null}.
063     * @param providers The (optional) providers to be used, ordered correspondingly.
064     * @return true, if a t least one {@link javax.money.format.MonetaryAmountFormat} is matching the query.
065     */
066    public boolean isAvailable(Locale locale, String... providers) {
067        return isAvailable(AmountFormatQuery.of(locale, providers));
068    }
069
070    /**
071     * Access the default {@link javax.money.format.MonetaryAmountFormat} given a {@link java.util.Locale}.
072     *
073     * @param locale    the target {@link java.util.Locale}, not {@code null}.
074     * @param providers The (optional) providers to be used, oredered correspondingly.
075     * @return the matching {@link javax.money.format.MonetaryAmountFormat}
076     * @throws javax.money.MonetaryException if no registered {@link javax.money.spi.MonetaryAmountFormatProviderSpi} can provide a
077     *                           corresponding {@link javax.money.format.MonetaryAmountFormat} instance.
078     */
079    public MonetaryAmountFormat getAmountFormat(Locale locale, String... providers) {
080        return getAmountFormat(AmountFormatQueryBuilder.of(locale).setProviderNames(providers).build());
081    }
082
083    /**
084     * Access the default {@link javax.money.format.MonetaryAmountFormat} given a {@link java.util.Locale}.
085     *
086     * @param formatName the target format name, not {@code null}.
087     * @param providers  The (optional) providers to be used, ordered correspondingly.
088     * @return the matching {@link javax.money.format.MonetaryAmountFormat}
089     * @throws javax.money.MonetaryException if no registered {@link javax.money.spi.MonetaryAmountFormatProviderSpi} can provide a
090     *                           corresponding {@link javax.money.format.MonetaryAmountFormat} instance.
091     */
092    public MonetaryAmountFormat getAmountFormat(String formatName, String... providers) {
093        return getAmountFormat(AmountFormatQueryBuilder.of(formatName).setProviderNames(providers).build());
094    }
095
096}