TimeUnitEnum.java 1000 Bytes
package com.yohoufo.msg.constants;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 时间单位,1:秒,2:分钟,3:小时,4:天,5:周,6:月,7:年
 * Created by chao.chen on 2018/12/5.
 */
public enum  TimeUnitEnum {
    SECOND((short)1, TimeUnit.SECONDS),
    MIN((short)2, TimeUnit.MINUTES),
    HOUR((short)3, TimeUnit.HOURS),
    DAY((short)4, TimeUnit.DAYS);

    short code;
    private TimeUnit timeUnit;

    TimeUnitEnum(short code, TimeUnit timeUnit) {
        this.code = code;
        this.timeUnit = timeUnit;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public short getCode() {
        return code;
    }

    private static Map<Short,TimeUnitEnum> cache;

    static {
        cache = new HashMap<>(values().length);
        for(TimeUnitEnum tu: values()){
            cache.put(tu.code, tu);
        }
    }

    public static TimeUnitEnum getByCode(Short code){
        return cache.get(code);
    }
}