com.fitbur.assertj.util.Preconditions Maven / Gradle / Ivy
/**
* 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.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.util;
import static java.lang.String.format;
import com.fitbur.assertj.api.filter.FilterOperator;
/**
* Verifies correct argument values and state. Borrowed from Guava.
*
* @author [email protected] (Alex Ruiz)
*/
public final class Preconditions {
public static final String ARGUMENT_EMPTY = "Argument expected not to be empty!";
/**
* Verifies that the given {@code CharSequence} is not {@code null} or empty.
*
* @param s the given {@code CharSequence}.
* @return the validated {@code CharSequence}.
* @throws NullPointerException if the given {@code CharSequence} is {@code null}.
* @throws IllegalArgumentException if the given {@code CharSequence} is empty.
*/
public static CharSequence checkNotNullOrEmpty(CharSequence s) {
return checkNotNullOrEmpty(s, ARGUMENT_EMPTY);
}
/**
* Verifies that the given {@code CharSequence} is not {@code null} or empty.
*
* @param s the given {@code CharSequence}.
* @param message error message in case of empty {@code String}.
* @return the validated {@code CharSequence}.
* @throws NullPointerException if the given {@code CharSequence} is {@code null}.
* @throws IllegalArgumentException if the given {@code CharSequence} is empty.
*/
public static CharSequence checkNotNullOrEmpty(CharSequence s, String message) {
checkNotNull(s, message);
if (s.length() == 0) throwExceptionForBeingEmpty(message);
return s;
}
/**
* Verifies that the given array is not {@code null} or empty.
*
* @param array the given array.
* @return the validated array.
* @throws NullPointerException if the given array is {@code null}.
* @throws IllegalArgumentException if the given array is empty.
*/
public static T[] checkNotNullOrEmpty(T[] array) {
T[] checked = checkNotNull(array);
if (checked.length == 0) throwExceptionForBeingEmpty();
return checked;
}
/**
* Verifies that the given object reference is not {@code null}.
*
* @param reference the given object reference.
* @return the non-{@code null} reference that was validated.
* @throws NullPointerException if the given object reference is {@code null}.
*/
public static T checkNotNull(T reference) {
if (reference == null) throw new NullPointerException();
return reference;
}
/**
* Verifies that the given object reference is not {@code null}.
*
* @param reference the given object reference.
* @param message error message in case of null reference.
* @return the non-{@code null} reference that was validated.
* @throws NullPointerException if the given object reference is {@code null}.
*/
public static T checkNotNull(T reference, String message) {
if (reference == null) throw new NullPointerException(message);
return reference;
}
/**
* Verifies that the given FilterOperator reference is not {@code null}.
*
* @param reference the given object reference.
* @throws NullPointerException if the given object reference is {@code null}.
*/
public static void checkNotNull(FilterOperator filterOperator) {
// @format:off
if (filterOperator == null) throw new IllegalArgumentException(format("The expected value should not be null.%n"
+ "If you were trying to filter on a null value, please use filteredOnNull(String propertyOrFieldName) instead"));
// @format:on
}
private Preconditions() {}
private static void throwExceptionForBeingEmpty() {
throwExceptionForBeingEmpty(ARGUMENT_EMPTY);
}
private static void throwExceptionForBeingEmpty(String message) {
throw new IllegalArgumentException(message);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy