package com.jingsong.test;
/**
* @author jingsong
* @date 2022/4/26 0:02
* @desc com.alibaba.fastjson.parser.Feature
* 该文件中提供了描述某一事物的方法,可以更快的描述某一事物的特征,下面为简单的学习
*/
public class JSONFeatureTest {
public static void main(String[] args) {
// 1. 位运算符复习 http://t.csdn.cn/Nuquy
/*
2. 假设某个人,可以拥有的爱好如Hobby
可以使用某些数字来代表一个人有哪些爱好
例如(二进制见Hobby类):
3 代表 [eat,run]
10 代表 [run,study]
21 代表 [eat,drink,sleep]
*/
// 3. 获得固有爱好
int defaultHobbies = Hobby.getDefaultHobbies();
System.out.println("defaultHobbies = " + defaultHobbies);
// 4. 判断是否有这个爱好
boolean likeEat = Hobby.isLike(defaultHobbies, Hobby.eat);
System.out.println("likeEat = " + likeEat);
boolean likeShopping = Hobby.isLike(defaultHobbies, Hobby.shopping);
System.out.println("likeShopping = " + likeShopping);
// 5. 添加一个爱好
int addHobby = Hobby.addHobby(defaultHobbies, Hobby.study);
System.out.println("addHobby = " + addHobby);
// 6. 取消一个爱好
int cancelHobby = Hobby.cancelHobby(defaultHobbies, Hobby.sleep);
System.out.println("cancelHobby = " + cancelHobby);
// 7. 记录某个人的爱好
int hobbies = Hobby.recordHobbies(Hobby.study, Hobby.sleep, Hobby.shopping, Hobby.run);
System.out.println("hobbies = " + hobbies);
}
}
enum Hobby {
// mask
eat, // 00000001
run, // 00000010
drink, // 00000100
study, // 00001000
sleep, // 00010000
shopping // 00100000
;
private final int mask;
Hobby() {
mask = (1 << ordinal());
}
public static boolean isLike(int hobbies, Hobby hobby) {
return (hobby.mask & hobbies) != 0;
}
public static int getDefaultHobbies() {
int defaultHobbies = 0;// 00000000
defaultHobbies |= eat.mask; // 00000001 | 00000000 = 00000001
defaultHobbies |= drink.mask;// 00000100 | 00000001 = 00000101
defaultHobbies |= sleep.mask;// 00010000 | 00000101 = 00010101
return defaultHobbies;
}
public static int addHobby(int hobbies, Hobby hobby) {
return hobbies |= hobby.mask;
}
public static int cancelHobby(int hobbies, Hobby hobby) {
return hobbies &= ~hobby.mask;
}
public static int recordHobbies(Hobby... hobbies) {
int h = 0;
for (Hobby hobby : hobbies) {
h |= hobby.mask;
}
return h;
}
}
utils008_fastjson中Feature类学习(更快描述某些属性)
最新推荐文章于 2023-12-05 16:14:35 发布