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 java.util.function.Supplier; 019 020/** 021 * Object 工具类 022 * 023 * @author warm 024 * @since 2023/5/18 9:42 025 */ 026public class ObjectUtil { 027 028 /** 029 * 判断一个对象是否为空 030 * 031 * @param object Object 032 * @return true:为空 false:非空 033 */ 034 public static boolean isNull(Object object) { 035 return object == null; 036 } 037 038 /** 039 * 判断一个对象是否非空 040 * 041 * @param object Object 042 * @return true:非空 false:空 043 */ 044 public static boolean isNotNull(Object object) { 045 return !isNull(object); 046 } 047 048 @SuppressWarnings("unchecked") 049 public static <T> T cast(Object obj) { 050 return (T) obj; 051 } 052 053 /** 054 * 判断字符串是否为true 055 * 056 * @param str 057 * @return 058 */ 059 public static boolean isStrTrue(String str) { 060 return StringUtils.isNotEmpty(str) && "true".equals(str); 061 } 062 063 /** 064 * 如果被检查对象为 {@code null}, 返回默认值(由 defaultValueSupplier 提供);否则直接返回 065 * 066 * @param source 被检查对象 067 * @param defaultValueSupplier 默认值提供者 068 * @param <T> 对象类型 069 * @return 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值 070 * @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出 071 * @since 5.7.20 072 */ 073 public static <T> T defaultIfNull(T source, Supplier<? extends T> defaultValueSupplier) { 074 if (isNull(source)) { 075 return defaultValueSupplier.get(); 076 } 077 return source; 078 } 079}