All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.apollographql.apollo.api.internal.Utils Maven / Gradle / Ivy

There is a newer version: 3.4.2
Show newest version
/**
 * Copyright 2018-2019 Amazon.com,
 * Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Amazon Software License (the "License").
 * You may not use this file except in compliance with the
 * License. A copy of the License is located at
 *
 *     http://aws.amazon.com/asl/
 *
 * or in the "license" file accompanying this file. This file is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, express or implied. See the License
 * for the specific language governing permissions and
 * limitations under the License.
 */

package com.apollographql.apollo.api.internal;

import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Contains utility methods for checking Preconditions
 */
public final class Utils {
  private Utils() {
  }

  /**
   * Checks if the object is null. Returns the object if it is not null, else throws a NullPointerException with the
   * error message.
   *
   * @param reference    the object whose nullability has to be checked
   * @param errorMessage the message to use with the NullPointerException
   * @param           the value type
   * @return The object itself
   * @throws NullPointerException if the object is null
   */
  @Nonnull public static  T checkNotNull(T reference, @Nullable Object errorMessage) {
    if (reference == null) {
      throw new NullPointerException(String.valueOf(errorMessage));
    }
    return reference;
  }

  /**
   * Checks if two {@link Set} are disjoint. Returns true if the sets don't have a single common element. Also returns
   * true if either of the sets is null.
   *
   * @param setOne the first set
   * @param setTwo the second set
   * @param     the value type contained within the sets
   * @return True if the sets don't have a single common element or if either of the sets is null.
   */
  public static  boolean areDisjoint(Set setOne, Set setTwo) {
    if (setOne == null || setTwo == null) {
      return true;
    }
    Set smallerSet = setOne;
    Set largerSet = setTwo;
    if (setOne.size() > setTwo.size()) {
      smallerSet = setTwo;
      largerSet = setOne;
    }
    for (E el : smallerSet) {
      if (largerSet.contains(el)) {
        return false;
      }
    }
    return true;
  }

  /**
   * Checks if the object is null. Returns the object if it is not null, else throws a NullPointerException.
   *
   * @param reference the object whose nullability has to be checked
   * @param        the value type
   * @return The object itself
   * @throws NullPointerException if the object is null
   */
  @Nonnull public static  T checkNotNull(T reference) {
    if (reference == null) {
      throw new NullPointerException();
    }
    return reference;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy