001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://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,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.number;
018
019import java.util.Set;
020
021import org.springframework.context.support.EmbeddedValueResolutionSupport;
022import org.springframework.format.AnnotationFormatterFactory;
023import org.springframework.format.Formatter;
024import org.springframework.format.Parser;
025import org.springframework.format.Printer;
026import org.springframework.format.annotation.NumberFormat;
027import org.springframework.format.annotation.NumberFormat.Style;
028import org.springframework.util.NumberUtils;
029import org.springframework.util.StringUtils;
030
031/**
032 * Formats fields annotated with the {@link NumberFormat} annotation.
033 *
034 * @author Keith Donald
035 * @author Juergen Hoeller
036 * @since 3.0
037 * @see NumberFormat
038 */
039public class NumberFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
040                implements AnnotationFormatterFactory<NumberFormat> {
041
042        @Override
043        public Set<Class<?>> getFieldTypes() {
044                return NumberUtils.STANDARD_NUMBER_TYPES;
045        }
046
047        @Override
048        public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
049                return configureFormatterFrom(annotation);
050        }
051
052        @Override
053        public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
054                return configureFormatterFrom(annotation);
055        }
056
057
058        private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
059                if (StringUtils.hasLength(annotation.pattern())) {
060                        return new NumberStyleFormatter(resolveEmbeddedValue(annotation.pattern()));
061                }
062                else {
063                        Style style = annotation.style();
064                        if (style == Style.CURRENCY) {
065                                return new CurrencyStyleFormatter();
066                        }
067                        else if (style == Style.PERCENT) {
068                                return new PercentStyleFormatter();
069                        }
070                        else {
071                                return new NumberStyleFormatter();
072                        }
073                }
074        }
075
076}