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.Nonnull;
19 import javax.annotation.Nullable;
20
21 /**
22 * Thrown to indicate that a method was passed arguments which caused an invalid state.
23 *
24 * @author André Rouél
25 * @author Dominik Seichter
26 */
27 public class IllegalStateOfArgumentException extends RuntimeException {
28
29 private static final long serialVersionUID = -1782626786560016442L;
30
31 /**
32 * Default message to indicate that the given arguments caused an invalid state.
33 */
34 protected static final String DEFAULT_MESSAGE = "Arguments must be valid with the current state.";
35
36 /**
37 * Message to indicate that an invalid state was caused due to the passed arguments, which also
38 * provides an explanation why the state is invalid.
39 */
40 protected static final String MESSAGE_DESCRIPTION = "The passed arguments have caused an invalid state: ";
41
42 private static String format(@Nonnull final String descriptionTemplate, Object... descriptionTemplateArgs) {
43 if( descriptionTemplateArgs == null || descriptionTemplateArgs.length == 0 ) {
44 return MESSAGE_DESCRIPTION + descriptionTemplate;
45 } else {
46 return String.format(descriptionTemplate, descriptionTemplateArgs);
47 }
48 }
49
50 /**
51 * Constructs an {@code IllegalStateOfArgumentException} with the default message
52 * {@link IllegalStateOfArgumentException#DEFAULT_MESSAGE}.
53 */
54 public IllegalStateOfArgumentException() {
55 super(DEFAULT_MESSAGE);
56 }
57
58 /**
59 * Constructs an {@code IllegalStateOfArgumentException} with the message {@link IllegalStateOfArgumentException#MESSAGE_DESCRIPTION}
60 * including the given values of the arguments.
61 *
62 * @param description
63 * explains why the state is invalid
64 */
65 public IllegalStateOfArgumentException(@Nonnull final String description) {
66 super(format(description));
67 }
68
69 /**
70 * Constructs an {@code IllegalStateOfArgumentException} with the message {@link IllegalStateOfArgumentException#MESSAGE_DESCRIPTION}
71 * including the given values of the arguments.
72 *
73 * @param description
74 * format string template that explains why the state is invalid
75 * @param descriptionTemplateArgs
76 * format string template arguments to explain why the state is invalid
77 */
78 public IllegalStateOfArgumentException(@Nonnull final String description, Object... descriptionTemplateArgs) {
79 super(format(description, descriptionTemplateArgs));
80 }
81
82 /**
83 * Constructs a new exception with the message {@link IllegalStateOfArgumentException#MESSAGE_DESCRIPTION} including the given
84 * values of the arguments.
85 *
86 * @param description
87 * explains why the state is invalid
88 * @param cause
89 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
90 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
91 */
92 public IllegalStateOfArgumentException(@Nonnull final String description, @Nullable final Throwable cause) {
93 super(format(description), cause);
94 }
95
96 /**
97 * Constructs a new exception with the message {@link IllegalStateOfArgumentException#MESSAGE_DESCRIPTION} including the given
98 * values of the arguments.
99 *
100 * @param cause
101 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
102 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
103 * @param description
104 * format string template that explains why the state is invalid
105 * @param descriptionTemplateArgs
106 * format string template arguments to explain why the state is invalid
107 */
108 public IllegalStateOfArgumentException(@Nullable final Throwable cause, @Nonnull final String description, Object... descriptionTemplateArgs) {
109 super(format(description, descriptionTemplateArgs), cause);
110 }
111
112 /**
113 * Constructs a new exception with the default message {@link IllegalStateOfArgumentException#DEFAULT_MESSAGE}.
114 *
115 * @param cause
116 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
117 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
118 */
119 public IllegalStateOfArgumentException(@Nullable final Throwable cause) {
120 super(DEFAULT_MESSAGE, cause);
121 }
122
123 }