    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     */
    017    package org.apache.commons.math3.exception;
    018
    019    import org.apache.commons.math3.util.MathArrays;
    020    import org.apache.commons.math3.exception.util.LocalizedFormats;
    021
    022    /**
    023     * Exception to be thrown when the a sequence of values is not monotonically
    024     * increasing or decreasing.
    025     *
    026     * @since 2.2 (name changed to "NonMonotonicSequenceException" in 3.0)
    027     * @version $Id: NonMonotonicSequenceException.java 1364378 2012-07-22 17:42:38Z tn $
    028     */
    029    public class NonMonotonicSequenceException extends MathIllegalNumberException {
    030        /** Serializable version Id. */
    031        private static final long serialVersionUID = 3596849179428944575L;
    032        /**
    033         * Direction (positive for increasing, negative for decreasing).
    034         */
    035        private final MathArrays.OrderDirection direction;
    036        /**
    037         * Whether the sequence must be strictly increasing or decreasing.
    038         */
    039        private final boolean strict;
    040        /**
    041         * Index of the wrong value.
    042         */
    043        private final int index;
    044        /**
    045         * Previous value.
    046         */
    047        private final Number previous;
    048
    049        /**
    050         * Construct the exception.
    051         * This constructor uses default values assuming that the sequence should
    052         * have been strictly increasing.
    053         *
    054         * @param wrong Value that did not match the requirements.
    055         * @param previous Previous value in the sequence.
    056         * @param index Index of the value that did not match the requirements.
    057         */
    058        public NonMonotonicSequenceException(Number wrong,
    059                                             Number previous,
    060                                             int index) {
    061            this(wrong, previous, index, MathArrays.OrderDirection.INCREASING, true);
    062        }
    063
    064        /**
    065         * Construct the exception.
    066         *
    067         * @param wrong Value that did not match the requirements.
    068         * @param previous Previous value in the sequence.
    069         * @param index Index of the value that did not match the requirements.
    070         * @param direction Strictly positive for a sequence required to be
    071         * increasing, negative (or zero) for a decreasing sequence.
    072         * @param strict Whether the sequence must be strictly increasing or
    073         * decreasing.
    074         */
    075        public NonMonotonicSequenceException(Number wrong,
    076                                             Number previous,
    077                                             int index,
    078                                             MathArrays.OrderDirection direction,
    079                                             boolean strict) {
    080            super(direction == MathArrays.OrderDirection.INCREASING ?
    081                  (strict ?
    082                   LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
    083                   LocalizedFormats.NOT_INCREASING_SEQUENCE) :
    084                  (strict ?
    085                   LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
    086                   LocalizedFormats.NOT_DECREASING_SEQUENCE),
    087                  wrong, previous, index, index - 1);
    088
    089            this.direction = direction;
    090            this.strict = strict;
    091            this.index = index;
    092            this.previous = previous;
    093        }
    094
    095        /**
    096         * @return the order direction.
    097         **/
    098        public MathArrays.OrderDirection getDirection() {
    099            return direction;
    100        }
    101        /**
    102         * @return {@code true} is the sequence should be strictly monotonic.
    103         **/
    104        public boolean getStrict() {
    105            return strict;
    106        }
    107        /**
    108         * Get the index of the wrong value.
    109         *
    110         * @return the current index.
    111         */
    112        public int getIndex() {
    113            return index;
    114        }
    115        /**
    116         * @return the previous value.
    117         */
    118        public Number getPrevious() {
    119            return previous;
    120        }
    121    }