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 passed position index was out of the bounds of a string, list, array ... .
22 *
23 *
24 * @author André Rouél
25 * @author Dominik Seichter
26 */
27 public class IllegalPositionIndexException extends RuntimeException {
28
29 private static final long serialVersionUID = 1012569127264822249L;
30
31 /**
32 * Default message to indicate that the a given position index is not valid.
33 */
34 protected static final String DEFAULT_MESSAGE = "Position index must be within the defined bounds.";
35
36 /**
37 * Default message to indicate that the a given position index is not valid within the given bounds.
38 */
39 protected static final String MESSAGE_WITH_VALUES = "Position index '%d' must be within the defined bounds [0,%d].";
40
41 private static String format(final int index, final int size) {
42 return String.format(MESSAGE_WITH_VALUES, index, size);
43 }
44
45 /**
46 * Constructs an {@code IllegalPositionIndexException} with the default message
47 * {@link IllegalPositionIndexException#DEFAULT_MESSAGE}.
48 */
49 public IllegalPositionIndexException() {
50 super(DEFAULT_MESSAGE);
51 }
52
53 /**
54 * Constructs an {@code IllegalPositionIndexException} with the message
55 * {@link IllegalPositionIndexException#MESSAGE_WITH_VALUES} including the given values of the arguments.
56 *
57 * @param index
58 * an index in an array, list or string
59 * @param size
60 * the size of an array, list or string
61 */
62 public IllegalPositionIndexException(final int index, final int size) {
63 super(format(index, size));
64 }
65
66 /**
67 * Constructs a new exception with the message {@link IllegalPositionIndexException#MESSAGE_WITH_VALUES} including
68 * the given values of the arguments.
69 *
70 * @param index
71 * an index in an array, list or string
72 * @param size
73 * the size of an array, list or string
74 * @param cause
75 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
76 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
77 */
78 public IllegalPositionIndexException(final int index, final int size, @Nullable final Throwable cause) {
79 super(format(index, size), cause);
80 }
81
82 /**
83 * Constructs a new exception with the default message {@link IllegalPositionIndexException#DEFAULT_MESSAGE}.
84 *
85 * @param cause
86 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
87 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
88 */
89 public IllegalPositionIndexException(@Nullable final Throwable cause) {
90 super(DEFAULT_MESSAGE, cause);
91 }
92
93 }