001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2015
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 http://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 object Object to test.
054     * 
055     * @return {@code object}, if it is not {@code null}.
056     * 
057     * @throws NullPointerException if {@code object} is {@code null}.
058     */
059    @Deprecated
060    public static <T> T notNull(T object) {
061        if (object == null) {
062            throw new NullPointerException();
063        }
064        return object;
065    }
066
067    /**
068     * Ensures that a parameter passed to the calling method is not 
069     * {@code null}.
070     * 
071     * @param object Object to test.
072     * @param message Exception message to use if {@code object} is 
073     * {@code null}.
074     * 
075     * @return {@code object}, if it is not {@code null}.
076     * 
077     * @throws NullPointerException if {@code object} is {@code null}.
078     */
079    @Deprecated
080    public static <T> T notNull(T object, Object message) {
081        if (object == null) {
082            throw new NullPointerException(String.valueOf(message));
083        }
084        return object;
085    }
086
087    /**
088     * Ensures that a parameter passed to the calling method is not 
089     * {@code null}.
090     * 
091     * @param object Object to test.
092     * @param format Template used to create an exception if {@code object} is 
093     * {@code null}. Uses {@link String#format(String, Object...)}.
094     * @param values Values to use within {@code format}.
095     * 
096     * @return {@code object}, if it is not {@code null}.
097     * 
098     * @throws NullPointerException if {@code object} is {@code null}.
099     */
100    public static <T> T notNull(T object, String format, Object... values) {
101        if (object == null) {
102            throw new NullPointerException(String.format(format, values));
103        }
104        return object;
105    }
106
107    public static <T> List<T> noNulls(String message, T... objects) {
108        for (T object : objects) {
109            if (object == null) {
110                throw new NullPointerException(message);
111            }
112        }
113        return CollectionHelpers.list(objects);
114    }
115
116    /**
117     * Ensures the {@literal "truth"} of an expression involving parameters 
118     * passed to the calling method.
119     * 
120     * @param expression A boolean expression to test.
121     * 
122     * @throws IllegalArgumentException if {@code expression} is {@code false}.
123     */
124    public static void checkArg(boolean expression) {
125        if (!expression) {
126            throw new IllegalArgumentException();
127        }
128    }
129
130    /**
131     * Ensures the {@literal "truth"} of an expression involving parameters 
132     * passed to the calling method.
133     * 
134     * @param expression A boolean expression to test.
135     * @param message Exception message to use if {@code expression} is 
136     * {@code false}.
137     * 
138     * @throws IllegalArgumentException if {@code expression} is {@code false}.
139     */
140    public static void checkArg(boolean expression, Object message) {
141        if (!expression) {
142            throw new IllegalArgumentException(String.valueOf(message));
143        }
144    }
145
146    /**
147     * Ensures the {@literal "truth"} of an expression involving parameters 
148     * passed to the calling method.
149     * 
150     * @param expression A boolean expression to test.
151     * @param format Template used to create an exception if {@code expression} is 
152     * {@code false}. Uses {@link String#format(String, Object...)}.
153     * @param values Values to use within {@code format}.
154     * 
155     * @throws IllegalArgumentException if {@code expression} is {@code false}.
156     */
157    public static void checkArg(boolean expression, String format, 
158        Object... values) 
159    {
160        if (!expression) {
161            throw new IllegalArgumentException(String.format(format, values));
162        }
163    }
164
165    public static void instanceOf(Object object, Class<?> clazz) {
166        if (!clazz.isInstance(object)) {
167            throw new IllegalArgumentException();
168        }
169    }
170
171    public static void instanceOf(Object message, Object object, Class<?> clazz) {
172        if (!clazz.isInstance(object)) {
173            throw new IllegalArgumentException(String.valueOf(message));
174        }
175    }
176
177    public static boolean isInstanceOf(Object object, Class<?> clazz) {
178        notNull(object);
179        notNull(clazz);
180        return clazz.isInstance(object);
181    }
182}