cours0708/workspace/Palindrome/iutsud/Assertion.java

220 lines
6.7 KiB
Java
Raw Normal View History

2008-11-25 21:11:16 +00:00
/*
* Copyright (C) 1997 Roger Whitney <whitney@cs.sdsu.edu>
*
* This file is part of the San Diego State University Java Library.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package iutsud ;
/**
* Cette classe imite la macro ASSERT de C/C++.
* Elle permet de s'assurer qu'une condition est v<EFBFBD>rifi<EFBFBD>e
* avant de poursuivre l'ex<EFBFBD>cution.<p>
* Elle a trois m<EFBFBD>thodes
* statiques : <code>pre</code>, <code>post</code> et <code>invariant</code>
* Ces m<EFBFBD>thodes sont identiques. Chaque m<EFBFBD>thode arr<EFBFBD>te l'ex<EFBFBD>cution du
* programme en levant une exception si son argument est faux. <p>
* Un exemple d'utilisation d'assertion est :
* <pre>
* <code>Assertion.pre( (mois == 1 ) && ( jour <= 31 ));</code>
* </pre>
* Ceci v<EFBFBD>rifie que la condition est vrai <EFBFBD> ce stade de l'ex<EFBFBD>cution.
* Si elle n'est pas v<EFBFBD>rifi<EFBFBD>e, une exception de type
* <code>Assertion.PreConditionException</code>,
* <code>Assertion.PostConditionException</code>
* ou <code>Assertion.InvariantException</code>
* est lev<EFBFBD>e.
* Ces exceptions d<EFBFBD>rivent de <code>RuntimeException</code>
* et arr<EFBFBD>tent l'ex<EFBFBD>cution du programme en affichant
* l'<EFBFBD>tat de la pile d'ex<EFBFBD>cution. <P>
* Cette classe provient de la classe <code>sdsu.io.Assert</code> de l'universit<EFBFBD>
* de San Diego (<a href="http://www.eli.sdsu.edu/java-SDSU">http://www.eli.sdsu.edu/java-SDSU</a>).
* <p>Les modifications sont mineures :
* <li> d<EFBFBD>finition et utilisation d'exceptions internes <EFBFBD> la classe
* <li> suppression de la m<EFBFBD>thode <code>condition</code>
* <li> m<EFBFBD>thode <code>classInvariant</code> renomm<EFBFBD>e <code>invariant</code>
* <li> classe <code>Assert</code> renomm<EFBFBD>e <code>Assertion</code>
*/
/*
* <p> This class is modeled after the ASSERT macro in C/C++. The class
* has three static methods.
* The methods are identical. Each method will throw an exception if
* is argument is false. A sample use of the assert is:
* <pre>
* Assert.pre( (X > 5 ) && ( Z <= 10 ));
* </pre>
* This asserts that the condition should be true at this point.
* If it is not an exception will be thrown. So the program can
* assume that the condition is true after the above assert. <P>
* In C/C++ ASSERT is defined as a macro. Thus the macro can be
* turned off with a compile switch. This allows the code to contain
* ASSERTS with out any runtime cost. There is no way in Java to
* completely eliminate the cost of the Assert calls. Hopefully
* future compilers will be smart enough to use tricks with
* classpaths and two versions of this class to "turn off" the
* Assert calls in productin code without modifing the program
* source.<P>
* See <I>Writing Solid Code</I> by Steve Maguire, pp 13-44
* or <I>Object-Oriented Software Construction</I> by
* Bertrand Meyer, pp 111 - 163 for the use and importance
* of Assert.
*
* @see sdsu.test.AssertException
* @version 1.0 30 December 1997
* @author Roger Whitney
* (<a href=mailto:whitney@cs.sdsu.edu>whitney@cs.sdsu.edu</a>)
*/
public abstract class Assertion
{
private Assertion()
{
super() ;
}
// /* main de test
private static void main(String args[]) {
while (true) {
System.out.print( "Print a Boolean ");
boolean b = Console.readBoolean() ;
pre(b) ;
post(b) ;
invariant(b) ;
}
}
// */
/**
* Exception lev<EFBFBD>e lorsqu'une pr<EFBFBD>condition n'est pas v<EFBFBD>rifi<EFBFBD>e
*/
public static class PreConditionException extends RuntimeException
{
public PreConditionException(String message)
{
super(message) ;
}
} // end class PreConditionException
/**
* Exception lev<EFBFBD>e lorsqu'une postcondition n'est pas v<EFBFBD>rifi<EFBFBD>e
*/
public static class PostConditionException extends RuntimeException
{
public PostConditionException(String message)
{
super(message) ;
}
} // end class PostConditionException
/**
* Exception lev<EFBFBD>e lorsqu'un invariant n'est pas v<EFBFBD>rifi<EFBFBD>e
*/
public static class InvariantException extends RuntimeException
{
public InvariantException(String message)
{
super(message) ;
}
} // end class InvariantException
/**
* Arr<EFBFBD>te l'ex<EFBFBD>cution du programme si la condition n'est pas v<EFBFBD>rifi<EFBFBD>e
* en levant l'exception <code>PreConditionException</code>
* @param cond la condition <EFBFBD> v<EFBFBD>rifier
*/
public static void pre( boolean cond )
{
if ( !cond )
throw new PreConditionException( "Pr<EFBFBD>condition non v<>rifi<66>e" );
}
/**
* Arr<EFBFBD>te l'ex<EFBFBD>cution du programme si la condition n'est pas v<EFBFBD>rifi<EFBFBD>e
* en levant l'exception <code>PreConditionException</code>
* avec le message pass<EFBFBD> en param<EFBFBD>tre
* @param cond la condition <EFBFBD> v<EFBFBD>rifier
* @param message message <EFBFBD> afficher
*/
public static void pre( boolean cond , String message )
{
if ( !cond )
throw new PreConditionException( message );
}
/**
* Arr<EFBFBD>te l'ex<EFBFBD>cution du programme si la condition n'est pas v<EFBFBD>rifi<EFBFBD>e
* en levant l'exception <code>PostConditionException</code>
* @param cond la condition <EFBFBD> v<EFBFBD>rifier
*/
public static void post( boolean cond )
{
if ( !cond )
throw new PostConditionException( "Postcondition non v<>rifi<66>e" );
}
/**
* Arr<EFBFBD>te l'ex<EFBFBD>cution du programme si la condition n'est pas v<EFBFBD>rifi<EFBFBD>e
* en levant l'exception <code>PostConditionException</code>
* avec le message pass<EFBFBD> en param<EFBFBD>tre
* @param cond la condition <EFBFBD> v<EFBFBD>rifier
* @param message message <EFBFBD> afficher
*/
public static void post( boolean cond , String message)
{
if ( !cond )
throw new PostConditionException( message );
}
/**
* Arr<EFBFBD>te l'ex<EFBFBD>cution du programme si la condition n'est pas v<EFBFBD>rifi<EFBFBD>e
* en levant l'exception <code>InvariantException</code>
* @param cond la condition <EFBFBD> v<EFBFBD>rifier
*/
public static void invariant( boolean cond )
{
if ( !cond )
throw new InvariantException( "Invariant non v<>rifi<66>" );
}
/**
* Arr<EFBFBD>te l'ex<EFBFBD>cution du programme si la condition n'est pas v<EFBFBD>rifi<EFBFBD>e
* en levant l'exception <code>InvariantException</code>
* avec le message pass<EFBFBD> en param<EFBFBD>tre
* @param cond la condition <EFBFBD> v<EFBFBD>rifier
* @param message message <EFBFBD> afficher
*/
public static void invariant( boolean cond , String message )
{
if ( !cond )
throw new InvariantException( message );
}
}