View Javadoc

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 an illegal not {@code null} reference as argument where
22   * {@code null} is required.
23   * 
24   * @author Dominik Seichter
25   */
26  public class IllegalNotNullArgumentException extends RuntimeException {
27  
28  	private static final long serialVersionUID = -6988558700678645359L;
29  
30  	/**
31  	 * Default message to indicate that the a given argument must be a {@code null} reference.
32  	 */
33  	protected static final String DEFAULT_MESSAGE = "Argument must be null.";
34  
35  	/**
36  	 * Message to indicate that the the given argument with <em>name</em> must be a {@code null} reference.
37  	 */
38  	protected static final String MESSAGE_WITH_NAME = "Argument '%s' must be null.";
39  
40  	/**
41  	 * Returns the formatted string {@link IllegalNotNullArgumentException#MESSAGE_WITH_NAME} with the given
42  	 * {@code argumentName}.
43  	 * 
44  	 * @param argumentName
45  	 *            the name of the passed argument
46  	 * @return a formatted string of message with the given argument name
47  	 */
48  	private static String format(@Nullable final String argumentName) {
49  		return String.format(MESSAGE_WITH_NAME, argumentName);
50  	}
51  
52  	/**
53  	 * Constructs an {@code IllegalNotNullArgumentException} with the default message
54  	 * {@link IllegalNotNullArgumentException#DEFAULT_MESSAGE}.
55  	 */
56  	public IllegalNotNullArgumentException() {
57  		super(DEFAULT_MESSAGE);
58  	}
59  
60  	/**
61  	 * Constructs an {@code IllegalNotNullArgumentException} with the message
62  	 * {@link IllegalNotNullArgumentException#MESSAGE_WITH_NAME} including the given name of the argument as string
63  	 * representation.
64  	 * 
65  	 * @param argumentName
66  	 *            the name of the passed argument
67  	 */
68  	public IllegalNotNullArgumentException(@Nullable final String argumentName) {
69  		super(format(argumentName));
70  	}
71  
72  	/**
73  	 * Constructs a new exception with the message {@link IllegalNotNullArgumentException#MESSAGE_WITH_NAME} including
74  	 * the given name as string representation and cause.
75  	 * 
76  	 * @param argumentName
77  	 *            the name of the passed argument
78  	 * @param cause
79  	 *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
80  	 *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
81  	 */
82  	public IllegalNotNullArgumentException(@Nullable final String argumentName, @Nullable final Throwable cause) {
83  		super(format(argumentName), cause);
84  	}
85  
86  	/**
87  	 * Constructs a new exception with the default message {@link IllegalNotNullArgumentException#DEFAULT_MESSAGE}.
88  	 * 
89  	 * @param cause
90  	 *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
91  	 *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
92  	 */
93  	public IllegalNotNullArgumentException(@Nullable final Throwable cause) {
94  		super(DEFAULT_MESSAGE, cause);
95  	}
96  
97  }