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.format.internal; 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 */ 038public class DefaultAmountFormatProviderSpi implements MonetaryAmountFormatProviderSpi { 039 040 private static final String DEFAULT_STYLE = "default"; 041 private static final String PROVIDER_NAME = "default"; 042 043 private Set<Locale> supportedSets = new HashSet<>(); 044 private Set<String> formatNames = new HashSet<>(); 045 046 public DefaultAmountFormatProviderSpi() { 047 supportedSets.addAll(Arrays.asList(DecimalFormat.getAvailableLocales())); 048 supportedSets = Collections.unmodifiableSet(supportedSets); 049 formatNames.add(DEFAULT_STYLE); 050 formatNames = Collections.unmodifiableSet(formatNames); 051 } 052 053 @Override 054 public String getProviderName() { 055 return PROVIDER_NAME; 056 } 057 058 /* 059 * (non-Javadoc) 060 * @see 061 * javax.money.spi.MonetaryAmountFormatProviderSpi#getFormat(javax.money.format.AmountFormatContext) 062 */ 063 @Override 064 public Collection<MonetaryAmountFormat> getAmountFormats(AmountFormatQuery amountFormatQuery) { 065 Objects.requireNonNull(amountFormatQuery, "AmountFormatContext required"); 066 if (!amountFormatQuery.getProviderNames().isEmpty() && 067 !amountFormatQuery.getProviderNames().contains(getProviderName())) { 068 return Collections.emptySet(); 069 } 070 if (!(amountFormatQuery.getFormatName() == null || DEFAULT_STYLE.equals(amountFormatQuery.getFormatName()))) { 071 return Collections.emptySet(); 072 } 073 AmountFormatContextBuilder builder = AmountFormatContextBuilder.of(DEFAULT_STYLE); 074 if (amountFormatQuery.getLocale() != null) { 075 builder.setLocale(amountFormatQuery.getLocale()); 076 } 077 builder.importContext(amountFormatQuery, false); 078 builder.setMonetaryAmountFactory(amountFormatQuery.getMonetaryAmountFactory()); 079 return Arrays.asList(new MonetaryAmountFormat[]{new DefaultMonetaryAmountFormat(builder.build())}); 080 } 081 082 @Override 083 public Set<Locale> getAvailableLocales() { 084 return supportedSets; 085 } 086 087 @Override 088 public Set<String> getAvailableFormatNames() { 089 return formatNames; 090 } 091 092}