工具包的常用工具类 自我总结

1 .依赖 lang3

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
</dependency>

2.判空

StringUtils.isNotEmpty()

Boolean a=Boolean.FALSE;
System.out.println(BooleanUtils.isTrue(a));

3.时间

  @Test
    public void time(){
        //格式化时间
        System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        //时间加一天
        System.out.println(DateUtils.addDays(new Date(), 1));
    }

4.字符串判空

StringUtils.equals(a,b)

5. 布尔值判断

Boolean.TRUE.equals(ptacSoOrderApprovalApplyDto.getBusinessAutoAuditFlag())

6. 判断集合长度 collections4

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>
  			System.out.println(CollectionUtils.size(list1));   --> 0
        System.out.println("++++"+list1.size());  --> 直接报空指针异常

6.2 CollectionUtils.isNotEmpty(l1)底层 代码实现是

! (list==nul  ||  list.size==0)
    public static boolean isNotEmpty(Collection<?> coll) {
        return !isEmpty(coll);
    }
  public static boolean isEmpty(Collection<?> coll) {
        return coll == null || coll.isEmpty();
    }
    public boolean isEmpty() {
        return size() == 0;
    }

7. fastjson 转换

    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.3</version>
        </dependency>
  @Test
    public void testWrapperw1() throws JSONException {
        Map<String, Object> result = new HashMap<>();
        JSONObject object = new JSONObject();
        object.put("fid", 111);
        result.put("data", object);

        String data = result.get("data").toString();

        String fid = JSON.parseObject(data).get("fid").toString();
        System.out.println("fid = " + fid);
    }

8.id 雪花算法id 生成

在这里插入图片描述

测试

    @Test
    public void testWrapperw1222() throws JSONException {

        Long aLong = IdSnowflake.getLocalInstance().nextId(WrapperTest.class);
        Long aLong1 = IdSnowflake.getLocalInstance().nextId(WrapperTest.class);
        Long aLong2 = IdSnowflake.getLocalInstance().nextId(WrapperTest.class);

        System.out.println(aLong);
        System.out.println("aLong1 = " + aLong1);
        System.out.println("aLong2 = " + aLong2);
    }

工具类


import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.codec.digest.DigestUtils;

public class IdSnowflake {
    private final long startTime = 1488697858401L;
    private long workerIdBits = 5L;
    private long datacenterIdBits = 5L;
    private long maxWorkerId;
    private long maxDatacenterId;
    private long sequenceBits;
    private long workerIdLeftShift;
    private long datacenterIdLeftShift;
    private long timestampLeftShift;
    private long sequenceMask;
    private long workerId;
    private long sequence;
    private long lastTimestamp;
    private static IdSnowflake local;

    public IdSnowflake(long workerId) {
        this.maxWorkerId = ~(-1L << (int)this.workerIdBits);
        this.maxDatacenterId = ~(-1L << (int)this.datacenterIdBits);
        this.sequenceBits = 12L;
        this.workerIdLeftShift = this.sequenceBits;
        this.datacenterIdLeftShift = this.workerIdBits + this.workerIdLeftShift;
        this.timestampLeftShift = this.datacenterIdBits + this.datacenterIdLeftShift;
        this.sequenceMask = (long)(~(-1 << (int)this.sequenceBits));
        this.sequence = 0L;
        this.lastTimestamp = -1L;
        if (workerId >= 0L && workerId <= this.maxWorkerId) {
            this.workerId = workerId;
        } else {
            throw new IllegalArgumentException(String.format("workerId[%d] is less than 0 or greater than maxWorkerId[%d].", workerId, this.maxWorkerId));
        }
    }

    public IdSnowflake(long workerId, long datacenterId) {
        this.maxWorkerId = ~(-1L << (int)this.workerIdBits);
        this.maxDatacenterId = ~(-1L << (int)this.datacenterIdBits);
        this.sequenceBits = 12L;
        this.workerIdLeftShift = this.sequenceBits;
        this.datacenterIdLeftShift = this.workerIdBits + this.workerIdLeftShift;
        this.timestampLeftShift = this.datacenterIdBits + this.datacenterIdLeftShift;
        this.sequenceMask = (long)(~(-1 << (int)this.sequenceBits));
        this.sequence = 0L;
        this.lastTimestamp = -1L;
        if (workerId >= 0L && workerId <= this.maxWorkerId) {
            if (datacenterId >= 0L && datacenterId <= this.maxDatacenterId) {
                this.workerId = workerId;
            } else {
                throw new IllegalArgumentException(String.format("datacenterId[%d] is less than 0 or greater than maxDatacenterId[%d].", datacenterId, this.maxDatacenterId));
            }
        } else {
            throw new IllegalArgumentException(String.format("workerId[%d] is less than 0 or greater than maxWorkerId[%d].", workerId, this.maxWorkerId));
        }
    }

    public synchronized Long nextId(Class<?> clazz) {
        String md5 = DigestUtils.md5Hex(clazz.getName());
        int datacenterId = Integer.parseInt(md5.substring(0, 1), 16) + Integer.parseInt(md5.substring(30, 31), 16) - 1;
        long timestamp = this.timeGen();
        if (timestamp < this.lastTimestamp) {
            throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", this.lastTimestamp - timestamp));
        } else {
            if (timestamp == this.lastTimestamp) {
                this.sequence = this.sequence + 1L & this.sequenceMask;
                if (this.sequence == 0L) {
                    timestamp = this.tilNextMillis();
                    this.sequence = 0L;
                }
            } else {
                this.sequence = 0L;
            }

            this.lastTimestamp = timestamp;
            return timestamp - 1488697858401L << (int)this.timestampLeftShift | (long)(datacenterId << (int)this.datacenterIdLeftShift) | this.workerId << (int)this.workerIdLeftShift | this.sequence;
        }
    }

    protected long tilNextMillis() {
        long timestamp = this.timeGen();
        if (timestamp <= this.lastTimestamp) {
            timestamp = this.timeGen();
        }

        return timestamp;
    }

    protected long timeGen() {
        return System.currentTimeMillis();
    }

    public static synchronized IdSnowflake getLocalInstance() {
        if (local == null) {
            int workId = 1;

            try {
                InetAddress ia = InetAddress.getLocalHost();
                String hostAddress = ia.getHostAddress();
                hostAddress = DigestUtils.md5Hex(hostAddress);
                workId = Integer.parseInt(hostAddress.substring(0, 1), 16) + Integer.parseInt(hostAddress.substring(30, 31), 16);
            } catch (UnknownHostException var3) {
                var3.printStackTrace();
            }

            local = new IdSnowflake((long)workId);
        }

        return local;
    }
}

9.字符串 递增

需求
在这里插入图片描述

在这里插入图片描述

@Test
    public void testWrapperw1222() throws JSONException {
        for (int i = 0; i < 10; i++) {
            String format = String.format("%04d", i);
            System.out.println(format);
        }
    }




结果
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009

10 .字符串动态注入

 @Test
    public void testString(){

        String s = "【%s】验证码:%s,您正在小程序内绑定手机号,验证码3分钟内有效,不要告诉别人哦";
        String format = String.format(message, 12345, 1111);
        System.out.println(format);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值