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
19 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
20
21 /**
22 * Exception to be thrown when the a sequence of values is not monotonically
23 * increasing or decreasing.
24 *
25 * @since 2.2 (name changed to "NonMonotonicSequenceException" in 3.0)
26 */
27 public class NonMonotonicSequenceException extends MathIllegalNumberException {
28 /** Serializable version Id. */
29 private static final long serialVersionUID = 20210531L;
30 /**
31 * Whether the sequence should be increasing.
32 */
33 private final boolean increasing;
34 /**
35 * Whether the sequence must be strictly increasing or decreasing.
36 */
37 private final boolean strict;
38 /**
39 * Index of the wrong value.
40 */
41 private final int index;
42 /**
43 * Previous value.
44 */
45 private final Number previous;
46
47 /**
48 * Construct the exception.
49 * This constructor uses default values assuming that the sequence should
50 * have been strictly increasing.
51 *
52 * @param wrong Value that did not match the requirements.
53 * @param previous Previous value in the sequence.
54 * @param index Index of the value that did not match the requirements.
55 */
56 public NonMonotonicSequenceException(Number wrong,
57 Number previous,
58 int index) {
59 this(wrong, previous, index, true, true);
60 }
61
62 /**
63 * Construct the exception.
64 *
65 * @param wrong Value that did not match the requirements.
66 * @param previous Previous value in the sequence.
67 * @param index Index of the value that did not match the requirements.
68 * @param increasing {@code true} for a sequence required to be
69 * increasing, {@code false} for a decreasing sequence.
70 * @param strict Whether the sequence must be strictly increasing or
71 * decreasing.
72 */
73 public NonMonotonicSequenceException(Number wrong,
74 Number previous,
75 int index,
76 boolean increasing,
77 boolean strict) {
78 super(increasing ?
79 (strict ?
80 LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
81 LocalizedFormats.NOT_INCREASING_SEQUENCE) :
82 (strict ?
83 LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
84 LocalizedFormats.NOT_DECREASING_SEQUENCE),
85 wrong, previous, Integer.valueOf(index), Integer.valueOf(index - 1));
86
87 this.increasing = increasing;
88 this.strict = strict;
89 this.index = index;
90 this.previous = previous;
91 }
92
93 /**
94 * @return {@code true} if the sequence should be increasing.
95 **/
96 public boolean getIncreasing() {
97 return increasing;
98 }
99 /**
100 * @return {@code true} is the sequence should be strictly monotonic.
101 **/
102 public boolean getStrict() {
103 return strict;
104 }
105 /**
106 * Get the index of the wrong value.
107 *
108 * @return the current index.
109 */
110 public int getIndex() {
111 return index;
112 }
113 /**
114 * @return the previous value.
115 */
116 public Number getPrevious() {
117 return previous;
118 }
119 }