001    /*
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.common;
025    
026    /**
027     * This class provides some static utility methods for checking values of
028     * arguments and conditions. This can be used to ensure that a method is not
029     * invoked with null arguments, for example.
030     * 
031     * Here is a sample usage, where an MCRUsageException is thrown when "name" is
032     * empty:
033     * 
034     * <pre>
035     * public void sayHelloTo(String name) {
036     *     MCRArgumentChecker.ensureNotEmpty(name, &quot;name&quot;);
037     *     System.out.println(&quot;Hello &quot; + name);
038     * }
039     * </pre>
040     * 
041     * @deprecated
042     */
043    public abstract class MCRArgumentChecker {
044        /**
045         * Ensures that a given boolean expression is true, or throws an
046         * MCRUsageException with the error message provided otherwise.
047         * 
048         * @param expression
049         *            the boolean expression that should be true
050         * @param message
051         *            the error message to be thrown when the expression is not true
052         * @throws MCRUsageException
053         *             if the expression is not true
054         */
055        public static void ensureIsTrue(boolean expression, String message) {
056            if (!expression) {
057                throw new MCRUsageException(message);
058            }
059        }
060    
061        /**
062         * Ensures that a given boolean expression is false, or throws an
063         * MCRUsageException with the error message provided otherwise.
064         * 
065         * @param expression
066         *            the boolean expression that should be false
067         * @param message
068         *            the error message to be thrown when the expression is true
069         * @throws MCRUsageException
070         *             if the expression is true
071         */
072        public static void ensureIsFalse(boolean expression, String message) {
073            ensureIsTrue(!expression, message);
074        }
075    
076        /**
077         * Ensures that a given variable does not contain a null reference, or
078         * throws an MCRUsageException with an error message otherwise.
079         * 
080         * @param o
081         *            the Object variable that must not contain a null reference
082         * @param argumentName
083         *            the name of the argument variable that must not be null
084         * @throws MCRUsageException
085         *             if o is null
086         */
087        public static void ensureNotNull(Object o, String argumentName) {
088            ensureIsTrue(o != null, argumentName + " is null");
089        }
090    
091        /**
092         * Ensures that a given String variable does not contain a null reference,
093         * or a String of length 0, or a String containing only blanks.
094         * 
095         * @param s
096         *            the String variable that must not be null of empty
097         * @param argumentName
098         *            the name of the String argument
099         * @throws MCRUsageException
100         *             if s is null or s.trim().length() is 0
101         */
102        public static void ensureNotEmpty(String s, String argumentName) {
103            ensureNotNull(s, argumentName);
104            ensureIsTrue(s.trim().length() > 0, argumentName + " is an empty String");
105        }
106    
107        /**
108         * Ensures that a given numeric value is not negative.
109         * 
110         * @param value
111         *            the value that must not be negative
112         * @param argumentName
113         *            the name of the number argument
114         * @throws MCRUsageException
115         *             if value is negative
116         */
117        public static void ensureNotNegative(double value, String argumentName) {
118            ensureIsTrue(value >= 0, argumentName + " is negative");
119        }
120    }