From 03942913eb3b3a6458030856bfecc8a6927ab1ba Mon Sep 17 00:00:00 2001 From: Julien Lepiller Date: Sat, 15 Sep 2018 10:27:46 +0200 Subject: [PATCH] Remove dependency on jsr354 (javax.currency). jsr354 is a non-free package: https://github.com/JavaMoney/jsr354-api/blob/master/EVALUATION-LICENCE.txt --- .../number/money/CurrencyUnitFormatter.java | 44 ----- ...umberFormatAnnotationFormatterFactory.java | 163 ------------------ .../number/money/MonetaryAmountFormatter.java | 96 ----------- .../format/number/money/package-info.java | 4 - .../DefaultFormattingConversionService.java | 10 -- 5 files changed, 317 deletions(-) delete mode 100644 spring-context/src/main/java/org/springframework/format/number/money/CurrencyUnitFormatter.java delete mode 100644 spring-context/src/main/java/org/springframework/format/number/money/Jsr354NumberFormatAnnotationFormatterFactory.java delete mode 100644 spring-context/src/main/java/org/springframework/format/number/money/MonetaryAmountFormatter.java delete mode 100644 spring-context/src/main/java/org/springframework/format/number/money/package-info.java diff --git a/spring-context/src/main/java/org/springframework/format/number/money/CurrencyUnitFormatter.java b/spring-context/src/main/java/org/springframework/format/number/money/CurrencyUnitFormatter.java deleted file mode 100644 index d3f4749..0000000 --- a/spring-context/src/main/java/org/springframework/format/number/money/CurrencyUnitFormatter.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2002-2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.format.number.money; - -import java.util.Locale; -import javax.money.CurrencyUnit; -import javax.money.Monetary; - -import org.springframework.format.Formatter; - -/** - * Formatter for JSR-354 {@link javax.money.CurrencyUnit} values, - * from and to currency code Strings. - * - * @author Juergen Hoeller - * @since 4.2 - */ -public class CurrencyUnitFormatter implements Formatter { - - @Override - public String print(CurrencyUnit object, Locale locale) { - return object.getCurrencyCode(); - } - - @Override - public CurrencyUnit parse(String text, Locale locale) { - return Monetary.getCurrency(text); - } - -} diff --git a/spring-context/src/main/java/org/springframework/format/number/money/Jsr354NumberFormatAnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/number/money/Jsr354NumberFormatAnnotationFormatterFactory.java deleted file mode 100644 index 6cec059..0000000 --- a/spring-context/src/main/java/org/springframework/format/number/money/Jsr354NumberFormatAnnotationFormatterFactory.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2002-2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.format.number.money; - -import java.text.ParseException; -import java.util.Collections; -import java.util.Currency; -import java.util.Locale; -import java.util.Set; -import javax.money.CurrencyUnit; -import javax.money.Monetary; -import javax.money.MonetaryAmount; - -import org.springframework.context.support.EmbeddedValueResolutionSupport; -import org.springframework.format.AnnotationFormatterFactory; -import org.springframework.format.Formatter; -import org.springframework.format.Parser; -import org.springframework.format.Printer; -import org.springframework.format.annotation.NumberFormat; -import org.springframework.format.annotation.NumberFormat.Style; -import org.springframework.format.number.CurrencyStyleFormatter; -import org.springframework.format.number.NumberStyleFormatter; -import org.springframework.format.number.PercentStyleFormatter; -import org.springframework.util.StringUtils; - -/** - * Formats {@link javax.money.MonetaryAmount} fields annotated - * with Spring's common {@link NumberFormat} annotation. - * - * @author Juergen Hoeller - * @since 4.2 - * @see NumberFormat - */ -public class Jsr354NumberFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport - implements AnnotationFormatterFactory { - - private static final String CURRENCY_CODE_PATTERN = "\u00A4\u00A4"; - - - @Override - @SuppressWarnings("unchecked") - public Set> getFieldTypes() { - return (Set) Collections.singleton(MonetaryAmount.class); - } - - @Override - public Printer getPrinter(NumberFormat annotation, Class fieldType) { - return configureFormatterFrom(annotation); - } - - @Override - public Parser getParser(NumberFormat annotation, Class fieldType) { - return configureFormatterFrom(annotation); - } - - - private Formatter configureFormatterFrom(NumberFormat annotation) { - if (StringUtils.hasLength(annotation.pattern())) { - return new PatternDecoratingFormatter(resolveEmbeddedValue(annotation.pattern())); - } - else { - Style style = annotation.style(); - if (style == Style.NUMBER) { - return new NumberDecoratingFormatter(new NumberStyleFormatter()); - } - else if (style == Style.PERCENT) { - return new NumberDecoratingFormatter(new PercentStyleFormatter()); - } - else { - return new NumberDecoratingFormatter(new CurrencyStyleFormatter()); - } - } - } - - - private static class NumberDecoratingFormatter implements Formatter { - - private final Formatter numberFormatter; - - public NumberDecoratingFormatter(Formatter numberFormatter) { - this.numberFormatter = numberFormatter; - } - - @Override - public String print(MonetaryAmount object, Locale locale) { - return this.numberFormatter.print(object.getNumber(), locale); - } - - @Override - public MonetaryAmount parse(String text, Locale locale) throws ParseException { - CurrencyUnit currencyUnit = Monetary.getCurrency(locale); - Number numberValue = this.numberFormatter.parse(text, locale); - return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); - } - } - - - private static class PatternDecoratingFormatter implements Formatter { - - private final String pattern; - - public PatternDecoratingFormatter(String pattern) { - this.pattern = pattern; - } - - @Override - public String print(MonetaryAmount object, Locale locale) { - CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); - formatter.setCurrency(Currency.getInstance(object.getCurrency().getCurrencyCode())); - formatter.setPattern(this.pattern); - return formatter.print(object.getNumber(), locale); - } - - @Override - public MonetaryAmount parse(String text, Locale locale) throws ParseException { - CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); - Currency currency = determineCurrency(text, locale); - CurrencyUnit currencyUnit = Monetary.getCurrency(currency.getCurrencyCode()); - formatter.setCurrency(currency); - formatter.setPattern(this.pattern); - Number numberValue = formatter.parse(text, locale); - return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); - } - - private Currency determineCurrency(String text, Locale locale) { - try { - if (text.length() < 3) { - // Could not possibly contain a currency code -> - // try with locale and likely let it fail on parse. - return Currency.getInstance(locale); - } - else if (this.pattern.startsWith(CURRENCY_CODE_PATTERN)) { - return Currency.getInstance(text.substring(0, 3)); - } - else if (this.pattern.endsWith(CURRENCY_CODE_PATTERN)) { - return Currency.getInstance(text.substring(text.length() - 3)); - } - else { - // A pattern without a currency code... - return Currency.getInstance(locale); - } - } - catch (IllegalArgumentException ex) { - throw new IllegalArgumentException("Cannot determine currency for number value [" + text + "]", ex); - } - } - } - -} diff --git a/spring-context/src/main/java/org/springframework/format/number/money/MonetaryAmountFormatter.java b/spring-context/src/main/java/org/springframework/format/number/money/MonetaryAmountFormatter.java deleted file mode 100644 index 8d949ac..0000000 --- a/spring-context/src/main/java/org/springframework/format/number/money/MonetaryAmountFormatter.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2002-2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.format.number.money; - -import java.util.Locale; -import javax.money.MonetaryAmount; -import javax.money.format.MonetaryAmountFormat; -import javax.money.format.MonetaryFormats; - -import org.springframework.format.Formatter; - -/** - * Formatter for JSR-354 {@link javax.money.MonetaryAmount} values, - * delegating to {@link javax.money.format.MonetaryAmountFormat#format} - * and {@link javax.money.format.MonetaryAmountFormat#parse}. - * - * @author Juergen Hoeller - * @since 4.2 - * @see #getMonetaryAmountFormat - */ -public class MonetaryAmountFormatter implements Formatter { - - private String formatName; - - - /** - * Create a locale-driven MonetaryAmountFormatter. - */ - public MonetaryAmountFormatter() { - } - - /** - * Create a new MonetaryAmountFormatter for the given format name. - * @param formatName the format name, to be resolved by the JSR-354 - * provider at runtime - */ - public MonetaryAmountFormatter(String formatName) { - this.formatName = formatName; - } - - - /** - * Specify the format name, to be resolved by the JSR-354 provider - * at runtime. - *

