001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas/
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see https://www.gnu.org/licenses/.
027 */
028package edu.wisc.ssec.mcidasv.util;
029
030import java.util.List;
031
032/**
033 * This is a {@literal "convenience"} class--use these methods to reduce 
034 * boilerplate parameter verification. For example:
035 * <pre>
036 * if (str == null) {
037 *     throw new NullPointerException("null is bad");
038 * }</pre>
039 * can be replaced with
040 * <pre>
041 * notNull(str, "null is bad");</pre>
042 * 
043 * Remember that these methods are used to signal an error in the <b>calling</b>
044 * method!
045 */
046public final class Contract {
047    private Contract() { }
048
049    /**
050     * Ensures that a parameter passed to the calling method is not 
051     * {@code null}.
052     *
053     * @param <T> Type of object to test.
054     * @param object Object to test.
055     * 
056     * @return {@code object}, if it is not {@code null}.
057     * 
058     * @throws NullPointerException if {@code object} is {@code null}.
059     */
060    @Deprecated
061    public static <T> T notNull(T object) {
062        if (object == null) {
063            throw new NullPointerException();
064        }
065        return object;
066    }
067
068    /**
069     * Ensures that a parameter passed to the calling method is not 
070     * {@code null}.
071     *
072     * @param <T> Type of object to test.
073     * @param object Object to test.
074     * @param message Exception message to use if {@code object} is 
075     * {@code null}.
076     * 
077     * @return {@code object}, if it is not {@code null}.
078     * 
079     * @throws NullPointerException if {@code object} is {@code null}.
080     */
081    @Deprecated
082    public static <T> T notNull(T object, Object message) {
083        if (object == null) {
084            throw new NullPointerException(String.valueOf(message));
085        }
086        return object;
087    }
088
089    /**
090     * Ensures that a parameter passed to the calling method is not 
091     * {@code null}.
092     *
093     * @param <T> Type of object to test.
094     * @param object Object to test.
095     * @param format Template used to create an exception if {@code object} is 
096     * {@code null}. Uses {@link String#format(String, Object...)}.
097     * @param values Values to use within {@code format}.
098     * 
099     * @return {@code object}, if it is not {@code null}.
100     * 
101     * @throws NullPointerException if {@code object} is {@code null}.
102     */
103    public static <T> T notNull(T object, String format, Object... values) {
104        if (object == null) {
105            throw new NullPointerException(String.format(format, values));
106        }
107        return object;
108    }
109
110    public static <T> List<T> noNulls(String message, T... objects) {
111        for (T object : objects) {
112            if (object == null) {
113                throw new NullPointerException(message);
114            }
115        }
116        return CollectionHelpers.list(objects);
117    }
118
119    /**
120     * Ensures the {@literal "truth"} of an expression involving parameters 
121     * passed to the calling method.
122     * 
123     * @param expression A boolean expression to test.
124     * 
125     * @throws IllegalArgumentException if {@code expression} is {@code false}.
126     */
127    public static void checkArg(boolean expression) {
128        if (!expression) {
129            throw new IllegalArgumentException();
130        }
131    }
132
133    /**
134     * Ensures the {@literal "truth"} of an expression involving parameters 
135     * passed to the calling method.
136     * 
137     * @param expression A boolean expression to test.
138     * @param message Exception message to use if {@code expression} is 
139     * {@code false}.
140     * 
141     * @throws IllegalArgumentException if {@code expression} is {@code false}.
142     */
143    public static void checkArg(boolean expression, Object message) {
144        if (!expression) {
145            throw new IllegalArgumentException(String.valueOf(message));
146        }
147    }
148
149    /**
150     * Ensures the {@literal "truth"} of an expression involving parameters 
151     * passed to the calling method.
152     * 
153     * @param expression A boolean expression to test.
154     * @param format Template used to create an exception if {@code expression} is 
155     * {@code false}. Uses {@link String#format(String, Object...)}.
156     * @param values Values to use within {@code format}.
157     * 
158     * @throws IllegalArgumentException if {@code expression} is {@code false}.
159     */
160    public static void checkArg(boolean expression, String format, 
161        Object... values) 
162    {
163        if (!expression) {
164            throw new IllegalArgumentException(String.format(format, values));
165        }
166    }
167
168    public static void instanceOf(Object object, Class<?> clazz) {
169        if (!clazz.isInstance(object)) {
170            throw new IllegalArgumentException();
171        }
172    }
173
174    public static void instanceOf(Object message, Object object, Class<?> clazz) {
175        if (!clazz.isInstance(object)) {
176            throw new IllegalArgumentException(String.valueOf(message));
177        }
178    }
179
180    public static boolean isInstanceOf(Object object, Class<?> clazz) {
181        notNull(object);
182        notNull(clazz);
183        return clazz.isInstance(object);
184    }
185}