1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.exception;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Exception to be thrown when the a sequence of values is not monotonously
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * increasing or decreasing.
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.2
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision$ $Date$
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class NonMonotonousSequenceException extends MathIllegalNumberException {
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version Id. */
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = 3596849179428944575L;
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Direction (positive for increasing, negative for decreasing).
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final MathUtils.OrderDirection direction;
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Whether the sequence must be strictly increasing or decreasing.
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final boolean strict;
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Index of the wrong value.
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final int index;
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Previous value.
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final Number previous;
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct the exception.
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * This constructor uses default values assuming that the sequence should
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * have been strictly increasing.
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param wrong Value that did not match the requirements.
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param previous Previous value in the sequence.
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param index Index of the value that did not match the requirements.
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public NonMonotonousSequenceException(Number wrong,
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                          Number previous,
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                          int index) {
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this(wrong, previous, index, MathUtils.OrderDirection.INCREASING, true);
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct the exception.
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param wrong Value that did not match the requirements.
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param previous Previous value in the sequence.
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param index Index of the value that did not match the requirements.
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param direction Strictly positive for a sequence required to be
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * increasing, negative (or zero) for a decreasing sequence.
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param strict Whether the sequence must be strictly increasing or
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * decreasing.
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public NonMonotonousSequenceException(Number wrong,
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                          Number previous,
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                          int index,
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                          MathUtils.OrderDirection direction,
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                          boolean strict) {
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(direction == MathUtils.OrderDirection.INCREASING ?
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond              (strict ?
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond               LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond               LocalizedFormats.NOT_INCREASING_SEQUENCE) :
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond              (strict ?
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond               LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond               LocalizedFormats.NOT_DECREASING_SEQUENCE),
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond              wrong, previous, index, index - 1);
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.direction = direction;
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.strict = strict;
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.index = index;
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.previous = previous;
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the order direction.
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     **/
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MathUtils.OrderDirection getDirection() {
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return direction;
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} is the sequence should be strictly monotonous.
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     **/
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public boolean getStrict() {
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return strict;
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Get the index of the wrong value.
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the current index.
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public int getIndex() {
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return index;
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the previous value.
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Number getPrevious() {
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return previous;
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
124