001/*
002 * Copyright (c) 2007-2013, Stephen Colebourne & Michael Nascimento Santos
003 *
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are met:
008 *
009 *  * Redistributions of source code must retain the above copyright notice,
010 *    this list of conditions and the following disclaimer.
011 *
012 *  * Redistributions in binary form must reproduce the above copyright notice,
013 *    this list of conditions and the following disclaimer in the documentation
014 *    and/or other materials provided with the distribution.
015 *
016 *  * Neither the name of JSR-310 nor the names of its contributors
017 *    may be used to endorse or promote products derived from this software
018 *    without specific prior written permission.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032package org.threeten.bp.format;
033
034/**
035 * Enumeration of ways to handle the positive/negative sign.
036 * <p>
037 * The formatting engine allows the positive and negative signs of numbers
038 * to be controlled using this enum.
039 * See {@link DateTimeFormatterBuilder} for usage.
040 *
041 * <h3>Specification for implementors</h3>
042 * This is an immutable and thread-safe enum.
043 */
044public enum SignStyle {
045
046    /**
047     * Style to output the sign only if the value is negative.
048     * <p>
049     * In strict parsing, the negative sign will be accepted and the positive sign rejected.
050     * In lenient parsing, any sign will be accepted.
051     */
052    NORMAL,
053    /**
054     * Style to always output the sign, where zero will output '+'.
055     * <p>
056     * In strict parsing, the absence of a sign will be rejected.
057     * In lenient parsing, any sign will be accepted, with the absence
058     * of a sign treated as a positive number.
059     */
060    ALWAYS,
061    /**
062     * Style to never output sign, only outputting the absolute value.
063     * <p>
064     * In strict parsing, any sign will be rejected.
065     * In lenient parsing, any sign will be accepted unless the width is fixed.
066     */
067    NEVER,
068    /**
069     * Style to block negative values, throwing an exception on printing.
070     * <p>
071     * In strict parsing, any sign will be rejected.
072     * In lenient parsing, any sign will be accepted unless the width is fixed.
073     */
074    NOT_NEGATIVE,
075    /**
076     * Style to always output the sign if the value exceeds the pad width.
077     * A negative value will always output the '-' sign.
078     * <p>
079     * In strict parsing, the sign will be rejected unless the pad width is exceeded.
080     * In lenient parsing, any sign will be accepted, with the absence
081     * of a sign treated as a positive number.
082     */
083    EXCEEDS_PAD;
084
085    /**
086     * Parse helper.
087     *
088     * @param positive  true if positive sign parsed, false for negative sign
089     * @param strict  true if strict, false if lenient
090     * @param fixedWidth  true if fixed width, false if not
091     * @return
092     */
093    boolean parse(boolean positive, boolean strict, boolean fixedWidth) {
094        switch (ordinal()) {
095            case 0: // NORMAL
096                // valid if negative or (positive and lenient)
097                return !positive || !strict;
098            case 1: // ALWAYS
099            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}