001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2016 ForgeRock AS. 015 */ 016 017package org.forgerock.api.util; 018 019import java.util.regex.Pattern; 020 021/** 022 * API Descriptor model-validation utilities. 023 */ 024public final class ValidationUtil { 025 026 private static final Pattern FIND_WHITESPACE_PATTERN = Pattern.compile("\\s"); 027 028 private ValidationUtil() { 029 // empty 030 } 031 032 /** 033 * Checks a {@code String} for whitespace. 034 * 035 * @param s {@code String} to validate 036 * @return {@code true} if contains whitespace and {@code false} otherwise 037 */ 038 public static boolean containsWhitespace(final String s) { 039 return FIND_WHITESPACE_PATTERN.matcher(s).find(); 040 } 041 042 /** 043 * Determines if a {@code String} is {@code null}, or empty, or only contains whitespace. 044 * 045 * @param s {@code String} to validate 046 * @return {@code true} if {@code null} or empty, or only contains whitespace, and {@code false} otherwise 047 */ 048 public static boolean isEmpty(final String s) { 049 return s == null || s.trim().isEmpty(); 050 } 051 052 /** 053 * Determines if an array is {@code null} or zero-length. 054 * 055 * @param a Array to validate 056 * @return {@code true} if {@code null} or empty, and {@code false} otherwise 057 */ 058 public static boolean isEmpty(final Object[] a) { 059 return a == null || a.length == 0; 060 } 061 062 /** 063 * Checks that there is only a single single non-{@code null} argument. 064 * 065 * @param args Arguments 066 * @return {@code true} if there is a single non-{@code null} argument, and {@code false} otherwise 067 */ 068 public static boolean isSingleNonNull(final Object... args) { 069 boolean found = false; 070 if (args != null) { 071 for (final Object o : args) { 072 if (o != null) { 073 if (found) { 074 // there is more than one non-null argument 075 return false; 076 } 077 found = true; 078 } 079 } 080 } 081 return found; 082 } 083 084 /** 085 * Returns false if the given Boolean parameter is {@code null}. 086 * 087 * @param boolVal boolean parameter to check 088 * @return {@code false} if the parameter is null, and the parameter itself otherwise 089 */ 090 public static boolean nullToFalse(Boolean boolVal) { 091 return (boolVal == null) ? false : boolVal; 092 } 093}