简介
本算法为等级与时间结合计算对应优先级逻辑
等级越高者优先级越高
同等级下,时间越小者优先级越高
实现
主方法
calculatePriority
import com.zk.blog.enums.TypeEnum;
import org.apache.commons.lang3.StringUtils;
/**
* @program:
* @description:
* @author: zk
* @create: 2024-08-09 17:26
**/
public class PriorityService {
/**
* 基线时间
* 2024-01-01 00:00:00
* 秒值
*/
private static final Long baseTime = 1704067200L;
/**
* 前缀
*/
private static final String prefix = "1";
/**
* 按照阶梯类型+时间计算优先级
* 类型级别越高、时间越前者优先级越高
*
* @param type 类型 类型优先级最大2位
* @param time 计算时间
* @return 优先级结果 :越大优先级越高
*/
public String calculatePriority(TypeEnum type, Long time) {
//毫秒转秒
if (String.valueOf(time).length() == 13) {
time = time / 1000;
}
if (String.valueOf(time).length() != 10) {
throw new RuntimeException("时间格式错误");
}
String typePriority = String.valueOf(type.getLevelPriority());
String timePriority = String.valueOf(Integer.MAX_VALUE - (time - baseTime));
typePriority = StringUtils.leftPad(typePriority, 2, "0");
timePriority = StringUtils.leftPad(timePriority, 12, "0");
return new StringBuilder().append(prefix).append(typePriority).append(timePriority).toString();
}
}
采用:
Integer.MAX_VALUE
- (time
-基线时间
)计算出时间优先级
等级优先级为2位,不够则左侧补0
时间优先级为12位,不够则左侧补0
最终优先级::1位前缀
+2位级别优先级
+12位时间优先级
=13位字符串
等级类型枚举
TypeEnum
/**
* @program:
* @description: 类型级别
* @author: zk
* @create: 2024-08-09 17:30
**/
public enum TypeEnum {
/**
* 级别:描述, 等级优先级
*/
ONE("一级", 1),
TWO("二级", 2),
THREE("三级", 3),
FOUR("四级", 4),
FIVE("五级", 5),
SIX("六级", 6),
SEVEN("七级", 7),
EIGHT("八级", 8),
NINE("九级", 9),
TEN("十级", 10);
private String name;
private Integer levelPriority;
TypeEnum(String name, Integer levelPriority) {
this.name = name;
this.levelPriority = levelPriority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevelPriority() {
return levelPriority;
}
public void setLevelPriority(Integer levelPriority) {
this.levelPriority = levelPriority;
}
}
测试
- 测试代码
@Test
public void test(){
//一级 + 2024-02-01 00:00:00
System.out.println(calculatePriority(TypeEnum.ONE, 1706745600L));
//一级 + 2024-03-01 00:00:00
System.out.println(calculatePriority(TypeEnum.ONE, 1709251200L));
//九级 + 2024-02-01 00:00:00
System.out.println(calculatePriority(TypeEnum.NINE, 1706745600L));
//九级 + 2024-03-01 00:00:00
System.out.println(calculatePriority(TypeEnum.NINE, 1709251200L));
//十级 + 2024-02-01 00:00:00
System.out.println(calculatePriority(TypeEnum.TEN, 1706745600L));
//十级 + 2024-03-01 00:00:00
System.out.println(calculatePriority(TypeEnum.TEN, 1709251200L));
}
- 测试结果
101002144805247
101002142299647
109002144805247
109002142299647
110002144805247
110002142299647
结束
标签:00,优先级,String,TypeEnum,2024,算法,等级,calculatePriority From: https://www.cnblogs.com/zktww/p/18351272