自己写的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工具类之——BigDecimal运算封装(包含金额的计算方式)
- Java序列化和反序列化超强工具类(包含tif图片与其他格式互转)
- Java工具类之——BigDecimal运算封装(包含金额的计算方式)
- Java工具类之——BigDecimal运算封装(包含金额的计算方式)
- java日期计算工具类【包含常用的日期计算方法】
- Java工具类之——BigDecimal运算封装(包含金额的计算方式)
- java工具类,自定义注解参数校验 自定义注解redis缓存工具,文件工具 时间工具 加密等
- 不使用comm.jar的java串口通讯工具类,使用自己编写的dll类
- java从命令行参数args提取多个参数的工具类
- 计算Java中任意一个方法的执行时间的工具类
- java中判断一个字符串是否包含另外一个字符串,如果包含,计算出字符串的开始位置
- java图形验证码生成工具类及web页面校验验证码
- Java中List或者HashMap对象-序列化和反序列化并附读写文件操作
- Java对象序列化给分布式计算带来的方便
- 巧妙的...【可变数组】参数校验非空工具类
- Java序列化工具类
- 3、定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数。参数是此结构体类型的变量,返回值是整数。
- Java 中集合类型包含ArrayList、LinkedList、HashMap等类,下列描述正确的是(多选)?
- java图形验证码生成工具类及web页面校验验证码
- 定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数。参数是此结构体类型的变量,返回值是整数。