001 /* 002 * Copyright 2007-2016 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005 /* 006 * Copyright (C) 2008-2016 UnboundID Corp. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021 package com.unboundid.util; 022 023 024 025 import static com.unboundid.util.Debug.*; 026 import static com.unboundid.util.StaticUtils.*; 027 import static com.unboundid.util.UtilityMessages.*; 028 029 030 031 /** 032 * This class provides a number of methods that can be used to enforce 033 * constraints on the behavior of SDK methods. 034 */ 035 public final class Validator 036 { 037 /** 038 * Prevent this class from being instantiated. 039 */ 040 private Validator() 041 { 042 // No implementation is required. 043 } 044 045 046 047 /** 048 * Ensures that the provided object is not {@code null}. 049 * 050 * @param o The object to examine. 051 * 052 * @throws LDAPSDKUsageException If the provided object is {@code null}. 053 */ 054 public static void ensureNotNull(final Object o) 055 throws LDAPSDKUsageException 056 { 057 if (o == null) 058 { 059 final LDAPSDKUsageException e = new LDAPSDKUsageException( 060 ERR_VALIDATOR_NULL_CHECK_FAILURE.get(0, 061 getStackTrace(Thread.currentThread().getStackTrace()))); 062 debugCodingError(e); 063 throw e; 064 } 065 } 066 067 068 069 /** 070 * Ensures that the provided object is not {@code null}. 071 * 072 * @param o The object to examine. 073 * @param message The message to include in the exception thrown if the 074 * provided object is {@code null}. 075 * 076 * @throws LDAPSDKUsageException If the provided object is {@code null}. 077 */ 078 public static void ensureNotNullWithMessage(final Object o, 079 final String message) 080 throws LDAPSDKUsageException 081 { 082 if (o == null) 083 { 084 final LDAPSDKUsageException e = new LDAPSDKUsageException( 085 ERR_VALIDATOR_FAILURE_CUSTOM_MESSAGE.get(message, 086 getStackTrace(Thread.currentThread().getStackTrace()))); 087 debugCodingError(e); 088 throw e; 089 } 090 } 091 092 093 094 /** 095 * Ensures that none of the provided objects is {@code null}. 096 * 097 * @param o1 The first object for which to make the determination. 098 * @param o2 The second object for which to make the determination. 099 * 100 * @throws LDAPSDKUsageException If any of the provided objects is 101 * {@code null}. 102 */ 103 public static void ensureNotNull(final Object o1, final Object o2) 104 throws LDAPSDKUsageException 105 { 106 if ((o1 == null) || (o2 == null)) 107 { 108 final int index; 109 if (o1 == null) 110 { 111 index = 0; 112 } 113 else 114 { 115 index = 1; 116 } 117 118 final LDAPSDKUsageException e = new LDAPSDKUsageException( 119 ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index, 120 getStackTrace(Thread.currentThread().getStackTrace()))); 121 debugCodingError(e); 122 throw e; 123 } 124 } 125 126 127 128 /** 129 * Ensures that none of the provided objects is {@code null}. 130 * 131 * @param o1 The first object for which to make the determination. 132 * @param o2 The second object for which to make the determination. 133 * @param o3 The third object for which to make the determination. 134 * 135 * @throws LDAPSDKUsageException If any of the provided objects is 136 * {@code null}. 137 */ 138 public static void ensureNotNull(final Object o1, final Object o2, 139 final Object o3) 140 throws LDAPSDKUsageException 141 { 142 if ((o1 == null) || (o2 == null) || (o3 == null)) 143 { 144 final int index; 145 if (o1 == null) 146 { 147 index = 0; 148 } 149 else if (o2 == null) 150 { 151 index = 1; 152 } 153 else 154 { 155 index = 2; 156 } 157 158 final LDAPSDKUsageException e = new LDAPSDKUsageException( 159 ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index, 160 getStackTrace(Thread.currentThread().getStackTrace()))); 161 debugCodingError(e); 162 throw e; 163 } 164 } 165 166 167 168 /** 169 * Ensures that none of the provided objects is {@code null}. 170 * 171 * @param o1 The first object for which to make the determination. 172 * @param o2 The second object for which to make the determination. 173 * @param o3 The third object for which to make the determination. 174 * @param o4 The fourth object for which to make the determination. 175 * 176 * @throws LDAPSDKUsageException If any of the provided objects is 177 * {@code null}. 178 */ 179 public static void ensureNotNull(final Object o1, final Object o2, 180 final Object o3, final Object o4) 181 throws LDAPSDKUsageException 182 { 183 if ((o1 == null) || (o2 == null) || (o3 == null) || (o4 == null)) 184 { 185 final int index; 186 if (o1 == null) 187 { 188 index = 0; 189 } 190 else if (o2 == null) 191 { 192 index = 1; 193 } 194 else if (o3 == null) 195 { 196 index = 2; 197 } 198 else 199 { 200 index = 3; 201 } 202 203 final LDAPSDKUsageException e = new LDAPSDKUsageException( 204 ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index, 205 getStackTrace(Thread.currentThread().getStackTrace()))); 206 debugCodingError(e); 207 throw e; 208 } 209 } 210 211 212 213 /** 214 * Ensures that none of the provided objects is {@code null}. 215 * 216 * @param o1 The first object for which to make the determination. 217 * @param o2 The second object for which to make the determination. 218 * @param o3 The third object for which to make the determination. 219 * @param o4 The fourth object for which to make the determination. 220 * @param o5 The fifth object for which to make the determination. 221 * 222 * @throws LDAPSDKUsageException If any of the provided objects is 223 * {@code null}. 224 */ 225 public static void ensureNotNull(final Object o1, final Object o2, 226 final Object o3, final Object o4, 227 final Object o5) 228 throws LDAPSDKUsageException 229 { 230 if ((o1 == null) || (o2 == null) || (o3 == null) || (o4 == null) || 231 (o5 == null)) 232 { 233 final int index; 234 if (o1 == null) 235 { 236 index = 0; 237 } 238 else if (o2 == null) 239 { 240 index = 1; 241 } 242 else if (o3 == null) 243 { 244 index = 2; 245 } 246 else if (o4 == null) 247 { 248 index = 3; 249 } 250 else 251 { 252 index = 4; 253 } 254 255 final LDAPSDKUsageException e = new LDAPSDKUsageException( 256 ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index, 257 getStackTrace(Thread.currentThread().getStackTrace()))); 258 debugCodingError(e); 259 throw e; 260 } 261 } 262 263 264 265 /** 266 * Ensures that the provided condition is {@code true}. 267 * 268 * @param condition The condition to verify. 269 * 270 * @throws LDAPSDKUsageException If the provided condition is {@code false}. 271 */ 272 public static void ensureTrue(final boolean condition) 273 throws LDAPSDKUsageException 274 { 275 if (! condition) 276 { 277 final LDAPSDKUsageException e = new LDAPSDKUsageException( 278 ERR_VALIDATOR_TRUE_CHECK_FAILURE.get( 279 getStackTrace(Thread.currentThread().getStackTrace()))); 280 debugCodingError(e); 281 throw e; 282 } 283 } 284 285 286 287 /** 288 * Ensures that the provided condition is {@code true}. 289 * 290 * @param condition The condition to verify. 291 * @param message The message to include in the exception thrown if the 292 * provided object is {@code null}. 293 * 294 * @throws LDAPSDKUsageException If the provided condition is {@code false}. 295 */ 296 public static void ensureTrue(final boolean condition, final String message) 297 throws LDAPSDKUsageException 298 { 299 if (! condition) 300 { 301 final LDAPSDKUsageException e = new LDAPSDKUsageException( 302 ERR_VALIDATOR_FAILURE_CUSTOM_MESSAGE.get(message, 303 getStackTrace(Thread.currentThread().getStackTrace()))); 304 debugCodingError(e); 305 throw e; 306 } 307 } 308 309 310 311 /** 312 * Ensures that the provided condition is {@code false}. 313 * 314 * @param condition The condition to verify. 315 * 316 * @throws LDAPSDKUsageException If the provided condition is {@code true}. 317 */ 318 public static void ensureFalse(final boolean condition) 319 throws LDAPSDKUsageException 320 { 321 if (condition) 322 { 323 final LDAPSDKUsageException e = new LDAPSDKUsageException( 324 ERR_VALIDATOR_FALSE_CHECK_FAILURE.get( 325 getStackTrace(Thread.currentThread().getStackTrace()))); 326 debugCodingError(e); 327 throw e; 328 } 329 } 330 331 332 333 /** 334 * Ensures that the provided condition is {@code false}. 335 * 336 * @param condition The condition to verify. 337 * @param message The message to include in the exception thrown if the 338 * provided object is {@code null}. 339 * 340 * @throws LDAPSDKUsageException If the provided condition is {@code true}. 341 */ 342 public static void ensureFalse(final boolean condition, final String message) 343 throws LDAPSDKUsageException 344 { 345 if (condition) 346 { 347 final LDAPSDKUsageException e = new LDAPSDKUsageException( 348 ERR_VALIDATOR_FAILURE_CUSTOM_MESSAGE.get(message, 349 getStackTrace(Thread.currentThread().getStackTrace()))); 350 debugCodingError(e); 351 throw e; 352 } 353 } 354 }