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 */ 016package com.mybatisflex.core.exception; 017 018/** 019 * MybatisFlexException 异常封装类 020 */ 021public final class FlexExceptions { 022 023 private FlexExceptions() { 024 } 025 026 027 /** 028 * 封装 MybatisFlexException 异常 029 * 030 * @param throwable 异常 031 * @return MybatisFlexException 032 */ 033 public static MybatisFlexException wrap(Throwable throwable) { 034 if (throwable instanceof MybatisFlexException) { 035 return (MybatisFlexException) throwable; 036 } 037 return new MybatisFlexException(throwable); 038 } 039 040 041 /** 042 * 封装 MybatisFlexException 异常 043 * 044 * @param throwable 异常 045 * @param msg 消息 046 * @param params 消息参数 047 * @return MybatisFlexException 048 */ 049 public static MybatisFlexException wrap(Throwable throwable, String msg, Object... params) { 050 return new MybatisFlexException(String.format(msg, params), throwable); 051 } 052 053 /** 054 * 封装 MybatisFlexException 异常 055 * 056 * @param msg 消息 057 * @param params 消息参数 058 * @return MybatisFlexException 059 */ 060 public static MybatisFlexException wrap(String msg, Object... params) { 061 return new MybatisFlexException(String.format(msg, params)); 062 } 063 064 065 /** 066 * 断言 condition 必须为 true 067 * 068 * @param condition 条件 069 * @param msg 消息 070 * @param params 消息参数 071 */ 072 public static void assertTrue(boolean condition, String msg, Object... params) { 073 if (!condition) { 074 throw wrap(msg, params); 075 } 076 } 077 078 079 /** 080 * 断言传入的内容不能为 null 081 */ 082 public static void assertNotNull(Object object, String msg, Object params) { 083 assertTrue(object != null, msg, params); 084 } 085 086 087 /** 088 * 断言传入的数组内容不能为 null 或者 空 089 */ 090 public static <T> void assertAreNotNull(T[] elements, String msg, Object params) { 091 if (elements == null || elements.length == 0) { 092 throw wrap(msg, params); 093 } 094 for (T element : elements) { 095 if (element == null) { 096 throw wrap(msg, params); 097 } 098 } 099 } 100}