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