001/*
002 * Copyright (c) 2010-2024 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.Optional;
030import java.util.function.Supplier;
031
032public class ObjectUtil {
033
034  private ObjectUtil() {
035    // prevent instantiation
036  }
037
038  /**
039   * Ensures that {@code obj} isn't {@code null} or an empty string.
040   *
041   * @param obj
042   *          The parameter to check.
043   * @param errorText
044   *          The exception message.
045   * @throws IllegalArgumentException
046   *           If {@code obj} is {@code null} or an empty string.
047   */
048  public static String requireNotEmpty(String obj, String errorText) {
049    if (isBlank(obj)) {
050      throw new IllegalArgumentException(errorText);
051    }
052    return obj;
053  }
054
055  public static void requireNotEmpty(Collection<?> collection, String errorText) {
056    if (collection == null || collection.isEmpty()) {
057      throw new IllegalArgumentException(errorText);
058    }
059  }
060
061  /**
062   * Ensures that {@code obj} isn't {@code null}.
063   *
064   * @param obj
065   *          The parameter to check.
066   * @param exceptionSupplier
067   *          The supplier for the exception that is thrown if obj is null.
068   * @throws T
069   *           If {@code obj} is {@code null}.
070   */
071  public static <T extends Exception> void requireNotNull(Object obj, Supplier<T> exceptionSupplier) throws T {
072    Optional.ofNullable(obj).orElseThrow(exceptionSupplier);
073  }
074
075  /**
076   * Checks is the object is a empty 'collection' or 'map'.
077   * 
078   * @param obj
079   *          the object that is checked
080   * @return {@code true} if the given object is a empty collection or an empty map, {@code false} otherwise
081   */
082  public static boolean isEmptyCollectionOrMap(Object obj) {
083    if (obj instanceof Collection) {
084      return ((Collection) obj).isEmpty();
085    }
086
087    return (obj instanceof Map && ((Map) obj).isEmpty());
088  }
089
090  /**
091   * Ensures that {@code parameter} isn't {@code null} or an empty string.
092   *
093   * @param parameterName
094   *          The name of the parameter (to be used in exception message).
095   * @param parameter
096   *          The parameter to check.
097   * @throws IllegalArgumentException
098   *           If {@code parameter} is {@code null} or an empty string.
099   */
100  public static void verifyParameterPresence(String parameterName, String parameter) {
101    verifyParameterPresence(parameterName, (Object) parameter);
102    requireNotEmpty(parameter, "The '" + parameterName + "' parameter cannot be an empty string.");
103  }
104
105  /**
106   * Ensures that {@code parameter} isn't {@code null}.
107   *
108   * @param parameterName
109   *          The name of the parameter (to be used in exception message).
110   * @param parameter
111   *          The parameter to check.
112   * @throws NullPointerException
113   *           If {@code parameter} is {@code null}.
114   */
115  public static void verifyParameterPresence(String parameterName, Object parameter) {
116    Objects.requireNonNull(parameter, "The '" + parameterName + "' parameter cannot be null.");
117  }
118}