001    /*
002     * The MIT License
003     *
004     * Copyright (c) 2012, Ninja Squad
005     *
006     * Permission is hereby granted, free of charge, to any person obtaining a copy
007     * of this software and associated documentation files (the "Software"), to deal
008     * in the Software without restriction, including without limitation the rights
009     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010     * copies of the Software, and to permit persons to whom the Software is
011     * furnished to do so, subject to the following conditions:
012     *
013     * The above copyright notice and this permission notice shall be included in
014     * all copies or substantial portions of the Software.
015     *
016     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022     * THE SOFTWARE.
023     */
024    
025    package com.ninja_squad.dbsetup.util;
026    
027    /**
028     * Utility class to help verifying preconditions
029     * @author JB Nizet
030     */
031    public final class Preconditions {
032        private Preconditions() {
033        }
034    
035        /**
036         * Throws a NullPointerException with the given message if the given argument is <code>null</code>.
037         * @param argument the argument to check for <code>null</code>
038         * @param message the message of the thrown NullPointerException
039         * @throws NullPointerException if argument is <code>null</code>.
040         */
041        public static void checkNotNull(Object argument, String message) throws NullPointerException {
042            if (argument == null) {
043                throw new NullPointerException(message);
044            }
045        }
046    
047        /**
048         * Throws an IllegalStateException with the given message if the given condition is <code>false</code>.
049         * @param condition the condition to check
050         * @param message the message of the thrown IllegalStateException
051         * @throws IllegalStateException if the condition is <code>false</code>.
052         */
053        public static void checkState(boolean condition, String message) throws IllegalStateException {
054            if (!condition) {
055                throw new IllegalStateException(message);
056            }
057        }
058    
059        /**
060         * Throws an IllegalARgumentException with the given message if the given condition is <code>false</code>.
061         * @param condition the condition to check
062         * @param message the message of the thrown IllegalArgumentException
063         * @throws IllegalArgumentException if the condition is <code>false</code>.
064         */
065        public static void checkArgument(boolean condition, String message) throws IllegalStateException {
066            if (!condition) {
067                throw new IllegalArgumentException(message);
068            }
069        }
070    }