Java Assert assertPrecondition(boolean b)

Here you can find the source of assertPrecondition(boolean b)

Description

Checks that a precondition (declared using the javadoc @pre tag) is satisfied.

License

Apache License

Parameter

Parameter Description
b The value of executing the condition

Declaration

public static void assertPrecondition(boolean b) 

Method Source Code

//package com.java2s;
/*// ww  w .j ava 2  s .  c o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /**
     * Checks that a precondition (declared using the javadoc <code>@pre</code>
     * tag) is satisfied.
     *
     * @param b The value of executing the condition
     */
    public static void assertPrecondition(boolean b) {
        assertTrue(b);
    }

    public static void assertPrecondition(boolean b, String condition) {
        assertTrue(b, condition);
    }

    /**
     * Throws an internal error if condition is not true. It would be called
     * <code>assert</code>, but that is a keyword as of JDK 1.4.
     */
    public static void assertTrue(boolean b) {
        if (!b) {
            newInternal("assert failed");
        }
    }

    public static void assertTrue(boolean b, String message) {
        if (!b) {
            newInternal("assert failed: " + message);
        }
    }

    /**
     * Creates an internal error with a given message.
     */
    public static RuntimeException newInternal(String message) {
        return new RuntimeException(message);
    }

    /**
     * Creates an internal error with a given message and cause.
     */
    public static RuntimeException newInternal(Throwable e, String message) {
        return new RuntimeException(message, e);
    }
}

Related

  1. assertParameterWithinBounds(String name, long lower, long upper, long parameter)
  2. assertPasswordMeetsRequirements(String password, boolean expected)
  3. assertPositive(int num, String err)
  4. assertPositive(int val, String msg)
  5. assertPositive(int value)
  6. assertQualifiedString(String str, String argName)
  7. assertRange(String label, double value, double min, double max)
  8. assertRequiredArgs(Object[] methodArgs, int requiredArgs, String methodName)
  9. assertSame(Object expected, Object actual)