工具类之EmptyUtils

简单实用的判空工具类来了,不用老司机介绍你们也都能看懂,但最终还是由我来开车,哔哔~~

  • 判空相关→EmptyUtils.java→Test
isEmpty    : 判断对象是否为空
isNotEmpty : 判断对象是否非空

import android.os.Build;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.util.SparseLongArray;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * 
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/9/28
 *     desc  : 判空相关工具类
 * 
*/ public class EmptyUtils { private EmptyUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 判断对象是否为空 * * @param obj 对象 * @return {@code true}: 为空
{@code false}: 不为空 */ public static boolean isEmpty(Object obj) { if (obj == null) { return true; } if (obj instanceof String && obj.toString().length() == 0) { return true; } if (obj.getClass().isArray() && Array.getLength(obj) == 0) { return true; } if (obj instanceof Collection && ((Collection) obj).isEmpty()) { return true; } if (obj instanceof Map && ((Map) obj).isEmpty()) { return true; } if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) { return true; } if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) { return true; } if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) { return true; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) { return true; } } return false; } /** * 判断对象是否非空 * * @param obj 对象 * @return {@code true}: 非空
{@code false}: 空 */ public static boolean isNotEmpty(Object obj) { return !isEmpty(obj); } }

如果该工具类依赖其他工具类,都可以在我的Android开发人员不得不收集的代码(持续更新中)中找到。

你可能感兴趣的:(工具类之EmptyUtils)