001/*
002 * Copyright 2007-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2008-2019 Ping Identity Corporation
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 java.util.Collection;
026import java.util.Map;
027
028import static com.unboundid.util.UtilityMessages.*;
029
030
031
032/**
033 * This class provides a number of methods that can be used to enforce
034 * constraints on the behavior of SDK methods.
035 */
036@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
037public final class Validator
038{
039  /**
040   * Prevent this class from being instantiated.
041   */
042  private Validator()
043  {
044    // No implementation is required.
045  }
046
047
048
049  /**
050   * Ensures that the provided object is not {@code null}.
051   *
052   * @param  o  The object to examine.
053   *
054   * @throws  LDAPSDKUsageException  If the provided object is {@code null}.
055   */
056  public static void ensureNotNull(final Object o)
057         throws LDAPSDKUsageException
058  {
059    if (o == null)
060    {
061      final LDAPSDKUsageException e = new LDAPSDKUsageException(
062           ERR_VALIDATOR_NULL_CHECK_FAILURE.get(0,
063                StaticUtils.getStackTrace(
064                     Thread.currentThread().getStackTrace())));
065      Debug.debugCodingError(e);
066      throw e;
067    }
068  }
069
070
071
072  /**
073   * Ensures that the provided object is not {@code null}.
074   *
075   * @param  o        The object to examine.
076   * @param  message  The message to include in the exception thrown if the
077   *                  provided object is {@code null}.
078   *
079   * @throws  LDAPSDKUsageException  If the provided object is {@code null}.
080   */
081  public static void ensureNotNullWithMessage(final Object o,
082                                              final String message)
083         throws LDAPSDKUsageException
084  {
085    if (o == null)
086    {
087      final LDAPSDKUsageException e = new LDAPSDKUsageException(
088           ERR_VALIDATOR_FAILURE_CUSTOM_MESSAGE.get(message,
089                StaticUtils.getStackTrace(
090                     Thread.currentThread().getStackTrace())));
091      Debug.debugCodingError(e);
092      throw e;
093    }
094  }
095
096
097
098  /**
099   * Ensures that none of the provided objects is {@code null}.
100   *
101   * @param  o1  The first object for which to make the determination.
102   * @param  o2  The second object for which to make the determination.
103   *
104   * @throws  LDAPSDKUsageException  If any of the provided objects is
105   *                                 {@code null}.
106   */
107  public static void ensureNotNull(final Object o1, final Object o2)
108         throws LDAPSDKUsageException
109  {
110    if ((o1 == null) || (o2 == null))
111    {
112      final int index;
113      if (o1 == null)
114      {
115        index = 0;
116      }
117      else
118      {
119        index = 1;
120      }
121
122      final LDAPSDKUsageException e = new LDAPSDKUsageException(
123           ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index,
124                StaticUtils.getStackTrace(
125                     Thread.currentThread().getStackTrace())));
126      Debug.debugCodingError(e);
127      throw e;
128    }
129  }
130
131
132
133  /**
134   * Ensures that none of the provided objects is {@code null}.
135   *
136   * @param  o1  The first object for which to make the determination.
137   * @param  o2  The second object for which to make the determination.
138   * @param  o3  The third object for which to make the determination.
139   *
140   * @throws  LDAPSDKUsageException  If any of the provided objects is
141   *                                 {@code null}.
142   */
143  public static void ensureNotNull(final Object o1, final Object o2,
144                                   final Object o3)
145         throws LDAPSDKUsageException
146  {
147    if ((o1 == null) || (o2 == null) || (o3 == null))
148    {
149      final int index;
150      if (o1 == null)
151      {
152        index = 0;
153      }
154      else if (o2 == null)
155      {
156        index = 1;
157      }
158      else
159      {
160        index = 2;
161      }
162
163      final LDAPSDKUsageException e = new LDAPSDKUsageException(
164           ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index,
165                StaticUtils.getStackTrace(
166                     Thread.currentThread().getStackTrace())));
167      Debug.debugCodingError(e);
168      throw e;
169    }
170  }
171
172
173
174  /**
175   * Ensures that none of the provided objects is {@code null}.
176   *
177   * @param  o1  The first object for which to make the determination.
178   * @param  o2  The second object for which to make the determination.
179   * @param  o3  The third object for which to make the determination.
180   * @param  o4  The fourth object for which to make the determination.
181   *
182   * @throws  LDAPSDKUsageException  If any of the provided objects is
183   *                                 {@code null}.
184   */
185  public static void ensureNotNull(final Object o1, final Object o2,
186                                   final Object o3, final Object o4)
187         throws LDAPSDKUsageException
188  {
189    if ((o1 == null) || (o2 == null) || (o3 == null) || (o4 == null))
190    {
191      final int index;
192      if (o1 == null)
193      {
194        index = 0;
195      }
196      else if (o2 == null)
197      {
198        index = 1;
199      }
200      else if (o3 == null)
201      {
202        index = 2;
203      }
204      else
205      {
206        index = 3;
207      }
208
209      final LDAPSDKUsageException e = new LDAPSDKUsageException(
210           ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index,
211                StaticUtils.getStackTrace(
212                     Thread.currentThread().getStackTrace())));
213      Debug.debugCodingError(e);
214      throw e;
215    }
216  }
217
218
219
220  /**
221   * Ensures that none of the provided objects is {@code null}.
222   *
223   * @param  o1  The first object for which to make the determination.
224   * @param  o2  The second object for which to make the determination.
225   * @param  o3  The third object for which to make the determination.
226   * @param  o4  The fourth object for which to make the determination.
227   * @param  o5  The fifth object for which to make the determination.
228   *
229   * @throws  LDAPSDKUsageException  If any of the provided objects is
230   *                                 {@code null}.
231   */
232  public static void ensureNotNull(final Object o1, final Object o2,
233                                   final Object o3, final Object o4,
234                                   final Object o5)
235         throws LDAPSDKUsageException
236  {
237    if ((o1 == null) || (o2 == null) || (o3 == null) || (o4 == null) ||
238        (o5 == null))
239    {
240      final int index;
241      if (o1 == null)
242      {
243        index = 0;
244      }
245      else if (o2 == null)
246      {
247        index = 1;
248      }
249      else if (o3 == null)
250      {
251        index = 2;
252      }
253      else if (o4 == null)
254      {
255        index = 3;
256      }
257      else
258      {
259        index = 4;
260      }
261
262      final LDAPSDKUsageException e = new LDAPSDKUsageException(
263           ERR_VALIDATOR_NULL_CHECK_FAILURE.get(index,
264                StaticUtils.getStackTrace(
265                     Thread.currentThread().getStackTrace())));
266      Debug.debugCodingError(e);
267      throw e;
268    }
269  }
270
271
272
273  /**
274   * Ensures that the provided collection is not {@code null} and contains at
275   * least one item.
276   *
277   * @param  collection  The collection to verify.
278   *
279   * @throws  LDAPSDKUsageException  If the provided collection is {@code null}
280   *                                 or empty.
281   */
282  public static void ensureNotNullOrEmpty(final Collection<?> collection)
283  {
284    if (collection == null)
285    {
286      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
287           ERR_VALIDATOR_COLLECTION_NULL.get(StaticUtils.getStackTrace(
288                Thread.currentThread().getStackTrace())));
289      Debug.debugCodingError(e);
290      throw e;
291    }
292    else if (collection.isEmpty())
293    {
294      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
295           ERR_VALIDATOR_COLLECTION_EMPTY.get(StaticUtils.getStackTrace(
296                Thread.currentThread().getStackTrace())));
297      Debug.debugCodingError(e);
298      throw e;
299    }
300  }
301
302
303
304  /**
305   * Ensures that the provided collection is not {@code null} and contains at
306   * least one item.
307   *
308   * @param  collection  The collection to verify.
309   * @param  message     The message to include in the exception thrown if the
310   *                     provided collection is {@code null} or empty.
311   *
312   * @throws  LDAPSDKUsageException  If the provided collection is {@code null}
313   *                                 or empty.
314   */
315  public static void ensureNotNullOrEmpty(final Collection<?> collection,
316                                          final String message)
317  {
318    if (collection == null)
319    {
320      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
321           ERR_VALIDATOR_COLLECTION_NULL_CUSTOM_MESSAGE.get(message,
322                StaticUtils.getStackTrace(
323                     Thread.currentThread().getStackTrace())));
324      Debug.debugCodingError(e);
325      throw e;
326    }
327    else if (collection.isEmpty())
328    {
329      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
330           ERR_VALIDATOR_COLLECTION_EMPTY_CUSTOM_MESSAGE.get(message,
331                StaticUtils.getStackTrace(
332                     Thread.currentThread().getStackTrace())));
333      Debug.debugCodingError(e);
334      throw e;
335    }
336  }
337
338
339
340  /**
341   * Ensures that the provided map is not {@code null} and contains at least one
342   * item.
343   *
344   * @param  map  The map to verify.
345   *
346   * @throws  LDAPSDKUsageException  If the provided map is {@code null} or
347   *                                 empty.
348   */
349  public static void ensureNotNullOrEmpty(final Map<?,?> map)
350  {
351    if (map == null)
352    {
353      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
354           ERR_VALIDATOR_MAP_NULL.get(StaticUtils.getStackTrace(
355                Thread.currentThread().getStackTrace())));
356      Debug.debugCodingError(e);
357      throw e;
358    }
359    else if (map.isEmpty())
360    {
361      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
362           ERR_VALIDATOR_MAP_EMPTY.get(StaticUtils.getStackTrace(
363                Thread.currentThread().getStackTrace())));
364      Debug.debugCodingError(e);
365      throw e;
366    }
367  }
368
369
370
371  /**
372   * Ensures that the provided map is not {@code null} and contains at least one
373   * item.
374   *
375   * @param  map      The map to verify.
376   * @param  message  The message to include in the exception thrown if the
377   *                  provided map is {@code null} or empty.
378   *
379   * @throws  LDAPSDKUsageException  If the provided map is {@code null} or
380   *                                 empty.
381   */
382  public static void ensureNotNullOrEmpty(final Map<?,?> map,
383                                          final String message)
384  {
385    if (map == null)
386    {
387      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
388           ERR_VALIDATOR_MAP_NULL_CUSTOM_MESSAGE.get(message,
389                StaticUtils.getStackTrace(
390                     Thread.currentThread().getStackTrace())));
391      Debug.debugCodingError(e);
392      throw e;
393    }
394    else if (map.isEmpty())
395    {
396      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
397           ERR_VALIDATOR_MAP_EMPTY_CUSTOM_MESSAGE.get(message,
398                StaticUtils.getStackTrace(
399                     Thread.currentThread().getStackTrace())));
400      Debug.debugCodingError(e);
401      throw e;
402    }
403  }
404
405
406
407  /**
408   * Ensures that the provided array is not {@code null} and has a length of at
409   * least one.
410   *
411   * @param  array  The array to verify.
412   *
413   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
414   *                                 empty.
415   */
416  public static void ensureNotNullOrEmpty(final Object[] array)
417  {
418    if (array == null)
419    {
420      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
421           ERR_VALIDATOR_ARRAY_NULL.get(StaticUtils.getStackTrace(
422                Thread.currentThread().getStackTrace())));
423      Debug.debugCodingError(e);
424      throw e;
425    }
426    else if (array.length == 0)
427    {
428      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
429           ERR_VALIDATOR_ARRAY_EMPTY.get(StaticUtils.getStackTrace(
430                Thread.currentThread().getStackTrace())));
431      Debug.debugCodingError(e);
432      throw e;
433    }
434  }
435
436
437
438  /**
439   * Ensures that the provided array is not {@code null} and has a length of at
440   * least one.
441   *
442   * @param  array    The array to verify.
443   * @param  message  The message to include in the exception thrown if the
444   *                  provided array is {@code null} or empty.
445   *
446   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
447   *                                 empty.
448   */
449  public static void ensureNotNullOrEmpty(final Object[] array,
450                                          final String message)
451  {
452    if (array == null)
453    {
454      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
455           ERR_VALIDATOR_ARRAY_NULL_CUSTOM_MESSAGE.get(message,
456                StaticUtils.getStackTrace(
457                     Thread.currentThread().getStackTrace())));
458      Debug.debugCodingError(e);
459      throw e;
460    }
461    else if (array.length == 0)
462    {
463      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
464           ERR_VALIDATOR_ARRAY_EMPTY_CUSTOM_MESSAGE.get(message,
465                StaticUtils.getStackTrace(
466                     Thread.currentThread().getStackTrace())));
467      Debug.debugCodingError(e);
468      throw e;
469    }
470  }
471
472
473
474  /**
475   * Ensures that the provided array is not {@code null} and has a length of at
476   * least one.
477   *
478   * @param  array  The array to verify.
479   *
480   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
481   *                                 empty.
482   */
483  public static void ensureNotNullOrEmpty(final byte[] array)
484  {
485    if (array == null)
486    {
487      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
488           ERR_VALIDATOR_ARRAY_NULL.get(StaticUtils.getStackTrace(
489                Thread.currentThread().getStackTrace())));
490      Debug.debugCodingError(e);
491      throw e;
492    }
493    else if (array.length == 0)
494    {
495      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
496           ERR_VALIDATOR_ARRAY_EMPTY.get(StaticUtils.getStackTrace(
497                Thread.currentThread().getStackTrace())));
498      Debug.debugCodingError(e);
499      throw e;
500    }
501  }
502
503
504
505  /**
506   * Ensures that the provided array is not {@code null} and has a length of at
507   * least one.
508   *
509   * @param  array    The array to verify.
510   * @param  message  The message to include in the exception thrown if the
511   *                  provided array is {@code null} or empty.
512   *
513   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
514   *                                 empty.
515   */
516  public static void ensureNotNullOrEmpty(final byte[] array,
517                                          final String message)
518  {
519    if (array == null)
520    {
521      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
522           ERR_VALIDATOR_ARRAY_NULL_CUSTOM_MESSAGE.get(message,
523                StaticUtils.getStackTrace(
524                     Thread.currentThread().getStackTrace())));
525      Debug.debugCodingError(e);
526      throw e;
527    }
528    else if (array.length == 0)
529    {
530      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
531           ERR_VALIDATOR_ARRAY_EMPTY_CUSTOM_MESSAGE.get(message,
532                StaticUtils.getStackTrace(
533                     Thread.currentThread().getStackTrace())));
534      Debug.debugCodingError(e);
535      throw e;
536    }
537  }
538
539
540
541  /**
542   * Ensures that the provided array is not {@code null} and has a length of at
543   * least one.
544   *
545   * @param  array  The array to verify.
546   *
547   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
548   *                                 empty.
549   */
550  public static void ensureNotNullOrEmpty(final char[] array)
551  {
552    if (array == null)
553    {
554      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
555           ERR_VALIDATOR_ARRAY_NULL.get(StaticUtils.getStackTrace(
556                Thread.currentThread().getStackTrace())));
557      Debug.debugCodingError(e);
558      throw e;
559    }
560    else if (array.length == 0)
561    {
562      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
563           ERR_VALIDATOR_ARRAY_EMPTY.get(StaticUtils.getStackTrace(
564                Thread.currentThread().getStackTrace())));
565      Debug.debugCodingError(e);
566      throw e;
567    }
568  }
569
570
571
572  /**
573   * Ensures that the provided array is not {@code null} and has a length of at
574   * least one.
575   *
576   * @param  array    The array to verify.
577   * @param  message  The message to include in the exception thrown if the
578   *                  provided array is {@code null} or empty.
579   *
580   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
581   *                                 empty.
582   */
583  public static void ensureNotNullOrEmpty(final char[] array,
584                                          final String message)
585  {
586    if (array == null)
587    {
588      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
589           ERR_VALIDATOR_ARRAY_NULL_CUSTOM_MESSAGE.get(message,
590                StaticUtils.getStackTrace(
591                     Thread.currentThread().getStackTrace())));
592      Debug.debugCodingError(e);
593      throw e;
594    }
595    else if (array.length == 0)
596    {
597      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
598           ERR_VALIDATOR_ARRAY_EMPTY_CUSTOM_MESSAGE.get(message,
599                StaticUtils.getStackTrace(
600                     Thread.currentThread().getStackTrace())));
601      Debug.debugCodingError(e);
602      throw e;
603    }
604  }
605
606
607
608  /**
609   * Ensures that the provided array is not {@code null} and has a length of at
610   * least one.
611   *
612   * @param  array  The array to verify.
613   *
614   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
615   *                                 empty.
616   */
617  public static void ensureNotNullOrEmpty(final int[] array)
618  {
619    if (array == null)
620    {
621      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
622           ERR_VALIDATOR_ARRAY_NULL.get(StaticUtils.getStackTrace(
623                Thread.currentThread().getStackTrace())));
624      Debug.debugCodingError(e);
625      throw e;
626    }
627    else if (array.length == 0)
628    {
629      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
630           ERR_VALIDATOR_ARRAY_EMPTY.get(StaticUtils.getStackTrace(
631                Thread.currentThread().getStackTrace())));
632      Debug.debugCodingError(e);
633      throw e;
634    }
635  }
636
637
638
639  /**
640   * Ensures that the provided array is not {@code null} and has a length of at
641   * least one.
642   *
643   * @param  array    The array to verify.
644   * @param  message  The message to include in the exception thrown if the
645   *                  provided array is {@code null} or empty.
646   *
647   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
648   *                                 empty.
649   */
650  public static void ensureNotNullOrEmpty(final int[] array,
651                                          final String message)
652  {
653    if (array == null)
654    {
655      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
656           ERR_VALIDATOR_ARRAY_NULL_CUSTOM_MESSAGE.get(message,
657                StaticUtils.getStackTrace(
658                     Thread.currentThread().getStackTrace())));
659      Debug.debugCodingError(e);
660      throw e;
661    }
662    else if (array.length == 0)
663    {
664      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
665           ERR_VALIDATOR_ARRAY_EMPTY_CUSTOM_MESSAGE.get(message,
666                StaticUtils.getStackTrace(
667                     Thread.currentThread().getStackTrace())));
668      Debug.debugCodingError(e);
669      throw e;
670    }
671  }
672
673
674
675  /**
676   * Ensures that the provided array is not {@code null} and has a length of at
677   * least one.
678   *
679   * @param  array  The array to verify.
680   *
681   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
682   *                                 empty.
683   */
684  public static void ensureNotNullOrEmpty(final long[] array)
685  {
686    if (array == null)
687    {
688      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
689           ERR_VALIDATOR_ARRAY_NULL.get(StaticUtils.getStackTrace(
690                Thread.currentThread().getStackTrace())));
691      Debug.debugCodingError(e);
692      throw e;
693    }
694    else if (array.length == 0)
695    {
696      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
697           ERR_VALIDATOR_ARRAY_EMPTY.get(StaticUtils.getStackTrace(
698                Thread.currentThread().getStackTrace())));
699      Debug.debugCodingError(e);
700      throw e;
701    }
702  }
703
704
705
706  /**
707   * Ensures that the provided array is not {@code null} and has a length of at
708   * least one.
709   *
710   * @param  array    The array to verify.
711   * @param  message  The message to include in the exception thrown if the
712   *                  provided array is {@code null} or empty.
713   *
714   * @throws  LDAPSDKUsageException  If the provided array is {@code null} or
715   *                                 empty.
716   */
717  public static void ensureNotNullOrEmpty(final long[] array,
718                                          final String message)
719  {
720    if (array == null)
721    {
722      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
723           ERR_VALIDATOR_ARRAY_NULL_CUSTOM_MESSAGE.get(message,
724                StaticUtils.getStackTrace(
725                     Thread.currentThread().getStackTrace())));
726      Debug.debugCodingError(e);
727      throw e;
728    }
729    else if (array.length == 0)
730    {
731      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
732           ERR_VALIDATOR_ARRAY_EMPTY_CUSTOM_MESSAGE.get(message,
733                StaticUtils.getStackTrace(
734                     Thread.currentThread().getStackTrace())));
735      Debug.debugCodingError(e);
736      throw e;
737    }
738  }
739
740
741
742  /**
743   * Ensures that the provided character sequence is not {@code null} and has a
744   * length of at least one.
745   *
746   * @param  charSequence  The character sequence to verify.
747   *
748   * @throws  LDAPSDKUsageException  If the provided character sequence is
749   *                                 {@code null} or empty.
750   */
751  public static void ensureNotNullOrEmpty(final CharSequence charSequence)
752  {
753    if (charSequence == null)
754    {
755      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
756           ERR_VALIDATOR_CHAR_SEQUENCE_NULL.get(StaticUtils.getStackTrace(
757                Thread.currentThread().getStackTrace())));
758      Debug.debugCodingError(e);
759      throw e;
760    }
761    else if (charSequence.length() == 0)
762    {
763      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
764           ERR_VALIDATOR_CHAR_SEQUENCE_EMPTY.get(StaticUtils.getStackTrace(
765                Thread.currentThread().getStackTrace())));
766      Debug.debugCodingError(e);
767      throw e;
768    }
769  }
770
771
772
773  /**
774   * Ensures that the provided character sequence is not {@code null} and has a
775   * length of at least one.
776   *
777   * @param  charSequence  The character sequence to verify.
778   * @param  message        The message to include in the exception thrown if
779   *                        the provided character sequence is {@code null} or
780   *                        empty.
781   *
782   * @throws  LDAPSDKUsageException  If the provided character sequence is
783   *                                 {@code null} or empty.
784   */
785  public static void ensureNotNullOrEmpty(final CharSequence charSequence,
786                                          final String message)
787  {
788    if (charSequence == null)
789    {
790      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
791           ERR_VALIDATOR_CHAR_SEQUENCE_NULL_CUSTOM_MESSAGE.get(message,
792                StaticUtils.getStackTrace(
793                     Thread.currentThread().getStackTrace())));
794      Debug.debugCodingError(e);
795      throw e;
796    }
797    else if (charSequence.length() == 0)
798    {
799      final LDAPSDKUsageException e =  new LDAPSDKUsageException(
800           ERR_VALIDATOR_CHAR_SEQUENCE_EMPTY_CUSTOM_MESSAGE.get(message,
801                StaticUtils.getStackTrace(
802                     Thread.currentThread().getStackTrace())));
803      Debug.debugCodingError(e);
804      throw e;
805    }
806  }
807
808
809
810  /**
811   * Ensures that the provided condition is {@code true}.
812   *
813   * @param  condition  The condition to verify.
814   *
815   * @throws  LDAPSDKUsageException  If the provided condition is {@code false}.
816   */
817  public static void ensureTrue(final boolean condition)
818         throws LDAPSDKUsageException
819  {
820    if (! condition)
821    {
822      final LDAPSDKUsageException e = new LDAPSDKUsageException(
823           ERR_VALIDATOR_TRUE_CHECK_FAILURE.get(StaticUtils.getStackTrace(
824                Thread.currentThread().getStackTrace())));
825      Debug.debugCodingError(e);
826      throw e;
827    }
828  }
829
830
831
832  /**
833   * Ensures that the provided condition is {@code true}.
834   *
835   * @param  condition  The condition to verify.
836   * @param  message    The message to include in the exception thrown if the
837   *                    provided object is {@code null}.
838   *
839   * @throws  LDAPSDKUsageException  If the provided condition is {@code false}.
840   */
841  public static void ensureTrue(final boolean condition, final String message)
842         throws LDAPSDKUsageException
843  {
844    if (! condition)
845    {
846      final LDAPSDKUsageException e = new LDAPSDKUsageException(
847           ERR_VALIDATOR_FAILURE_CUSTOM_MESSAGE.get(message,
848                StaticUtils.getStackTrace(
849                     Thread.currentThread().getStackTrace())));
850      Debug.debugCodingError(e);
851      throw e;
852    }
853  }
854
855
856
857  /**
858   * Ensures that the provided condition is {@code false}.
859   *
860   * @param  condition  The condition to verify.
861   *
862   * @throws  LDAPSDKUsageException  If the provided condition is {@code true}.
863   */
864  public static void ensureFalse(final boolean condition)
865         throws LDAPSDKUsageException
866  {
867    if (condition)
868    {
869      final LDAPSDKUsageException e = new LDAPSDKUsageException(
870           ERR_VALIDATOR_FALSE_CHECK_FAILURE.get(StaticUtils.getStackTrace(
871                Thread.currentThread().getStackTrace())));
872      Debug.debugCodingError(e);
873      throw e;
874    }
875  }
876
877
878
879  /**
880   * Ensures that the provided condition is {@code false}.
881   *
882   * @param  condition  The condition to verify.
883   * @param  message    The message to include in the exception thrown if the
884   *                    provided object is {@code null}.
885   *
886   * @throws  LDAPSDKUsageException  If the provided condition is {@code true}.
887   */
888  public static void ensureFalse(final boolean condition, final String message)
889         throws LDAPSDKUsageException
890  {
891    if (condition)
892    {
893      final LDAPSDKUsageException e = new LDAPSDKUsageException(
894           ERR_VALIDATOR_FAILURE_CUSTOM_MESSAGE.get(message,
895                StaticUtils.getStackTrace(
896                     Thread.currentThread().getStackTrace())));
897      Debug.debugCodingError(e);
898      throw e;
899    }
900  }
901
902
903
904  /**
905   * Indicates that an expected condition was not true by throwing an
906   * {@link LDAPSDKUsageException} with the provided information.
907   *
908   * @param  message  The message to use for the resulting exception.  It must
909   *                  not be {@code null}.
910   *
911   * @throws  LDAPSDKUsageException  To indicate that a violation occurred.
912   */
913  public static void violation(final String message)
914         throws LDAPSDKUsageException
915  {
916    violation(message, null);
917  }
918
919
920
921  /**
922   * Indicates that an expected condition was not true by throwing an
923   * {@link LDAPSDKUsageException} with the provided information.
924   *
925   * @param  message  The message to use for the resulting exception.  It must
926   *                  not be {@code null}.
927   * @param  cause    The exception that triggered the violation.  It may be
928   *                  {@code null} if there is no associated exception.
929   *
930   * @throws  LDAPSDKUsageException  To indicate that a violation occurred.
931   */
932  public static void violation(final String message, final Throwable cause)
933         throws LDAPSDKUsageException
934  {
935    final LDAPSDKUsageException e = new LDAPSDKUsageException(message, cause);
936    Debug.debugCodingError(e);
937    throw e;
938  }
939}