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