001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.util;
017
018import javax.annotation.Nullable;
019
020/**
021 * Analogues of methods from guava's Preconditions
022 */
023public final class PreconditionUtils {
024
025  private PreconditionUtils() {
026  }
027
028  /**
029   * Ensures the truth of an expression involving one or more parameters to the calling method.
030   *
031   * @param expression a boolean expression
032   * @throws IllegalArgumentException if {@code expression} is false
033   */
034  public static void checkArgument(boolean expression) {
035    if (!expression) {
036      throw new IllegalArgumentException();
037    }
038  }
039
040  /**
041   * Ensures the truth of an expression involving one or more parameters to the calling method.
042   *
043   * @param expression a boolean expression
044   * @param errorMessage the exception message to use if the check fails; will be converted to a
045   *     string using {@link String#valueOf(Object)}
046   * @throws IllegalArgumentException if {@code expression} is false
047   */
048  public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
049    if (!expression) {
050      throw new IllegalArgumentException(String.valueOf(errorMessage));
051    }
052  }
053}