View Javadoc
1   package org.kuali.common.util.base;
2   
3   import static java.lang.String.format;
4   
5   /**
6    * <p>
7    * Utility methods for creating {@code IllegalStateException's} and {@code IllegaArgumentException's} with richly formatted error messages.
8    * </p>
9    * 
10   * Example usage:
11   * 
12   * <pre>
13   * throw Exceptions.illegalArgument(&quot;port must be &gt;= %s and &lt;= %s&quot;, 0, 65535);
14   * </pre>
15   */
16  public class Exceptions {
17  
18  	public static IllegalStateException illegalState(Throwable cause) {
19  		return new IllegalStateException(cause);
20  	}
21  
22  	public static IllegalStateException illegalState(String msg) {
23  		return new IllegalStateException(msg);
24  	}
25  
26  	public static IllegalStateException illegalState(String msg, Object... args) {
27  		return new IllegalStateException(formattedMessage(msg, args));
28  	}
29  
30  	public static IllegalStateException illegalState(Throwable cause, String msg, Object... args) {
31  		return new IllegalStateException(formattedMessage(msg, args), cause);
32  	}
33  
34  	public static IllegalArgumentException illegalArgument(Throwable cause) {
35  		return new IllegalArgumentException(cause);
36  	}
37  
38  	public static IllegalArgumentException illegalArgument(String msg) {
39  		return new IllegalArgumentException(msg);
40  	}
41  
42  	public static IllegalArgumentException illegalArgument(String msg, Object... args) {
43  		return new IllegalArgumentException(formattedMessage(msg, args));
44  	}
45  
46  	public static IllegalArgumentException illegalArgument(Throwable cause, String msg, Object... args) {
47  		return new IllegalArgumentException(formattedMessage(msg, args), cause);
48  	}
49  
50  	protected static String formattedMessage(String msg, Object... args) {
51  		if (args == null || args.length == 0) {
52  			return msg;
53  		} else {
54  			return format(msg, args);
55  		}
56  	}
57  
58  }