001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.mybatisflex.core.exception; 018 019import java.util.Collection; 020import java.util.Map; 021 022/** 023 * 断言。 024 * 025 * @author 王帅 026 * @since 2023-07-08 027 */ 028public final class FlexAssert { 029 030 private FlexAssert() { 031 } 032 033 /** 034 * 断言对象不为空,如果为空抛出异常,并指明哪个对象为空。 035 * 036 * @param obj 对象 037 * @param message 错误消息 038 * @throws MybatisFlexException 如果对象为空,抛出此异常。 039 */ 040 public static void notNull(Object obj, String message) { 041 if (obj == null) { 042 throw FlexExceptions.wrap(message); 043 } 044 } 045 046 /** 047 * 断言 Map 集合不为 {@code null} 或者空集合,如果为空则抛出异常,并指明为什么不允许为空集合。 048 * 049 * @param map Map 集合 050 * @param message 错误消息 051 * @throws MybatisFlexException 如果集合为空,抛出此异常。 052 */ 053 public static void notEmpty(Map<?, ?> map, String message) { 054 if (map == null || map.isEmpty()) { 055 throw FlexExceptions.wrap(message); 056 } 057 } 058 059 /** 060 * 断言集合不为 {@code null} 或者空集合,如果为空则抛出异常,并指明为什么不允许为空集合。 061 * 062 * @param collection 集合 063 * @param message 错误消息 064 * @throws MybatisFlexException 如果集合为空,抛出此异常。 065 */ 066 public static void notEmpty(Collection<?> collection, String message) { 067 if (collection == null || collection.isEmpty()) { 068 throw FlexExceptions.wrap(message); 069 } 070 } 071 072 /** 073 * 断言数组不为 {@code null} 或者空数组,如果为空则抛出异常,并指明为什么不允许为空数组。 074 * 075 * @param array 数组 076 * @param message 错误消息 077 * @throws MybatisFlexException 如果数组为空,抛出此异常。 078 */ 079 public static <T> void notEmpty(T[] array, String message) { 080 if (array == null || array.length == 0) { 081 throw FlexExceptions.wrap(message); 082 } 083 } 084 085}