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