001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2017, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------
028 * Args.java
029 * ---------
030 * (C) Copyright 2011-2017, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 */
035
036package org.jfree.chart.util;
037
038/**
039 * A utility class for checking method arguments.
040 */
041public class Args {
042
043    /**
044     * Throws an {@code IllegalArgumentException} if the supplied 
045     * {@code param} is {@code null}.
046     *
047     * @param param  the parameter to check ({@code null} permitted).
048     * @param name  the name of the parameter (to use in the exception message
049     *     if {@code param} is {@code null}).
050     *
051     * @throws IllegalArgumentException  if {@code param} is 
052     *     {@code null}.
053     */
054    public static void nullNotPermitted(Object param, String name) {
055        if (param == null) {
056            throw new IllegalArgumentException("Null '" + name + "' argument.");
057        }
058    }
059    
060    /**
061     * Throws an {@code IllegalArgumentException} if {@code value} is negative.
062     * 
063     * @param value  the value.
064     * @param name  the parameter name (for use in the exception message).
065     */
066    public static void requireNonNegative(int value, String name) {
067        if (value < 0) {
068            throw new IllegalArgumentException("Require '" + name + "' (" 
069                    + value + ") to be non-negative.");
070        }
071    }
072    
073    /**
074     * Checks that the value falls within the specified range and, if it does
075     * not, throws an {@code IllegalArgumentException}.
076     * 
077     * @param value  the value.
078     * @param name  the parameter name.
079     * @param lowerBound  the lower bound of the permitted range.
080     * @param upperBound  the upper bound fo the permitted range.
081     */
082    public static void requireInRange(int value, String name, 
083            int lowerBound, int upperBound) {
084        if (value < lowerBound) {
085            throw new IllegalArgumentException("Require '" + name + "' (" 
086                    + value + ") to be in the range " + lowerBound + " to " 
087                    + upperBound);
088        }
089    }
090}