当前位置:首页 > IT技术 > 编程语言 > 正文

Java工具集-对象(ObjectUtils)
2022-03-06 18:08:39


简单工具类


写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦
网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是
发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些
甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用
每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能
做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独
使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用.
抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!


介绍


遵从两大原则



  • 1.绝不依赖JDK以外的源码
  • 2.牺牲代码复用性,每个类都必须是单独的组件,绝不互相引用,做到完全解耦

package *;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

/**
* @program: simple_tools
* @description:
* @author: ChenWenLong
* @create: 2019-05-30 16:49
**/
public class ObjectUtils {

public static final Null NULL = new Null();
//单例的NULL
public static class Null implements Serializable {

private static final long serialVersionUID = 7092611880189329093L;
Null() {
super();
}
private Object readResolve() {
return ObjectUtils.NULL;
}
}
public ObjectUtils() {
super();
}

/**
* 功能描述:
* 〈设置一个默认的对象〉
*
* @params : [object, defaultValue]
* @return : java.lang.Object
* @author : cwl
* @date : 2019/5/30 16:52
*/
public static Object defaultIfNull(Object object, Object defaultValue) {
return object != null ? object : defaultValue;
}

/**
* 功能描述:
* 〈判断object1和object2是否相等〉
*
* @params : [object1, object2]
* @return : boolean
* @author : cwl
* @date : 2019/5/30 16:52
*/
public static boolean equal(Object object1, Object object2) {
if (object1 == object2) {
return true;
}
if ((object1 == null) || (object2 == null)) {
return false;
}
return object1.equals(object2);
}

/**
* 功能描述:
* 〈获得obj的hashcode值,如果对象为null,则默认为0〉
*
* @params : [obj]
* @return : int
* @author : cwl
* @date : 2019/5/30 16:53
*/
public static int hashCode(Object obj) {
return (obj == null) ? 0 : obj.hashCode();
}

/**
* 功能描述:
* 〈以字符串的形式返回一个字符串,该字符串是全限定类名+@+hashCode的16进制〉
*
* @params : [object]
* @return : java.lang.String
* @author : cwl
* @date : 2019/5/30 16:55
*/
public static String identityToString(Object object) {
if (object == null) {
return null;
}
StringBuffer buffer = new StringBuffer();
buffer.append(object.getClass().getName())
.append('@')
.append(Integer.toHexString(System.identityHashCode(object)));
return buffer.toString();
}

/**
* 功能描述:
* 〈对象的toString方法,对null进行空字符串处理〉
*
* @params : [obj]
* @return : java.lang.String
* @author : cwl
* @date : 2019/5/30 16:59
*/
public static String toString(Object obj) {
return obj == null ? "" : obj.toString();
}

/**
* 功能描述:
* 〈对象的toString方法,对null进行自定义nullStr处理〉
*
* @params : [obj, nullStr]
* @return : java.lang.String
* @author : cwl
* @date : 2019/5/30 17:01
*/
public static String toString(Object obj, String nullStr) {
return obj == null ? nullStr : obj.toString();
}

/**
* 功能描述:
* 〈取实现了Comparable接口的对象的最小值,自定义比较方法〉
*
* @params : [c1, c2]
* @return : java.lang.Object
* @author : cwl
* @date : 2019/5/30 17:02
*/
public static Object min(Comparable c1, Comparable c2) {
if (c1 != null && c2 != null) {
return c1.compareTo(c2) < 1 ? c1 : c2;
} else {
return c1 != null ? c1 : c2;
}
}

/**
* 功能描述:
* 〈取实现了Comparable接口的对象的最大值,自定义比较方法〉
*
* @params : [c1, c2]
* @return : java.lang.Object
* @author : cwl
* @date : 2019/5/30 17:02
*/
public static Object max(Comparable c1, Comparable c2) {
if (c1 != null && c2 != null) {
return c1.compareTo(c2) >= 0 ? c1 : c2;
} else {
return c1 != null ? c1 : c2;
}
}

/**
* 对象中是否包含元素<br>
* 支持的对象类型包括:
* <ul>
* <li>String</li>
* <li>Collection</li>
* <li>Map</li>
* <li>Iterator</li>
* <li>Enumeration</li>
* <li>Array</li>
* </ul>
*
* @param obj 对象
* @param element 元素
* @return 是否包含
*/
public static boolean contains(Object obj, Object element) {
if (obj == null) {
return false;
}
if (obj instanceof String) {
if (element == null) {
return false;
}
return ((String) obj).contains(element.toString());
}
if (obj instanceof Collection) {
return ((Collection<?>) obj).contains(element);
}
if (obj instanceof Map) {
return ((Map<?, ?>) obj).values().contains(element);
}

if (obj instanceof Iterator) {
Iterator<?> iter = (Iterator<?>) obj;
while (iter.hasNext()) {
Object o = iter.next();
if (equal(o, element)) {
return true;
}
}
return false;
}
if (obj instanceof Enumeration) {
Enumeration<?> enumeration = (Enumeration<?>) obj;
while (enumeration.hasMoreElements()) {
Object o = enumeration.nextElement();
if (equal(o, element)) {
return true;
}
}
return false;
}
if (obj.getClass().isArray() == true) {
int len = Array.getLength(obj);
for (int i = 0; i < len; i++) {
Object o = Array.get(obj, i);
if (equal(o, element)) {
return true;
}
}
}
return false;
}

/**
* 检查对象是否为null<br>
* 判断标准为:
* <pre>
* 1. == null
* 2. equals(null)
* </pre>
*
* @param obj 对象
* @return 是否为null
*/
public static boolean isNull(Object obj) {
return null == obj || obj.equals(null);
}

/**
* 检查对象是否不为null
*
* @param obj 对象
* @return 是否为null
*/
public static boolean isNotNull(Object obj) {
return null != obj && false == obj.equals(null);
}

}



本文摘自 :https://blog.51cto.com/u