开发支持类库

2020-12-23  本文已影响0人  曾梦想仗剑天涯

UUID类

import java.util.UUID;
public class UUIDApi {
    public static void main(String[] args) {
        UUID uid = UUID.randomUUID();
        System.out.println(uid);
    }
}

Optional类

package com.company.OptionalApi;
interface IMessage {
    public String getContent();
}
class MessageImpl implements IMessage {
    @Override
    public String getContent() {
        return "Hello World";
    }
}
class MessageUtil {
    private MessageUtil() {
    }
    public static IMessage getMessage() {
        return null;
    }
    public static void useMessage(IMessage msg) {
        if (msg != null) {
            System.out.println(msg.getContent());       //有可能会出现null而导致空指向
        }
    }
}
public class OptionalApi {
    public static void main(String[] args) throws Exception {
        MessageUtil.useMessage(MessageUtil.getMessage());
    }
}
此图来源于李兴华老师
package com.company.OptionalApi;
import java.util.Optional;
interface IMessage {
    public String getContent();
}
class MessageImpl implements IMessage {
    @Override
    public String getContent() {
        return "Hello World";
    }
}
class MessageUtil {
    private MessageUtil() {
    }
    public static Optional<IMessage> getMessage() {
        return Optional.of(new MessageImpl());
    }
    public static void useMessage(IMessage msg) {
        if (msg != null) {
            System.out.println(msg.getContent());       //有可能会出现null而导致空指向
        }
    }
}
public class OptionalApi {
    public static void main(String[] args) throws Exception {
        IMessage msg = MessageUtil.getMessage().get();
        MessageUtil.useMessage(msg);
    }
}
public static Optional<IMessage> getMessage() {
    return Optional.of(null);
}
public class OptionalApi {
    public static void main(String[] args) throws Exception {
        IMessage msg = MessageUtil.getMessage().orElse(new MessageImpl());
        MessageUtil.useMessage(msg);
    }
}

ThreadLocal类

package com.company.ThreadLocal;
class Channel {
    private static Message message;
    private Channel() {
    }
    public static void setMessage(Message m) {
        message = m;
    }
    public static void send() {
        System.out.println("【消息发送】" + message.getInfo());
    }
}
class Message {
    private String info;
    public void setInfo(String info) {
        this.info = info;
    }
    public String getInfo() {
        return info;
    }
}
public class ThreadLocalApi {
    public static void main(String[] args) throws Exception {
        Message msg = new Message();
        msg.setInfo("Hello World");
        Channel.setMessage(msg);
        Channel.send();
    }
}
public class ThreadLocalApi {
    public static void main(String[] args) throws Exception {
        for (int x = 0; x < 3; x++) {
            int num = x;
            new Thread(() -> {
                Message msg = new Message();
                msg.setInfo("第" + num + "个消息");
                Channel.setMessage(msg);
                Channel.send();
            }, "线程" + num).start();
        }
    }
}
此图来源于李兴华老师 此图来源于李兴华老师
package com.company.ThreadLocal;
class Channel {
    private static final ThreadLocal<Message> THREADLOCAL = new ThreadLocal<Message>();
    private Channel() {}
    public static void setMessage(Message m) {
        THREADLOCAL.set(m);
    }
    public static void send() {
        System.out.println(Thread.currentThread().getName() + "、【消息发送】" + THREADLOCAL.get().getInfo());
    }
}
class Message {
    private String info;
    public void setInfo(String info) {
        this.info = info;
    }
    public String getInfo() {
        return info;
    }
}
public class ThreadLocalApi {
    public static void main(String[] args) throws Exception {
        for (int x = 0; x < 3; x++) {
            int num = x;
            new Thread(() -> {
                Message msg = new Message();
                msg.setInfo("第" + num + "个消息");
                Channel.setMessage(msg);
                Channel.send();
            }, "线程" + num).start();
        }
    }
}

定时器

package com.company.TimerApi;
import java.util.Timer;
import java.util.TimerTask;
class MyTask extends TimerTask {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "、定时任务,当前时间:" + System.currentTimeMillis());
    }
}
public class TimerApi {
    public static void main(String[] args) throws Exception {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTask(), 100, 1000);
        //timer.schedule(new MyTask(), 1000);
    }
}

Base64加密与解密

package com.company.Base64APi;
import java.util.Base64;
public class Base64Api {
    public static void main(String[] args) throws Exception {
        String msg = "Hello World";
        String enMsg = new String(Base64.getEncoder().encode(msg.getBytes()));
        System.out.println(enMsg);
        String deMsg = new String(Base64.getDecoder().decode(enMsg));
        System.out.println(deMsg);
    }
}
String salt = "Mars";
String msg = "Hello World" + "{" + salt + "}";
package com.company.Base64APi;
import java.util.Base64;
class StringUtil {
    private static final String SALT = "Mars";
    private static final int REPEAT = 5;
    /** 
    * @Description: 加密处理 
    * @Param: [str要加密的字符串需要与盐值整合]
    * @return: java.lang.String 加密后的数据
    * @Author: mars_wu
    * @Date: 2020/12/25 下午6:14
    */ 
    public static String encode(String str) {
        String temp = str + "{" + SALT + "}";
        byte [] data = temp.getBytes();
        for (int x = 0; x < REPEAT; x++) {
            data = Base64.getEncoder().encode(data);
        }
        return new String(data);
    }
    /** 
    * @Description: 解密处理 
    * @Param: [str要解密的内容]
    * @return: java.lang.String 解密后的原始数据
    * @Author: mars_wu
    * @Date: 2020/12/25 下午6:22
    */ 
    public static String decode(String str) {
        byte [] data = str.getBytes();
        for (int x = 0; x < REPEAT; x++) {
            data = Base64.getDecoder().decode(data);
        }
        return new String(data).replaceAll("\\{\\w+\\}", "");
    }
}
public class Base64Api {
    public static void main(String[] args) throws Exception {
        String str = StringUtil.encode("Hello World");
        System.out.println(StringUtil.decode(str));
    }
}
上一篇下一篇

猜你喜欢

热点阅读