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