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

com.github.tomakehurst.wiremock.common.ParameterUtils Maven / Gradle / Ivy

There is a newer version: 3.9.2
Show newest version
/*
 * Copyright (C) 2023-2024 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock.common;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Predicate;

public class ParameterUtils {

  private ParameterUtils() {}

  public static  T getFirstNonNull(T first, T second) {
    if (first != null) {
      return first;
    }
    if (second != null) {
      return second;
    }
    throw new NullPointerException("Both parameters are null");
  }

  public static  T getFirstNonNull(T first, T second, String etr) {
    if (first != null) {
      return first;
    }
    if (second != null) {
      return second;
    }
    throw new NullPointerException(etr);
  }

  public static void checkParameter(boolean condition, String errorMessage) {
    if (!condition) {
      throw new IllegalArgumentException(errorMessage);
    }
  }

  public static void checkState(boolean expression, String errorMessage) {
    if (!expression) {
      throw new IllegalStateException(errorMessage);
    }
  }

  public static  T checkNotNull(T value, String errorMessage) {
    if (value == null) {
      throw new NullPointerException(errorMessage);
    }
    return value;
  }

  public static  int indexOf(Iterable iterable, Predicate predicate) {
    checkNotNull(iterable, "iterable");
    checkNotNull(predicate, "predicate");
    Iterator iterator = iterable.iterator();
    for (int i = 0; iterator.hasNext(); i++) {
      T current = iterator.next();
      if (predicate.test(current)) {
        return i;
      }
    }

    return -1;
  }

  public static  T getFirst(Iterable iterable, T defaultValue) {
    return iterable != null && iterable.iterator().hasNext()
        ? iterable.iterator().next()
        : defaultValue;
  }

  public static  T getLast(Iterable iterable) {
    if (iterable == null || !iterable.iterator().hasNext()) {
      throw new NoSuchElementException();
    }
    Iterator iterator = iterable.iterator();
    while (true) {
      T current = iterator.next();
      if (!iterator.hasNext()) {
        return current;
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy