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.math4.legacy.exception;

  18. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;

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

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

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

  82.         this.increasing = increasing;
  83.         this.strict = strict;
  84.         this.index = index;
  85.         this.previous = previous;
  86.     }

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