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.query;
017
018import com.mybatisflex.core.util.ClassUtil;
019import com.mybatisflex.core.util.StringUtil;
020
021import java.lang.reflect.Array;
022import java.util.Collection;
023import java.util.Map;
024
025public class If {
026
027    private If() {
028    }
029
030    /**
031     * 判断对象是否为空
032     */
033    public static boolean isNull(Object object) {
034        return object == null;
035    }
036
037    /**
038     * 判断对象是否非空
039     */
040    public static boolean notNull(Object object) {
041        return !isNull(object);
042    }
043
044    /**
045     * 查看某个对象是否为空,支持数组、集合、map 等
046     *
047     * @param object
048     */
049    public static boolean notEmpty(Object object) {
050        if (object == null) {
051            return false;
052        }
053
054        if (object instanceof Collection) {
055            return !((Collection<?>) object).isEmpty();
056        }
057
058        if (ClassUtil.isArray(object.getClass())) {
059            return Array.getLength(object) > 0;
060        }
061
062        if (object instanceof Map) {
063            return !((Map<?, ?>) object).isEmpty();
064        }
065
066        if (object instanceof String) {
067            return StringUtil.isNotBlank((String) object);
068        }
069        return true;
070    }
071
072
073    /**
074     * 查看某个对象是否为空数据 或者 null
075     *
076     * @param object
077     */
078    public static boolean isEmpty(Object object) {
079        return !notEmpty(object);
080    }
081
082
083    /**
084     * 查看某个 string 对象是否有文本内容
085     *
086     * @param object
087     */
088    public static boolean hasText(Object object) {
089        return object != null && StringUtil.isNotBlank((String) object);
090    }
091
092}