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