您的位置:首页 > 编程语言 > Java开发

自己写的java工具类(包含序列化,参数校验,判空,HashMap,分页计算)

2017-11-01 17:27 477 查看
没有其他特殊依赖,拷贝过去即可用.

package org.xx.xx

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class Utils {
private static Utils myUtil = null;
public static final Integer EMAIL = 1;
public static final Integer USERNAME = 2;
public static final Integer PASSWORD = 3;
public static final Integer TEL = 4;

private Utils() {
}

/**
* 单例模式,返回该类对象
*
* @dagewang 2017年11月1日
*/
public static Utils getUtil() {
if (myUtil == null) {
synchronized (Utils.class) {
if (myUtil == null) {
myUtil = new Utils();
}
}
}
return myUtil;
}

/**
* 参数的校验,type在本类中有常量表示
*
* @dagewang 2017年11月1日
*/
public boolean check(Integer type, String data) {
boolean result = false;
if (type == EMAIL) {
result = data.matches("[\\w]+@[\\w]+\\.{1}[\\w]{2,3}");
} else if (type == USERNAME) {
result = data.matches("[a-zA-Z]{1}+[\\w]{7,19}");
} else if (type == PASSWORD) {
result = data.matches("^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).{8,22}$");
} else if (type == TEL) {
result = data.matches("[0-9]{11}");
}
return result;
}

/**
* 参数判空,可变长度
*
* @dagewang 2017年10月24日
*/
public boolean isNull(Object... obj) {
for (int i = 0; i < obj.length; i++) {
if (obj[i] == null || obj[i].equals("")) {
return true;
}
}
return false;
}

/**
* 通过总数和每页需要多少条,计算有多少页
*
* @dagewang 2017年7月31日
*/
public Integer getPage(Integer count, Integer pageNum) {
double size = (double) count;
size = size / pageNum;
return size < 1 ? 1 :(int)Math.ceil(size);
}

/**
* 序列化
*
* @dagewang 2017年11月1日
*/
public byte[] serialize(Object object) throws Exception {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
return baos.toByteArray();
}

/**
* 反序列化
*
* @throws Exception
*
* @dagewang 2017年11月1日
*/
public static Object unserialize(byte[] bytes) throws Exception {
ByteArrayInputStream bais = null;
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}

/**
* 构造HashMap
*
* @dagewang 2017年11月1日
*/
public  HashMap newHashMap() {
return new HashMap();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 分页 hashmap
相关文章推荐