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