001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 003 * 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 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 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 */ 016package org.dromara.warm.flow.core.utils; 017 018import org.dromara.warm.flow.core.exception.FlowException; 019 020import java.util.Collection; 021import java.util.Map; 022 023/** 024 * Assert类 025 * 026 * @author warm 027 * @since 2023/3/30 14:05 028 */ 029public class AssertUtil { 030 private AssertUtil() { 031 032 } 033 034 public static void isNull(Object obj, String errorMsg) { 035 if (obj == null) { 036 throw new FlowException(errorMsg); 037 } 038 } 039 040 public static void isNotNull(Object obj, String errorMsg) { 041 if (obj == null) { 042 throw new FlowException(errorMsg); 043 } 044 } 045 046 /** 047 * 为true不抛异常 048 * 049 * @param obj 050 * @param errorMsg 051 */ 052 public static void isFalse(boolean obj, String errorMsg) { 053 if (!obj) { 054 throw new FlowException(errorMsg); 055 } 056 } 057 058 /** 059 * 为false不抛异常 060 * 061 * @param obj 062 * @param errorMsg 063 */ 064 public static void isTrue(boolean obj, String errorMsg) { 065 if (obj) { 066 throw new FlowException(errorMsg); 067 } 068 } 069 070 public static void isNotEmpty(Object obj, String errorMsg) { 071 if (obj != null) { 072 if (obj instanceof String) { 073 AssertUtil.isTrue(StringUtils.isNotEmpty((String) obj), errorMsg); 074 } else if (obj instanceof Collection) { 075 AssertUtil.isTrue(CollUtil.isNotEmpty((Collection<?>) obj), errorMsg); 076 } else if (obj instanceof Map) { 077 AssertUtil.isTrue(MapUtil.isNotEmpty((Map<?, ?>) obj), errorMsg); 078 } else { 079 throw new FlowException("Unsupported type: " + obj.getClass().getName()); 080 } 081 } 082 } 083 084 085 public static void isEmpty(Object obj, String errorMsg) { 086 if (obj == null) { 087 throw new FlowException(errorMsg); 088 } else if (obj instanceof String) { 089 AssertUtil.isTrue(StringUtils.isEmpty((String) obj), errorMsg); 090 } else if (obj instanceof Collection) { 091 AssertUtil.isTrue(CollUtil.isEmpty((Collection<?>) obj), errorMsg); 092 } else if (obj instanceof Map) { 093 AssertUtil.isTrue(MapUtil.isEmpty((Map<?, ?>) obj), errorMsg); 094 } else { 095 throw new FlowException("Unsupported type: " + obj.getClass().getName()); 096 } 097 } 098 099 public static <T> void contains(Collection<T> a, T b, String errorMsg) { 100 if (CollUtil.isNotEmpty(a) && a.contains(b)) { 101 throw new FlowException(errorMsg); 102 } 103 } 104 105 public static <T> void notContains(Collection<T> a, T b, String errorMsg) { 106 if (CollUtil.isEmpty(a) || !a.contains(b)) { 107 throw new FlowException(errorMsg); 108 } 109 } 110 111}