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 specific argument of a method has been passed with a positive integer reference that does 22 * not accept it as valid. For example, if a method needs a negative value and is passed with <code>42</code> then this 23 * exception should be thrown. 24 * 25 * @author André Rouél 26 * @author Dominik Seichter 27 */ 28 public class IllegalPositiveArgumentException extends RuntimeException { 29 30 /** 31 * 32 */ 33 private static final long serialVersionUID = -2636014042680095222L; 34 35 /** 36 * Default message to indicate that the a given argument must not be empty. 37 */ 38 protected static final String DEFAULT_MESSAGE = "The passed argument must be smaller than 0."; 39 40 /** 41 * Message to indicate that the the given argument with <em>name</em> must not be empty. 42 */ 43 protected static final String MESSAGE_WITH_NAME = "The passed argument '%s' must be smaller than 0."; 44 45 /** 46 * Determines the message to be used, depending on the passed argument name. If if the given argument name is 47 * {@code null} or empty {@code DEFAULT_MESSAGE} will be returned, otherwise a formatted {@code MESSAGE_WITH_NAME} 48 * with the passed name. 49 * 50 * @param argumentName 51 * the name of the passed argument 52 * @return {@code DEFAULT_MESSAGE} if the given argument name is {@code null} or empty, otherwise a formatted 53 * {@code MESSAGE_WITH_NAME} 54 */ 55 private static String determineMessage(@Nullable final String argumentName) { 56 return argumentName != null && !argumentName.isEmpty() ? format(argumentName) : DEFAULT_MESSAGE; 57 } 58 59 /** 60 * Returns the formatted string {@link IllegalPositiveArgumentException#MESSAGE_WITH_NAME} with the given 61 * {@code argumentName}. 62 * 63 * @param argumentName 64 * the name of the passed argument 65 * @return a formatted string of message with the given argument name 66 */ 67 private static String format(@Nullable final String argumentName) { 68 return String.format(MESSAGE_WITH_NAME, argumentName); 69 } 70 71 /** 72 * Constructs an {@code IllegalNullArgumentException} with the default message 73 * {@link IllegalPositiveArgumentException#DEFAULT_MESSAGE}. 74 */ 75 public IllegalPositiveArgumentException() { 76 super(DEFAULT_MESSAGE); 77 } 78 79 /** 80 * Constructs an {@code IllegalNullArgumentException} with the message 81 * {@link IllegalPositiveArgumentException#MESSAGE_WITH_NAME} including the given name of the argument as string 82 * representation. 83 * 84 * @param argumentName 85 * the name of the passed argument 86 */ 87 public IllegalPositiveArgumentException(@Nullable final String argumentName) { 88 super(determineMessage(argumentName)); 89 } 90 91 /** 92 * Constructs a new exception with the message {@link IllegalPositiveArgumentException#MESSAGE_WITH_NAME} including 93 * the given name as string representation and cause. 94 * 95 * @param argumentName 96 * the name of the passed argument 97 * @param cause 98 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A 99 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) 100 */ 101 public IllegalPositiveArgumentException(@Nullable final String argumentName, @Nullable final Throwable cause) { 102 super(determineMessage(argumentName), cause); 103 } 104 105 /** 106 * Constructs a new exception with the default message {@link IllegalPositiveArgumentException#DEFAULT_MESSAGE}. 107 * 108 * @param cause 109 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A 110 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) 111 */ 112 public IllegalPositiveArgumentException(@Nullable final Throwable cause) { 113 super(DEFAULT_MESSAGE, cause); 114 } 115 116 }