001/* 002 * $Id: Contract.java,v 1.6 2011/03/24 16:06:35 davep Exp $ 003 * 004 * This file is part of McIDAS-V 005 * 006 * Copyright 2007-2011 007 * Space Science and Engineering Center (SSEC) 008 * University of Wisconsin - Madison 009 * 1225 W. Dayton Street, Madison, WI 53706, USA 010 * https://www.ssec.wisc.edu/mcidas 011 * 012 * All Rights Reserved 013 * 014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and 015 * some McIDAS-V source code is based on IDV and VisAD source code. 016 * 017 * McIDAS-V is free software; you can redistribute it and/or modify 018 * it under the terms of the GNU Lesser Public License as published by 019 * the Free Software Foundation; either version 3 of the License, or 020 * (at your option) any later version. 021 * 022 * McIDAS-V is distributed in the hope that it will be useful, 023 * but WITHOUT ANY WARRANTY; without even the implied warranty of 024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 025 * GNU Lesser Public License for more details. 026 * 027 * You should have received a copy of the GNU Lesser Public License 028 * along with this program. If not, see http://www.gnu.org/licenses. 029 */ 030package edu.wisc.ssec.mcidasv.util; 031 032import java.util.List; 033 034/** 035 * This is a {@literal "convenience"} class--use these methods to reduce 036 * boilerplate parameter verification. For example: 037 * <pre> 038 * if (str == null) { 039 * throw new NullPointerException("null is bad"); 040 * }</pre> 041 * can be replaced with 042 * <pre> 043 * notNull(str, "null is bad");</pre> 044 * 045 * Remember that these methods are used to signal an error in the <b>calling</b> 046 * method! 047 */ 048public final class Contract { 049 private Contract() { } 050 051 /** 052 * Ensures that a parameter passed to the calling method is not 053 * {@code null}. 054 * 055 * @param object Object to test. 056 * 057 * @return {@code object}, if it is not {@code null}. 058 * 059 * @throws NullPointerException if {@code object} is {@code null}. 060 */ 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 object Object to test. 073 * @param message Exception message to use if {@code object} is 074 * {@code null}. 075 * 076 * @return {@code object}, if it is not {@code null}. 077 * 078 * @throws NullPointerException if {@code object} is {@code null}. 079 */ 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}