Default is none, obtaining a {@link MonetaryAmountFormat} - * based on the current locale. - */ - public void setFormatName(String formatName) { - this.formatName = formatName; - } - - - @Override - public String print(MonetaryAmount object, Locale locale) { - return getMonetaryAmountFormat(locale).format(object); - } - - @Override - public MonetaryAmount parse(String text, Locale locale) { - return getMonetaryAmountFormat(locale).parse(text); - } - - - /** - * Obtain a MonetaryAmountFormat for the given locale. - *

The default implementation simply calls - * {@link javax.money.format.MonetaryFormats#getAmountFormat} - * with either the configured format name or the given locale. - * @param locale the current locale - * @return the MonetaryAmountFormat (never {@code null}) - * @see #setFormatName - */ - protected MonetaryAmountFormat getMonetaryAmountFormat(Locale locale) { - if (this.formatName != null) { - return MonetaryFormats.getAmountFormat(this.formatName); - } - else { - return MonetaryFormats.getAmountFormat(locale); - } - } - -} diff --git a/spring-context/src/main/java/org/springframework/format/number/money/package-info.java b/spring-context/src/main/java/org/springframework/format/number/money/package-info.java deleted file mode 100644 index d19fccf..0000000 --- a/spring-context/src/main/java/org/springframework/format/number/money/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Integration with the JSR-354 javax.money package. - */ -package org.springframework.format.number.money; diff --git a/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java index 2c7c60a..5e38006 100644 --- a/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java @@ -21,9 +21,6 @@ import org.springframework.format.FormatterRegistry; import org.springframework.format.datetime.DateFormatterRegistrar; import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar; import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar; -import org.springframework.format.number.money.CurrencyUnitFormatter; -import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory; -import org.springframework.format.number.money.MonetaryAmountFormatter; import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; import org.springframework.util.ClassUtils; import org.springframework.util.StringValueResolver; @@ -104,13 +101,6 @@ public class DefaultFormattingConversionService extends FormattingConversionServ // Default handling of number values formatterRegistry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); - // Default handling of monetary values - if (jsr354Present) { - formatterRegistry.addFormatter(new CurrencyUnitFormatter()); - formatterRegistry.addFormatter(new MonetaryAmountFormatter()); - formatterRegistry.addFormatterForFieldAnnotation(new Jsr354NumberFormatAnnotationFormatterFactory()); - } - // Default handling of date-time values if (jsr310Present) { // just handling JSR-310 specific date and time types -- 2.18.0