001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.exception;
018
019import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
020
021/**
022 * Exception to be thrown when the a sequence of values is not monotonically
023 * increasing or decreasing.
024 *
025 * @since 2.2 (name changed to "NonMonotonicSequenceException" in 3.0)
026 */
027public class NonMonotonicSequenceException extends MathIllegalNumberException {
028    /** Serializable version Id. */
029    private static final long serialVersionUID = 20210531L;
030    /**
031     * Whether the sequence should be increasing.
032     */
033    private final boolean increasing;
034    /**
035     * Whether the sequence must be strictly increasing or decreasing.
036     */
037    private final boolean strict;
038    /**
039     * Index of the wrong value.
040     */
041    private final int index;
042    /**
043     * Previous value.
044     */
045    private final Number previous;
046
047    /**
048     * Construct the exception.
049     * This constructor uses default values assuming that the sequence should
050     * have been strictly increasing.
051     *
052     * @param wrong Value that did not match the requirements.
053     * @param previous Previous value in the sequence.
054     * @param index Index of the value that did not match the requirements.
055     */
056    public NonMonotonicSequenceException(Number wrong,
057                                         Number previous,
058                                         int index) {
059        this(wrong, previous, index, true, true);
060    }
061
062    /**
063     * Construct the exception.
064     *
065     * @param wrong Value that did not match the requirements.
066     * @param previous Previous value in the sequence.
067     * @param index Index of the value that did not match the requirements.
068     * @param increasing {@code true} for a sequence required to be
069     * increasing, {@code false} for a decreasing sequence.
070     * @param strict Whether the sequence must be strictly increasing or
071     * decreasing.
072     */
073    public NonMonotonicSequenceException(Number wrong,
074                                         Number previous,
075                                         int index,
076                                         boolean increasing,
077                                         boolean strict) {
078        super(increasing ?
079              (strict ?
080               LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
081               LocalizedFormats.NOT_INCREASING_SEQUENCE) :
082              (strict ?
083               LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
084               LocalizedFormats.NOT_DECREASING_SEQUENCE),
085              wrong, previous, Integer.valueOf(index), Integer.valueOf(index - 1));
086
087        this.increasing = increasing;
088        this.strict = strict;
089        this.index = index;
090        this.previous = previous;
091    }
092
093    /**
094     * @return {@code true} if the sequence should be increasing.
095     **/
096    public boolean getIncreasing() {
097        return increasing;
098    }
099    /**
100     * @return {@code true} is the sequence should be strictly monotonic.
101     **/
102    public boolean getStrict() {
103        return strict;
104    }
105    /**
106     * Get the index of the wrong value.
107     *
108     * @return the current index.
109     */
110    public int getIndex() {
111        return index;
112    }
113    /**
114     * @return the previous value.
115     */
116    public Number getPrevious() {
117        return previous;
118    }
119}