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