• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.format;
33 
34 /**
35  * Enumeration of ways to handle the positive/negative sign.
36  * <p>
37  * The formatting engine allows the positive and negative signs of numbers
38  * to be controlled using this enum.
39  * See {@link DateTimeFormatterBuilder} for usage.
40  *
41  * <h3>Specification for implementors</h3>
42  * This is an immutable and thread-safe enum.
43  */
44 public enum SignStyle {
45 
46     /**
47      * Style to output the sign only if the value is negative.
48      * <p>
49      * In strict parsing, the negative sign will be accepted and the positive sign rejected.
50      * In lenient parsing, any sign will be accepted.
51      */
52     NORMAL,
53     /**
54      * Style to always output the sign, where zero will output '+'.
55      * <p>
56      * In strict parsing, the absence of a sign will be rejected.
57      * In lenient parsing, any sign will be accepted, with the absence
58      * of a sign treated as a positive number.
59      */
60     ALWAYS,
61     /**
62      * Style to never output sign, only outputting the absolute value.
63      * <p>
64      * In strict parsing, any sign will be rejected.
65      * In lenient parsing, any sign will be accepted unless the width is fixed.
66      */
67     NEVER,
68     /**
69      * Style to block negative values, throwing an exception on printing.
70      * <p>
71      * In strict parsing, any sign will be rejected.
72      * In lenient parsing, any sign will be accepted unless the width is fixed.
73      */
74     NOT_NEGATIVE,
75     /**
76      * Style to always output the sign if the value exceeds the pad width.
77      * A negative value will always output the '-' sign.
78      * <p>
79      * In strict parsing, the sign will be rejected unless the pad width is exceeded.
80      * In lenient parsing, any sign will be accepted, with the absence
81      * of a sign treated as a positive number.
82      */
83     EXCEEDS_PAD;
84 
85     /**
86      * Parse helper.
87      *
88      * @param positive  true if positive sign parsed, false for negative sign
89      * @param strict  true if strict, false if lenient
90      * @param fixedWidth  true if fixed width, false if not
91      * @return true if valid
92      */
parse(boolean positive, boolean strict, boolean fixedWidth)93     boolean parse(boolean positive, boolean strict, boolean fixedWidth) {
94         switch (ordinal()) {
95             case 0: // NORMAL
96                 // valid if negative or (positive and lenient)
97                 return !positive || !strict;
98             case 1: // ALWAYS
99             case 4: // EXCEEDS_PAD
100                 return true;
101             default:
102                 // valid if lenient and not fixed width
103                 return !strict && !fixedWidth;
104         }
105     }
106 
107 }
108