/*******************************************************************************
 * Copyright (c) 2013 EclipseSource and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    EclipseSource  - initial API and implementation
 *    Benjamin Pabst - further development
 ******************************************************************************/

package com.xeviox.commons.conditions;

import java.lang.reflect.InvocationTargetException;

public final class Clauses {

    public static class Clause {

        private final boolean condition;

        public Clause(boolean condition) {
            this.condition = condition;
        }

        public void throwIllegalState() {
            if (condition) {
                throw new IllegalStateException();
            }
        }

        public void throwIllegalState(String message) {
            if (condition) {
                throw new IllegalStateException(message);
            }
        }

        public void throwIllegalArgument() {
            if (condition) {
                throw new IllegalArgumentException();
            }
        }

        public void throwIllegalArgument(String message) {
            if (condition) {
                throw new IllegalArgumentException(message);
            }
        }

        public <T extends RuntimeException> void doThrow(T rt) {
            if (!condition) {
                return;
            }
            
            throw rt;
        }

        public void doThrow(Class<? extends RuntimeException> clazz) {
            if (!condition) {
                return;
            }
            
            try {
                throw clazz.newInstance();
            }
            catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        public void doThrow(Class<? extends RuntimeException> clazz, String message) {
            if (!condition) {
                return;
            }
            
            try {
                throw clazz.getDeclaredConstructor(String.class).newInstance(message);
            }
            catch (InstantiationException | IllegalAccessException | NoSuchMethodException
                    | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static Clause TRUE_CLAUSE = new Clause(true);
    private static Clause FALSE_CLAUSE = new Clause(false);

    public static Clause when(boolean condition) {
        return condition ? TRUE_CLAUSE : FALSE_CLAUSE;
    }

    public static Clause whenNot(boolean condition) {
        return when(!condition);
    }

    public static Clause whenNull(Object object) {
        return when(object == null);
    }

    private Clauses() {
        // prevent instantiation
    }
}
@@ -1045,16 +1045,22 @@
 alState(
+final 
 String m
@@ -1061,32 +1061,32 @@
 ring message) %7B%0A
-
             if (
@@ -1171,32 +1171,365 @@
      %7D%0A        %7D
+%0A        %0A        public void throwNullPointer() %7B%0A            if (condition) %7B%0A                throw new NullPointerException();%0A            %7D%0A        %7D%0A        %0A        public void throwNullPointer(final String message) %7B%0A            if (condition) %7B%0A                throw new NullPointerException(message);%0A            %7D%0A        %7D
 %0A%0A        public
@@ -1708,16 +1708,22 @@
 rgument(
+final 
 String m
@@ -2465,16 +2465,22 @@
 %3E clazz,
+ final
  String 
@@ -3285,24 +3285,24 @@
 t object) %7B%0A
-
         retu
@@ -3327,24 +3327,799 @@
 null);%0A    %7D
+%0A    %0A    /**%0A     * Performs a null check and returns the given reference.%0A     * %0A     * @param reference%0A     * @return reference%0A     * @throws NullPointerException if reference is null.%0A     */%0A    public static %3CT%3E T nullChecked(final T reference) %7B%0A        whenNull(reference).throwNullPointer();%0A        return reference;%0A    %7D%0A%0A    /**%0A     * Performs a null check and returns the given reference.%0A     * %0A     * @param reference%0A     * @param message Additional message to be added to the %7B@link NullPointerException%7D.%0A     * @return%0A     * @throws NullPointerException if reference is null.%0A     */%0A    public static %3CT%3E T nullChecked(final T reference, final String message) %7B%0A        whenNull(reference).throwNullPointer(message);%0A        return reference;%0A    %7D
 %0A%0A    privat

Add methods to throw NPE in clauses.