type.lang
Class SE

java.lang.Object
  extended by type.lang.SE

public class SE
extends java.lang.Object

The SE (Software Engineering)class provides a collection of static services to implement design-by-contract assertions. All methods in this class are static and, hence, you simply invoke them on the class itself.

The class provides 4 methods that are meant to implement assertions in various contexts and throw corresponding exceptions:

Each of the four methods comes in two overloaded variants to enable printing a custom message upon termination.

Assertion Example:

 import type.lang.*;
 public class Assert
 {  public static void main (String[] args)
    {  // Outputs the largest of three distinct entered integers.
       IO.println("Enter three distinct integers, pressing ENTER after each");
       int a = IO.readInt();
       int b = IO.readInt();
       int c = IO.readInt();
       SE.require(a != b && a != c && b != c);  // Precondition:
       int max;
       if (a > b && a > c)
       {  max = a;
       } else if (b > a && b > c)
       {  max = b;
       } else
       {  SE.check(c > a && c > b);  // Assertion
          max = c;
       }
       SE.ensure(max >= a && max >= b && max >= c);  // Postcondition
       IO.println("Their largest is: " + max);
    }
 }
Note: pre/post conditions are typically applied in the context of methods. The above example extends their notion to an app context in which the main method can be viewed as implementing a contract between the (human) user of the app and its developer.

Version:
1.5 (Summer 2002)
Author:
H. Roumani, roumani@cs.yorku.ca

Method Summary
static void check(boolean condition)
          Assert that the specified condition is met or else terminate.
static void check(boolean condition, java.lang.String msg)
          Assert that the specified condition is met or else terminate.
static void ensure(boolean condition)
          Assert that the specified condition is met or else terminate.
static void ensure(boolean condition, java.lang.String msg)
          Assert that the specified condition is met or else terminate.
static void invariant(boolean condition)
          Assert that the specified condition is met or else terminate.
static void invariant(boolean condition, java.lang.String msg)
          Assert that the specified condition is met or else terminate.
static void require(boolean condition)
          Assert that the specified condition is met or else terminate.
static void require(boolean condition, java.lang.String msg)
          Assert that the specified condition is met or else terminate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

require

public static void require(boolean condition,
                           java.lang.String msg)
Assert that the specified condition is met or else terminate. Test the passed condition and if false then terminate the program with a Precondition Exception, the passed string, and a stack trace. Otherwise, program execution proceeds normally.

Parameters:
condition - the assertion condition to examined.
msg - the message to print upon termination. If this parameter is null, it is ignored; i.e. not displayed.
Throws:
SEpreconditionException

require

public static void require(boolean condition)
Assert that the specified condition is met or else terminate. This is the same as the overloaded 2-parameter method (boolean, null).

Parameters:
condition - the assertion condition to examined.

ensure

public static void ensure(boolean condition,
                          java.lang.String msg)
Assert that the specified condition is met or else terminate. Test the passed condition and if false then terminate the program with a Postcondition Exception, the passed string, and a stack trace. Otherwise, program execution proceeds normally.

Parameters:
condition - the assertion condition to examined.
msg - the message to print upon termination. If this parameter is null, it is ignored; i.e. not displayed.
Throws:
SEpostconditionException

ensure

public static void ensure(boolean condition)
Assert that the specified condition is met or else terminate. This is the same as the overloaded 2-parameter method (boolean, null).

Parameters:
condition - the assertion condition to examined.

invariant

public static void invariant(boolean condition,
                             java.lang.String msg)
Assert that the specified condition is met or else terminate. Test the passed condition and if false then terminate the program with an Invariant Exception, the passed string, and a stack trace. Otherwise, program execution proceeds normally.

Parameters:
condition - the assertion condition to examined.
msg - the message to print upon termination. If this parameter is null, it is ignored; i.e. not displayed.
Throws:
SEinvariantException

invariant

public static void invariant(boolean condition)
Assert that the specified condition is met or else terminate. This is the same as the overloaded 2-parameter method (boolean, null).

Parameters:
condition - the assertion condition to examined.

check

public static void check(boolean condition,
                         java.lang.String msg)
Assert that the specified condition is met or else terminate. Test the passed condition and if false then terminate the program with an Assertion Exception, the passed string, and a stack trace. Otherwise, program execution proceeds normally.

Parameters:
condition - the assertion condition to examined.
msg - the message to print upon termination. If this parameter is null, it is ignored; i.e. not displayed.
Throws:
SEassertionException

check

public static void check(boolean condition)
Assert that the specified condition is met or else terminate. This is the same as the overloaded 2-parameter method (boolean, null).

Parameters:
condition - the assertion condition to examined.


Java by Abstraction: A Client-View Approach