View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2016 ForgeRock AS.
15   */
16  
17  package org.forgerock.api.util;
18  
19  import java.util.regex.Pattern;
20  
21  /**
22   * API Descriptor model-validation utilities.
23   */
24  public final class ValidationUtil {
25  
26      private static final Pattern FIND_WHITESPACE_PATTERN = Pattern.compile("\\s");
27  
28      private ValidationUtil() {
29          // empty
30      }
31  
32      /**
33       * Checks a {@code String} for whitespace.
34       *
35       * @param s {@code String} to validate
36       * @return {@code true} if contains whitespace and {@code false} otherwise
37       */
38      public static boolean containsWhitespace(final String s) {
39          return FIND_WHITESPACE_PATTERN.matcher(s).find();
40      }
41  
42      /**
43       * Determines if a {@code String} is {@code null}, or empty, or only contains whitespace.
44       *
45       * @param s {@code String} to validate
46       * @return {@code true} if {@code null} or empty, or only contains whitespace, and {@code false} otherwise
47       */
48      public static boolean isEmpty(final String s) {
49          return s == null || s.trim().isEmpty();
50      }
51  
52      /**
53       * Determines if an array is {@code null} or zero-length.
54       *
55       * @param a Array to validate
56       * @return {@code true} if {@code null} or empty, and {@code false} otherwise
57       */
58      public static boolean isEmpty(final Object[] a) {
59          return a == null || a.length == 0;
60      }
61  
62      /**
63       * Checks that there is only a single single non-{@code null} argument.
64       *
65       * @param args Arguments
66       * @return {@code true} if there is a single non-{@code null} argument, and {@code false} otherwise
67       */
68      public static boolean isSingleNonNull(final Object... args) {
69          boolean found = false;
70          if (args != null) {
71              for (final Object o : args) {
72                  if (o != null) {
73                      if (found) {
74                          // there is more than one non-null argument
75                          return false;
76                      }
77                      found = true;
78                  }
79              }
80          }
81          return found;
82      }
83  
84      /**
85       * Returns false if the given Boolean parameter is {@code null}.
86       *
87       * @param boolVal boolean parameter to check
88       * @return {@code false} if the parameter is null, and the parameter itself otherwise
89       */
90      public static boolean nullToFalse(Boolean boolVal) {
91          return (boolVal == null) ? false : boolVal;
92      }
93  }