001/**
002 * Copyright (c) 2010-2019 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;
028
029public class ObjectUtil {
030
031  private ObjectUtil() {
032    // prevent instantiation
033  }
034
035  /**
036   * Ensures that {@code obj} isn't {@code null} or an empty string.
037   *
038   * @param obj
039   *          The parameter to check.
040   * @param errorText
041   *          The exception message.
042   * @throws IllegalArgumentException
043   *           If {@code obj} is {@code null} or an empty string.
044   */
045  public static String requireNotEmpty(String obj, String errorText) {
046    if (isBlank(obj)) {
047      throw new IllegalArgumentException(errorText);
048    }
049    return obj;
050  }
051
052  /**
053   * Checks is the object is a empty 'collection' or 'map'.
054   * 
055   * @param obj
056   *          the object that is checked
057   * @return {@code true} if the given object is a empty collection or an empty map, {@code false} otherwise
058   */
059  public static boolean isEmptyCollectionOrMap(Object obj) {
060    if (obj instanceof Collection) {
061      return ((Collection) obj).isEmpty();
062    }
063
064    return (obj instanceof Map && ((Map) obj).isEmpty());
065  }
066
067  /**
068   * Ensures that {@code parameter} isn't {@code null} or an empty string.
069   *
070   * @param parameterName
071   *          The name of the parameter (to be used in exception message).
072   * @param parameter
073   *          The parameter to check.
074   * @throws IllegalArgumentException
075   *           If {@code parameter} is {@code null} or an empty string.
076   */
077  public static void verifyParameterPresence(String parameterName, String parameter) {
078    verifyParameterPresence(parameterName, (Object) parameter);
079    if (parameter.trim().length() == 0) {
080      throw new IllegalArgumentException("The '" + parameterName + "' parameter cannot be an empty string.");
081    }
082  }
083
084  /**
085   * Ensures that {@code parameter} isn't {@code null}.
086   *
087   * @param parameterName
088   *          The name of the parameter (to be used in exception message).
089   * @param parameter
090   *          The parameter to check.
091   * @throws NullPointerException
092   *           If {@code parameter} is {@code null}.
093   */
094  public static void verifyParameterPresence(String parameterName, Object parameter) {
095    if (parameter == null) {
096      throw new NullPointerException("The '" + parameterName + "' parameter cannot be null.");
097    }
098  }
099}