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.annotation;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025/**
026 * Declares that a field or method parameter should be formatted as a number.
027 *
028 * <p>Supports formatting by style or custom pattern string.
029 * Can be applied to any JDK {@code java.lang.Number} type.
030 *
031 * <p>For style-based formatting, set the {@link #style} attribute to be the
032 * desired {@link Style}. For custom formatting, set the {@link #pattern}
033 * attribute to be the number pattern, such as {@code #, ###.##}.
034 *
035 * <p>Each attribute is mutually exclusive, so only set one attribute per
036 * annotation instance (the one most convenient one for your formatting needs).
037 * When the {@link #pattern} attribute is specified, it takes precedence over
038 * the {@link #style} attribute. When no annotation attributes are specified,
039 * the default format applied is style-based for either number of currency,
040 * depending on the annotated field or method parameter type.
041 *
042 * @author Keith Donald
043 * @author Juergen Hoeller
044 * @since 3.0
045 * @see java.text.NumberFormat
046 */
047@Documented
048@Retention(RetentionPolicy.RUNTIME)
049@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
050public @interface NumberFormat {
051
052        /**
053         * The style pattern to use to format the field.
054         * <p>Defaults to {@link Style#DEFAULT} for general-purpose number formatting
055         * for most annotated types, except for money types which default to currency
056         * formatting. Set this attribute when you wish to format your field in
057         * accordance with a common style other than the default style.
058         */
059        Style style() default Style.DEFAULT;
060
061        /**
062         * The custom pattern to use to format the field.
063         * <p>Defaults to empty String, indicating no custom pattern String has been specified.
064         * Set this attribute when you wish to format your field in accordance with a
065         * custom number pattern not represented by a style.
066         */
067        String pattern() default "";
068
069
070        /**
071         * Common number format styles.
072         */
073        enum Style {
074075                /**
076                 * The default format for the annotated type: typically 'number' but possibly
077                 * 'currency' for a money type (e.g. {@code javax.money.MonetaryAmount)}.
078                 * @since 4.2
079                 */
080                DEFAULT,
081
082                /**
083                 * The general-purpose number format for the current locale.
084                 */
085                NUMBER,
086
087                /**
088                 * The percent format for the current locale.
089                 */
090                PERCENT,
091
092                /**
093                 * The currency format for the current locale.
094                 */
095                CURRENCY
096        }
097
098}