1 /*******************************************************************************
2 * Copyright 2013 André Rouél and Dominik Seichter
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16 package net.sf.qualitycheck.exception;
17
18 import javax.annotation.Nullable;
19
20 /**
21 * Thrown to indicate that a method has been passed with a range as arguments.
22 *
23 * A range (<em>start</em>, <em>end</em>, <em>size</em>) is valid if the following conditions are true: - start < size -
24 * end < size - start < end
25 *
26 * @author André Rouél
27 * @author Dominik Seichter
28 */
29 public class IllegalRangeException extends RuntimeException {
30
31 private static final long serialVersionUID = 4515679658955102518L;
32
33 /**
34 * Default message to indicate that the a given arguments are no valid range.
35 */
36 protected static final String DEFAULT_MESSAGE = "Arguments must be a valid range.";
37
38 /**
39 * Message to indicate that the the given arguments <em>start</em>, <em>end</em> and <em>size</em> must be a valid
40 * range.
41 */
42 protected static final String MESSAGE_WITH_VALUES = "Arguments start='%d', end='%d' and size='%d' must be a valid range.";
43
44 private static String format(final int start, final int end, final int size) {
45 return String.format(MESSAGE_WITH_VALUES, start, end, size);
46 }
47
48 /**
49 * Constructs an {@code IllegalRangeException} with the default message
50 * {@link IllegalRangeException#DEFAULT_MESSAGE}.
51 */
52 public IllegalRangeException() {
53 super(DEFAULT_MESSAGE);
54 }
55
56 /**
57 * Constructs an {@code IllegalRangeException} with the message {@link IllegalRangeException#MESSAGE_WITH_VALUES}
58 * including the given values of the arguments.
59 *
60 * @param start
61 * the start value of the invalid range
62 * @param end
63 * the end value of the invalid range
64 * @param size
65 * the size value of the invalid range
66 */
67 public IllegalRangeException(final int start, final int end, final int size) {
68 super(format(start, end, size));
69 }
70
71 /**
72 * Constructs a new exception with the message {@link IllegalRangeException#MESSAGE_WITH_VALUES} including the given
73 * values of the arguments.
74 *
75 * @param start
76 * the start value of the invalid range
77 * @param end
78 * the end value of the invalid range
79 * @param size
80 * the size value of the invalid range
81 * @param cause
82 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
83 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
84 */
85 public IllegalRangeException(final int start, final int end, final int size, @Nullable final Throwable cause) {
86 super(format(start, end, size), cause);
87 }
88
89 /**
90 * Constructs a new exception with the default message {@link IllegalRangeException#DEFAULT_MESSAGE}.
91 *
92 * @param cause
93 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
94 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
95 */
96 public IllegalRangeException(@Nullable final Throwable cause) {
97 super(DEFAULT_MESSAGE, cause);
98 }
99
100 }