001/*
002 * Copyright (c) 2012, 2015, 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.spi.format;
017
018
019import java.text.DecimalFormat;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.HashSet;
024import java.util.Locale;
025import java.util.Objects;
026import java.util.Set;
027
028import javax.money.format.AmountFormatContextBuilder;
029import javax.money.format.AmountFormatQuery;
030import javax.money.format.MonetaryAmountFormat;
031import javax.money.spi.MonetaryAmountFormatProviderSpi;
032
033/**
034 * Default format provider, which mainly maps the existing JDK functionality into the JSR 354 logic.
035 *
036 * @author Anatole Tresch
037 * @author Werner Keil
038 */
039public class DefaultAmountFormatProviderSpi implements MonetaryAmountFormatProviderSpi {
040
041    private static final String DEFAULT_FORMAT = "default";
042    private static final String PROVIDER_NAME = "default";
043
044    private Set<Locale> supportedSets = new HashSet<>();
045    private Set<String> formatNames = new HashSet<>();
046
047    public DefaultAmountFormatProviderSpi() {
048        supportedSets.addAll(Arrays.asList(DecimalFormat.getAvailableLocales()));
049        supportedSets = Collections.unmodifiableSet(supportedSets);
050        formatNames.add(DEFAULT_FORMAT);
051        formatNames = Collections.unmodifiableSet(formatNames);
052    }
053
054    @Override
055    public String getProviderName() {
056        return PROVIDER_NAME;
057    }
058
059    /*
060         * (non-Javadoc)
061         * @see
062         * MonetaryAmountFormatProviderSpi#getFormat(AmountFormatContext)
063         */
064    @Override
065    public Collection<MonetaryAmountFormat> getAmountFormats(AmountFormatQuery amountFormatQuery) {
066        Objects.requireNonNull(amountFormatQuery, "AmountFormatContext required");
067        if (!amountFormatQuery.getProviderNames().isEmpty() &&
068                !amountFormatQuery.getProviderNames().contains(getProviderName())) {
069            return Collections.emptySet();
070        }
071        if (!(amountFormatQuery.getFormatName() == null || DEFAULT_FORMAT.equals(amountFormatQuery.getFormatName()))) {
072            return Collections.emptySet();
073        }
074        AmountFormatContextBuilder builder = AmountFormatContextBuilder.of(DEFAULT_FORMAT);
075        if (amountFormatQuery.getLocale() != null) {
076            builder.setLocale(amountFormatQuery.getLocale());
077        }
078        builder.importContext(amountFormatQuery, false);
079        builder.setMonetaryAmountFactory(amountFormatQuery.getMonetaryAmountFactory());
080        return Arrays.asList(new MonetaryAmountFormat[]{new DefaultMonetaryAmountFormat(builder.build())});
081    }
082
083    @Override
084    public Set<Locale> getAvailableLocales() {
085        return supportedSets;
086    }
087
088    @Override
089    public Set<String> getAvailableFormatNames() {
090        return formatNames;
091    }
092
093}