001/* 002 * Copyright (c) 2010-2021 Mark Allen, Norbert Bartels. 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy 005 * of this software and associated documentation files (the "Software"), to deal 006 * in the Software without restriction, including without limitation the rights 007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 008 * copies of the Software, and to permit persons to whom the Software is 009 * furnished to do so, subject to the following conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in 012 * all copies or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 020 * THE SOFTWARE. 021 */ 022package com.restfb.util; 023 024import static com.restfb.util.StringUtils.isBlank; 025 026import java.util.Collection; 027import java.util.Map; 028import java.util.Objects; 029import java.util.function.Supplier; 030 031public class ObjectUtil { 032 033 private ObjectUtil() { 034 // prevent instantiation 035 } 036 037 /** 038 * Ensures that {@code obj} isn't {@code null} or an empty string. 039 * 040 * @param obj 041 * The parameter to check. 042 * @param errorText 043 * The exception message. 044 * @throws IllegalArgumentException 045 * If {@code obj} is {@code null} or an empty string. 046 */ 047 public static String requireNotEmpty(String obj, String errorText) { 048 if (isBlank(obj)) { 049 throw new IllegalArgumentException(errorText); 050 } 051 return obj; 052 } 053 054 /** 055 * Ensures that {@code obj} isn't {@code null}. 056 * 057 * @param obj 058 * The parameter to check. 059 * @param exceptionSupplier 060 * The supplier for the exception that is thrown if obj is null. 061 * @throws T 062 * If {@code obj} is {@code null}. 063 */ 064 public static <T extends Exception> void requireNotNull(Object obj, Supplier<T> exceptionSupplier) throws T { 065 if (obj == null) { 066 throw exceptionSupplier.get(); 067 } 068 } 069 070 /** 071 * Checks is the object is a empty 'collection' or 'map'. 072 * 073 * @param obj 074 * the object that is checked 075 * @return {@code true} if the given object is a empty collection or an empty map, {@code false} otherwise 076 */ 077 public static boolean isEmptyCollectionOrMap(Object obj) { 078 if (obj instanceof Collection) { 079 return ((Collection) obj).isEmpty(); 080 } 081 082 return (obj instanceof Map && ((Map) obj).isEmpty()); 083 } 084 085 /** 086 * Ensures that {@code parameter} isn't {@code null} or an empty string. 087 * 088 * @param parameterName 089 * The name of the parameter (to be used in exception message). 090 * @param parameter 091 * The parameter to check. 092 * @throws IllegalArgumentException 093 * If {@code parameter} is {@code null} or an empty string. 094 */ 095 public static void verifyParameterPresence(String parameterName, String parameter) { 096 verifyParameterPresence(parameterName, (Object) parameter); 097 requireNotEmpty(parameter, "The '" + parameterName + "' parameter cannot be an empty string."); 098 } 099 100 /** 101 * Ensures that {@code parameter} isn't {@code null}. 102 * 103 * @param parameterName 104 * The name of the parameter (to be used in exception message). 105 * @param parameter 106 * The parameter to check. 107 * @throws NullPointerException 108 * If {@code parameter} is {@code null}. 109 */ 110 public static void verifyParameterPresence(String parameterName, Object parameter) { 111 Objects.requireNonNull(parameter, "The '" + parameterName + "' parameter cannot be null."); 112 } 113}