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