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

com.alipay.sofa.ark.common.util.AssertUtils Maven / Gradle / Ivy

There is a newer version: 3.1.9.rpa
Show newest version
/*
 * 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 com.alipay.sofa.ark.common.util;

/**
 *
 * @author ruoshan
 * @since 0.1.0
 */
public class AssertUtils {

    /**
     * Validate current object must not be null
     *
     * @param instance object instance
     * @param msg error message
     * @throws IllegalArgumentException if object instance is null
     */
    public static void assertNotNull(Object instance, String msg) {
        if (instance == null) {
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * Validate current object must be null
     *
     * @param instance object instance
     * @param msg error message
     * @throws IllegalArgumentException if object instance is null
     */
    public static void assertNull(Object instance, String msg) {
        if (instance != null) {
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * 

Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.

* * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message, null array not recommended * @throws IllegalArgumentException if expression is {@code false} */ public static void isTrue(final boolean expression, final String message, final Object... values) { if (!expression) { throw new IllegalArgumentException(String.format(message, values)); } } /** *

Validate that the argument condition is {@code false}; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.

* * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message, null array not recommended * @throws IllegalArgumentException if expression is {@code false} */ public static void isFalse(final boolean expression, final String message, final Object... values) { if (expression) { throw new IllegalArgumentException(String.format(message, values)); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy