NonMonotonicSequenceException.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math3.exception;

  18. import org.apache.commons.math3.util.MathArrays;
  19. import org.apache.commons.math3.exception.util.LocalizedFormats;

  20. /**
  21.  * Exception to be thrown when the a sequence of values is not monotonically
  22.  * increasing or decreasing.
  23.  *
  24.  * @since 2.2 (name changed to "NonMonotonicSequenceException" in 3.0)
  25.  */
  26. public class NonMonotonicSequenceException extends MathIllegalNumberException {
  27.     /** Serializable version Id. */
  28.     private static final long serialVersionUID = 3596849179428944575L;
  29.     /**
  30.      * Direction (positive for increasing, negative for decreasing).
  31.      */
  32.     private final MathArrays.OrderDirection direction;
  33.     /**
  34.      * Whether the sequence must be strictly increasing or decreasing.
  35.      */
  36.     private final boolean strict;
  37.     /**
  38.      * Index of the wrong value.
  39.      */
  40.     private final int index;
  41.     /**
  42.      * Previous value.
  43.      */
  44.     private final Number previous;

  45.     /**
  46.      * Construct the exception.
  47.      * This constructor uses default values assuming that the sequence should
  48.      * have been strictly increasing.
  49.      *
  50.      * @param wrong Value that did not match the requirements.
  51.      * @param previous Previous value in the sequence.
  52.      * @param index Index of the value that did not match the requirements.
  53.      */
  54.     public NonMonotonicSequenceException(Number wrong,
  55.                                          Number previous,
  56.                                          int index) {
  57.         this(wrong, previous, index, MathArrays.OrderDirection.INCREASING, true);
  58.     }

  59.     /**
  60.      * Construct the exception.
  61.      *
  62.      * @param wrong Value that did not match the requirements.
  63.      * @param previous Previous value in the sequence.
  64.      * @param index Index of the value that did not match the requirements.
  65.      * @param direction Strictly positive for a sequence required to be
  66.      * increasing, negative (or zero) for a decreasing sequence.
  67.      * @param strict Whether the sequence must be strictly increasing or
  68.      * decreasing.
  69.      */
  70.     public NonMonotonicSequenceException(Number wrong,
  71.                                          Number previous,
  72.                                          int index,
  73.                                          MathArrays.OrderDirection direction,
  74.                                          boolean strict) {
  75.         super(direction == MathArrays.OrderDirection.INCREASING ?
  76.               (strict ?
  77.                LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
  78.                LocalizedFormats.NOT_INCREASING_SEQUENCE) :
  79.               (strict ?
  80.                LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
  81.                LocalizedFormats.NOT_DECREASING_SEQUENCE),
  82.               wrong, previous, Integer.valueOf(index), Integer.valueOf(index - 1));

  83.         this.direction = direction;
  84.         this.strict = strict;
  85.         this.index = index;
  86.         this.previous = previous;
  87.     }

  88.     /**
  89.      * @return the order direction.
  90.      **/
  91.     public MathArrays.OrderDirection getDirection() {
  92.         return direction;
  93.     }
  94.     /**
  95.      * @return {@code true} is the sequence should be strictly monotonic.
  96.      **/
  97.     public boolean getStrict() {
  98.         return strict;
  99.     }
  100.     /**
  101.      * Get the index of the wrong value.
  102.      *
  103.      * @return the current index.
  104.      */
  105.     public int getIndex() {
  106.         return index;
  107.     }
  108.     /**
  109.      * @return the previous value.
  110.      */
  111.     public Number getPrevious() {
  112.         return previous;
  113.     }
  114. }