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