001package org.kuali.common.util.base;
002
003/**
004 * <p>
005 * Create {@code IllegalStateException's} and {@code IllegaArgumentException's} with richly formatted error messages.
006 * </p>
007 * 
008 * Example usage:
009 * 
010 * <pre>
011 * throw Exceptions.illegalArg(&quot;port must be &gt;= %s and &lt;= %s&quot;, 0, 65535);
012 * </pre>
013 */
014public class Exceptions {
015
016        public static IllegalStateException illegalState(Throwable cause) {
017                return new IllegalStateException(cause);
018        }
019
020        public static IllegalStateException illegalState(String msg) {
021                return new IllegalStateException(msg);
022        }
023
024        public static IllegalStateException illegalState(String msg, Object... args) {
025                return new IllegalStateException(formattedMessage(msg, args));
026        }
027
028        public static IllegalStateException illegalState(Throwable cause, String msg, Object... args) {
029                return new IllegalStateException(formattedMessage(msg, args), cause);
030        }
031
032        public static IllegalArgumentException illegalArg(Throwable cause) {
033                return new IllegalArgumentException(cause);
034        }
035
036        public static IllegalArgumentException illegalArg(String msg) {
037                return new IllegalArgumentException(msg);
038        }
039
040        public static IllegalArgumentException illegalArg(String msg, Object... args) {
041                return new IllegalArgumentException(formattedMessage(msg, args));
042        }
043
044        public static IllegalArgumentException illegalArg(Throwable cause, String msg, Object... args) {
045                return new IllegalArgumentException(formattedMessage(msg, args), cause);
046        }
047
048        protected static String formattedMessage(String msg, Object... args) {
049                if (args == null || args.length == 0) {
050                        return msg;
051                } else {
052                        return String.format(msg, args);
053                }
054        }
055
056}