当前位置:   article > 正文

utils008_fastjson中Feature类学习(更快描述某些属性)_com.alibaba.fastjson.parser.feature

com.alibaba.fastjson.parser.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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/923072
推荐阅读
相关标签
  

闽ICP备14008679号