Raymond | dee0849 | 2015-04-02 10:43:13 -0700 | [diff] [blame] | 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.math.exception; |
| 18 | |
| 19 | import org.apache.commons.math.util.MathUtils; |
| 20 | import org.apache.commons.math.exception.util.LocalizedFormats; |
| 21 | |
| 22 | /** |
| 23 | * Exception to be thrown when the a sequence of values is not monotonously |
| 24 | * increasing or decreasing. |
| 25 | * |
| 26 | * @since 2.2 |
| 27 | * @version $Revision$ $Date$ |
| 28 | */ |
| 29 | public class NonMonotonousSequenceException extends MathIllegalNumberException { |
| 30 | |
| 31 | /** Serializable version Id. */ |
| 32 | private static final long serialVersionUID = 3596849179428944575L; |
| 33 | |
| 34 | /** |
| 35 | * Direction (positive for increasing, negative for decreasing). |
| 36 | */ |
| 37 | private final MathUtils.OrderDirection direction; |
| 38 | /** |
| 39 | * Whether the sequence must be strictly increasing or decreasing. |
| 40 | */ |
| 41 | private final boolean strict; |
| 42 | /** |
| 43 | * Index of the wrong value. |
| 44 | */ |
| 45 | private final int index; |
| 46 | /** |
| 47 | * Previous value. |
| 48 | */ |
| 49 | private final Number previous; |
| 50 | |
| 51 | /** |
| 52 | * Construct the exception. |
| 53 | * This constructor uses default values assuming that the sequence should |
| 54 | * have been strictly increasing. |
| 55 | * |
| 56 | * @param wrong Value that did not match the requirements. |
| 57 | * @param previous Previous value in the sequence. |
| 58 | * @param index Index of the value that did not match the requirements. |
| 59 | */ |
| 60 | public NonMonotonousSequenceException(Number wrong, |
| 61 | Number previous, |
| 62 | int index) { |
| 63 | this(wrong, previous, index, MathUtils.OrderDirection.INCREASING, true); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Construct the exception. |
| 68 | * |
| 69 | * @param wrong Value that did not match the requirements. |
| 70 | * @param previous Previous value in the sequence. |
| 71 | * @param index Index of the value that did not match the requirements. |
| 72 | * @param direction Strictly positive for a sequence required to be |
| 73 | * increasing, negative (or zero) for a decreasing sequence. |
| 74 | * @param strict Whether the sequence must be strictly increasing or |
| 75 | * decreasing. |
| 76 | */ |
| 77 | public NonMonotonousSequenceException(Number wrong, |
| 78 | Number previous, |
| 79 | int index, |
| 80 | MathUtils.OrderDirection direction, |
| 81 | boolean strict) { |
| 82 | super(direction == MathUtils.OrderDirection.INCREASING ? |
| 83 | (strict ? |
| 84 | LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE : |
| 85 | LocalizedFormats.NOT_INCREASING_SEQUENCE) : |
| 86 | (strict ? |
| 87 | LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE : |
| 88 | LocalizedFormats.NOT_DECREASING_SEQUENCE), |
| 89 | wrong, previous, index, index - 1); |
| 90 | |
| 91 | this.direction = direction; |
| 92 | this.strict = strict; |
| 93 | this.index = index; |
| 94 | this.previous = previous; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @return the order direction. |
| 99 | **/ |
| 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 | } |