org.apache.sshd.common.util.ValidateUtils Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.sshd.common.util;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
/**
* @author Apache MINA SSHD Project
*/
public final class ValidateUtils {
private ValidateUtils() {
throw new UnsupportedOperationException("No instance");
}
public static T checkNotNull(T t, String message) {
checkTrue(t != null, message);
return t;
}
public static T checkNotNull(T t, String message, Object arg) {
checkTrue(t != null, message, arg);
return t;
}
public static T checkNotNull(T t, String message, long value) {
checkTrue(t != null, message, value);
return t;
}
public static T checkNotNull(T t, String message, Object... args) {
checkTrue(t != null, message, args);
return t;
}
public static String checkNotNullAndNotEmpty(String t, String message) {
t = checkNotNull(t, message).trim();
checkTrue(GenericUtils.length(t) > 0, message);
return t;
}
public static String checkNotNullAndNotEmpty(String t, String message, Object arg) {
t = checkNotNull(t, message, arg).trim();
checkTrue(GenericUtils.length(t) > 0, message, arg);
return t;
}
public static String checkNotNullAndNotEmpty(String t, String message, Object... args) {
t = checkNotNull(t, message, args).trim();
checkTrue(GenericUtils.length(t) > 0, message, args);
return t;
}
public static > M checkNotNullAndNotEmpty(M t, String message, Object... args) {
checkNotNull(t, message, args);
checkTrue(MapEntryUtils.size(t) > 0, message, args);
return t;
}
public static > C checkNotNullAndNotEmpty(C t, String message, Object... args) {
checkNotNull(t, message, args);
checkTrue(GenericUtils.size(t) > 0, message, args);
return t;
}
public static > C checkNotNullAndNotEmpty(C t, String message, Object... args) {
checkNotNull(t, message, args);
checkTrue(GenericUtils.isNotEmpty(t), message, args);
return t;
}
public static byte[] checkNotNullAndNotEmpty(byte[] a, String message) {
checkNotNull(a, message);
checkTrue(NumberUtils.length(a) > 0, message);
return a;
}
public static byte[] checkNotNullAndNotEmpty(byte[] a, String message, Object... args) {
checkNotNull(a, message, args);
checkTrue(NumberUtils.length(a) > 0, message, args);
return a;
}
public static char[] checkNotNullAndNotEmpty(char[] a, String message) {
checkNotNull(a, message);
checkTrue(GenericUtils.length(a) > 0, message);
return a;
}
public static char[] checkNotNullAndNotEmpty(char[] a, String message, Object... args) {
checkNotNull(a, message, args);
checkTrue(GenericUtils.length(a) > 0, message, args);
return a;
}
public static int[] checkNotNullAndNotEmpty(int[] a, String message) {
checkNotNull(a, message);
checkTrue(NumberUtils.length(a) > 0, message);
return a;
}
public static int[] checkNotNullAndNotEmpty(int[] a, String message, Object... args) {
checkNotNull(a, message, args);
checkTrue(NumberUtils.length(a) > 0, message, args);
return a;
}
public static T[] checkNotNullAndNotEmpty(T[] t, String message, Object... args) {
checkNotNull(t, message, args);
checkTrue(GenericUtils.length(t) > 0, message, args);
return t;
}
public static T checkInstanceOf(Object v, Class expected, String message, long value) {
Class> actual = checkNotNull(v, message, value).getClass();
checkTrue(expected.isAssignableFrom(actual), message, value);
return expected.cast(v);
}
public static T checkInstanceOf(Object v, Class expected, String message) {
return checkInstanceOf(v, expected, message, GenericUtils.EMPTY_OBJECT_ARRAY);
}
public static T checkInstanceOf(Object v, Class expected, String message, Object arg) {
Class> actual = checkNotNull(v, message, arg).getClass();
checkTrue(expected.isAssignableFrom(actual), message, arg);
return expected.cast(v);
}
public static T checkInstanceOf(Object v, Class expected, String message, Object... args) {
Class> actual = checkNotNull(v, message, args).getClass();
checkTrue(expected.isAssignableFrom(actual), message, args);
return expected.cast(v);
}
public static void checkTrue(boolean flag, String message) {
if (!flag) {
throwIllegalArgumentException(message, GenericUtils.EMPTY_OBJECT_ARRAY);
}
}
public static void checkTrue(boolean flag, String message, long value) {
if (!flag) {
throwIllegalArgumentException(message, value);
}
}
public static void checkTrue(boolean flag, String message, Object arg) {
if (!flag) {
throwIllegalArgumentException(message, arg);
}
}
public static void checkTrue(boolean flag, String message, Object... args) {
if (!flag) {
throwIllegalArgumentException(message, args);
}
}
public static void throwIllegalArgumentException(String format, Object... args) {
throw createFormattedException(IllegalArgumentException::new, format, args);
}
public static void checkState(boolean flag, String message) {
if (!flag) {
throwIllegalStateException(message, GenericUtils.EMPTY_OBJECT_ARRAY);
}
}
public static void checkState(boolean flag, String message, long value) {
if (!flag) {
throwIllegalStateException(message, value);
}
}
public static void checkState(boolean flag, String message, Object arg) {
if (!flag) {
throwIllegalStateException(message, arg);
}
}
public static void checkState(boolean flag, String message, Object... args) {
if (!flag) {
throwIllegalStateException(message, args);
}
}
public static void throwIllegalStateException(String format, Object... args) {
throw createFormattedException(IllegalStateException::new, format, args);
}
public static T createFormattedException(
Function super String, ? extends T> constructor, String format, Object... args) {
String message = String.format(format, args);
return constructor.apply(message);
}
public static T initializeExceptionCause(T err, Throwable cause) {
if (cause == null) {
return err;
}
if (err.getCause() != null) {
return err; // already initialized - avoid IllegalStateException
}
err.initCause(cause);
return err;
}
}