utils008_fastjson中Feature类学习(更快描述某些属性)

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值