001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.util;
038
039/**
040 * A utility class for checking method arguments.
041 */
042public class Args {
043
044    /**
045     * Throws an {@code IllegalArgumentException} if the supplied 
046     * {@code param} is {@code null}.
047     *
048     * @param param  the parameter to check ({@code null} permitted).
049     * @param name  the name of the parameter (to use in the exception message
050     *     if {@code param} is {@code null}).
051     *
052     * @throws IllegalArgumentException  if {@code param} is 
053     *     {@code null}.
054     */
055    public static void nullNotPermitted(Object param, String name) {
056        if (param == null) {
057            throw new IllegalArgumentException("Null '" + name + "' argument.");
058        }
059    }
060    
061    /**
062     * Throws an {@code IllegalArgumentException} if {@code value} is negative.
063     * 
064     * @param value  the value.
065     * @param name  the parameter name (for use in the exception message).
066     */
067    public static void requireNonNegative(int value, String name) {
068        if (value < 0) {
069            throw new IllegalArgumentException("Require '" + name + "' (" 
070                    + value + ") to be non-negative.");
071        }
072    }
073    
074    /**
075     * Throws an {@code IllegalArgumentException} if {@code value} is negative.
076     * 
077     * @param value  the value.
078     * @param name  the parameter name (for use in the exception message).
079     */
080    public static void requireNonNegative(double value, String name) {
081        if (value < 0) {
082            throw new IllegalArgumentException("Require '" + name + "' (" 
083                    + value + ") to be non-negative.");
084        }
085    }
086    
087    /**
088     * Checks that the value falls within the specified range and, if it does
089     * not, throws an {@code IllegalArgumentException}.
090     * 
091     * @param value  the value.
092     * @param name  the parameter name.
093     * @param lowerBound  the lower bound of the permitted range.
094     * @param upperBound  the upper bound fo the permitted range.
095     */
096    public static void requireInRange(int value, String name, 
097            int lowerBound, int upperBound) {
098        if (value < lowerBound || value > upperBound) {
099            throw new IllegalArgumentException("Require '" + name + "' (" 
100                    + value + ") to be in the range " + lowerBound + " to " 
101                    + upperBound);
102        }
103    }
104    
105    /**
106     * Checks the supplied value is finite (neither infinite nor NaN) and 
107     * throws an {@code IllegalArgumentException} if the requirement is not
108     * met.
109     * 
110     * @param value  the value.
111     * @param name  the parameter name (for use in the exception message).
112     * 
113     * @since 1.5.4
114     */
115    public static void requireFinite(double value, String name) {
116        if (!Double.isFinite(value)) {
117            throw new IllegalArgumentException("Require '" + name + "' (" 
118                    + value + ") to be finite.");
119        }
120    }
121}