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