View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 org.kuali.common.util.base;
17  
18  import static java.lang.String.format;
19  
20  /**
21   * <p>
22   * Utility methods for creating {@code IllegalStateException's} and {@code IllegaArgumentException's} with richly formatted error messages.
23   * </p>
24   * 
25   * Example usage:
26   * 
27   * <pre>
28   * throw Exceptions.illegalArgument(&quot;port must be &gt;= %s and &lt;= %s&quot;, 0, 65535);
29   * </pre>
30   */
31  public class Exceptions {
32  
33  	public static IllegalStateException illegalState(Throwable cause) {
34  		return new IllegalStateException(cause);
35  	}
36  
37  	public static IllegalStateException illegalState(String msg) {
38  		return new IllegalStateException(msg);
39  	}
40  
41  	public static IllegalStateException illegalState(String msg, Object... args) {
42  		return new IllegalStateException(formattedMessage(msg, args));
43  	}
44  
45  	public static IllegalStateException illegalState(Throwable cause, String msg, Object... args) {
46  		return new IllegalStateException(formattedMessage(msg, args), cause);
47  	}
48  
49  	public static IllegalArgumentException illegalArgument(Throwable cause) {
50  		return new IllegalArgumentException(cause);
51  	}
52  
53  	public static IllegalArgumentException illegalArgument(String msg) {
54  		return new IllegalArgumentException(msg);
55  	}
56  
57  	public static IllegalArgumentException illegalArgument(String msg, Object... args) {
58  		return new IllegalArgumentException(formattedMessage(msg, args));
59  	}
60  
61  	public static IllegalArgumentException illegalArgument(Throwable cause, String msg, Object... args) {
62  		return new IllegalArgumentException(formattedMessage(msg, args), cause);
63  	}
64  
65  	protected static String formattedMessage(String msg, Object... args) {
66  		if (args == null || args.length == 0) {
67  			return msg;
68  		} else {
69  			return format(msg, args);
70  		}
71  	}
72  
73  }