001package com.google.common.base;
002
003import static com.google.common.base.Preconditions.format;
004
005import java.lang.reflect.InvocationTargetException;
006
007import javax.annotation.Nullable;
008
009/**
010 * 条件检查工具类
011 * @author guyadong
012 *
013 */
014public class ConditionChecks {
015
016        private ConditionChecks() {
017        }
018        /**
019         * 执行表达式,为false时抛出 declareType 异常
020         * @param <X> 抛出异常类型
021         * @param b
022         * @param declareType 异常类型
023         * @param errorMessageTemplate a template for the exception message should the check fail. The
024         *     message is formed by replacing each {@code %s} placeholder in the template with an
025         *     argument. These are matched by position - the first {@code %s} gets {@code
026         *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
027         *     square braces. Unmatched placeholders will be left as-is.
028         * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
029         *     are converted to strings using {@link String#valueOf(Object)}.
030         * @throws X
031         */
032        public static <X extends Throwable> void checkTrue(
033                        boolean b, 
034                        Class<X> declareType, 
035                        @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) throws X {
036                if (!b) {
037                        try {
038                                throw com.google.common.base.Preconditions.checkNotNull(declareType,"declareType is null").getConstructor(String.class).newInstance(format(errorMessageTemplate, errorMessageArgs));
039                        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
040                                        | InvocationTargetException | NoSuchMethodException | SecurityException e) {
041                                e.printStackTrace();
042                        }
043                }
044        }
045        /**
046         * 执行表达式,为false时抛出 declareType 异常
047         *
048         * <p>See {@link #checkTrue(boolean, Class, String, Object...)} for details.
049         * @throws X 
050         */
051        public static <X extends Throwable> void checkTrue(
052                        boolean b, 
053                        Class<X> declareType, 
054                        @Nullable String errorMessageTemplate, @Nullable Object p1) throws X {
055                if (!b) {
056                        try {
057                                throw com.google.common.base.Preconditions.checkNotNull(declareType,"declareType is null").getConstructor(String.class).newInstance(format(errorMessageTemplate, p1));
058                        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
059                                        | InvocationTargetException | NoSuchMethodException | SecurityException e) {
060                                e.printStackTrace();
061                        }       
062                }
063        }
064        /**
065         * reference为{@code null}时抛出 declareType 异常
066         * @param <T> 对象类型
067         * @param <X> 抛出异常类型
068         * @param reference
069         * @param declareType 异常类型
070         * @param errorMessageTemplate a template for the exception message should the check fail. The
071         *     message is formed by replacing each {@code %s} placeholder in the template with an
072         *     argument. These are matched by position - the first {@code %s} gets {@code
073         *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
074         *     square braces. Unmatched placeholders will be left as-is.
075         * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
076         *     are converted to strings using {@link String#valueOf(Object)}.
077         * @return 
078         * @throws X
079         */
080        public static <T,X extends Throwable> T checkNotNull(
081                        T reference, 
082                        Class<X> declareType, 
083                        @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) throws X {
084                if (null==reference) {
085                        try {
086                                throw com.google.common.base.Preconditions.checkNotNull(declareType,"declareType is null").getConstructor(String.class).newInstance(format(errorMessageTemplate, errorMessageArgs));
087                        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
088                                        | InvocationTargetException | NoSuchMethodException | SecurityException e) {
089                                e.printStackTrace();
090                        }
091                }
092                return reference;
093        }
094        /**
095         * reference为{@code null}时抛出 declareType 异常
096         *
097         * <p>See {@link #checkNotNull(boolean, Class, String, Object...)} for details.
098         * @return 
099         * @throws X 
100         */
101        public static <T,X extends Throwable> T checkNotNull(
102                        T reference,
103                        Class<X> declareType, 
104                        @Nullable String errorMessageTemplate, @Nullable Object p1) throws X {
105                if (null==reference) {
106                        try {
107                                throw com.google.common.base.Preconditions.checkNotNull(declareType,"declareType is null").getConstructor(String.class).newInstance(format(errorMessageTemplate, p1));
108                        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
109                                        | InvocationTargetException | NoSuchMethodException | SecurityException e) {
110                                e.printStackTrace();
111                        }       
112                }
113                return reference;
114        }
115}