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