001/* ===================================================
002 * JFreeSVG : an SVG library for the Java(tm) platform
003 * ===================================================
004 * 
005 * (C)opyright 2013-2021, by Object Refinery Limited.  All rights reserved.
006 *
007 * Project Info:  http://www.jfree.org/jfreesvg/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * JFreeSVG home page:
028 * 
029 * http://www.jfree.org/jfreesvg
030 * 
031 */
032
033package org.jfree.graphics2d;
034
035/**
036 * A utility class that performs checks for method argument validity.
037 */
038public class Args {
039
040    private Args() {
041        // no need to instantiate this
042    }
043 
044    /**
045     * Checks that an argument is non-{@code null} and throws an 
046     * {@code IllegalArgumentException} otherwise.
047     * 
048     * @param obj  the object to check for {@code null}.
049     * @param ref  the text name for the parameter (to include in the exception
050     *     message).
051     */
052    public static void nullNotPermitted(Object obj, String ref) {
053        if (obj == null) {
054            throw new IllegalArgumentException("Null '" + ref + "' argument.");
055        }
056    }
057    
058    /**
059     * Checks an array to ensure it has the correct length and throws an
060     * {@code IllegalArgumentException} if it does not.
061     * 
062     * @param length  the required length.
063     * @param array  the array to check.
064     * @param ref  the text name of the array parameter (to include in the 
065     *     exception message).
066     */
067    public static void arrayMustHaveLength(int length, boolean[] array, 
068            String ref) {
069        nullNotPermitted(array, "array");
070        if (array.length != length) {
071            throw new IllegalArgumentException("Array '" + ref 
072                    + "' requires length " + length);
073        }
074    }
075
076    /**
077     * Checks an array to ensure it has the correct length and throws an
078     * {@code IllegalArgumentException} if it does not.
079     * 
080     * @param length  the required length.
081     * @param array  the array to check ({@code null} not permitted).
082     * @param ref  the text name of the array parameter (to include in the 
083     *     exception message).
084     */
085    public static void arrayMustHaveLength(int length, double[] array, 
086            String ref) {
087        nullNotPermitted(array, "array");
088        if (array.length != length) {
089            throw new IllegalArgumentException("Array '" + ref 
090                    + "' requires length " + length);
091        }
092    }
093}