占位标签应用

定义:标签是更细维度的标识,用来附加更多属性标签,以此来区分复杂的业务标识,属性标签最大支持64种,用二进制位表示

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

一共64位,0表示false、1表示true,从右到左,每一位固定一种属性标签

代码如下

public static void main(String[] args) {
    System.out.println(getTagByTagSet(new HashSet<>(Arrays.asList("LongTag1","LongTag2","LongTag3","LongTag4","LongTag5"))));
    System.out.println(getTagSetByTag(31L));
    Byte[] byteTagByTagSet = getByteTagByTagSet(new HashSet<>(Arrays.asList("ByteTag1", "ByteTag2", "ByteTag3", "ByteTag4", "ByteTag5")));
    for (Byte b : byteTagByTagSet) {
        System.out.print((int)b);
    }
    System.out.println();
    Byte[] bytes = new Byte[]{31};
    System.out.println(getTagSetByByteTag(bytes));
}

/**
 * Long标签配置
 */
private static Map<String, TagCO> longTagConfig = new HashMap<String, TagCO>(){{
    put("LongTag1", new TagCO(0, "Long标签1"));
    put("LongTag2", new TagCO(1, "Long标签2"));
    put("LongTag3", new TagCO(2, "Long标签3"));
    put("LongTag4", new TagCO(3, "Long标签4"));
    put("LongTag5", new TagCO(4, "Long标签5"));
}};

/**
 * byte标签配置
 */
private static Map<String, TagCO> byteTagConfig = new HashMap<String, TagCO>(){{
    put("ByteTag1", new TagCO(0, "Byte标签1"));
    put("ByteTag2", new TagCO(1, "Byte标签2"));
    put("ByteTag3", new TagCO(2, "Byte标签3"));
    put("ByteTag4", new TagCO(3, "Byte标签4"));
    put("ByteTag5", new TagCO(4, "Byte标签5"));
}};

/**
 * 通过标签集合获取Tag
 */
public static Long getTagByTagSet(Set<String> tags) {
    if (CollectionUtils.isEmpty(tags)) {
        return 0L;
    }
    long tag = 0L;
    for (String tagStr : tags) {
        TagCO tagCO = longTagConfig.get(tagStr);
        tag = setBitStorageAttribute(tag, tagCO);
    }
    return tag;
}

/**
 * 通过Tag获取标签集合
 */
public static Set<String> getTagSetByTag(Long tag) {
    if (Objects.isNull(tag)) {
        return Collections.emptySet();
    }
    Set<String> result = Sets.newHashSet();
    for (String tagStr : longTagConfig.keySet()) {
        if (getBitStorageAttribute(tag, longTagConfig.get(tagStr))) {
            result.add(tagStr);
        }
    }
    return result;
}

/**
 * 通过标签集合获取tag
 */
public static Byte[] getByteTagByTagSet(Set<String> tags) {
    if (CollectionUtils.isEmpty(tags)) {
        return new Byte[0];
    }
    int maxBit = byteTagConfig.values().stream().mapToInt(TagCO::getBit).max().orElse(0);
    int maxLength = maxBit / 8 + 1;
    Byte[] localTag = new Byte[maxLength];
    for (String tag : tags) {
        TagCO tagCO = byteTagConfig.get(tag);
        localTag = setBitStorageAttribute(localTag, tagCO);
    }
    // 倒序第一个非NULL未知
    int revertFirstNoNullIndex = 0;
    for (int i = maxLength - 1; i >= 0; i--) {
        if (Objects.nonNull(localTag[i])) {
            revertFirstNoNullIndex = i;
            break;
        }
    }
    Byte[] byteTag = new Byte[revertFirstNoNullIndex + 1];
    for (int i = revertFirstNoNullIndex, j = 0; i >= 0; i--, j++) {
        // Byte数组倒转,再去除头部的null元素
        byteTag[i] = Objects.isNull(localTag[j]) ? 0 : localTag[j];
    }
    StringBuilder sb = new StringBuilder();
    for (Byte aByte : byteTag) {
        sb.append(StringUtils.leftPad(Integer.toBinaryString(Byte.toUnsignedInt(aByte)),  8, "0"));
    }
    return byteTag;
}

/**
 * 通过byteTag获取标签集合
 */
public static Set<String> getTagSetByByteTag(Byte[] byteTag) {
    if (Objects.isNull(byteTag)) {
        return Collections.emptySet();
    }
    // Byte数组倒转使用
    Byte[] localTag = new Byte[byteTag.length];
    for (int i = 0; i < byteTag.length; i++) {
        localTag[byteTag.length - 1 - i] = byteTag[i];
    }
    Set<String> tags = Sets.newHashSet();
    for (String tag : byteTagConfig.keySet()) {
        if (getBitStorageAttribute(localTag, byteTagConfig.get(tag))) {
            tags.add(tag);
        }
    }
    StringBuilder sb = new StringBuilder();
    for (Byte aByte : byteTag) {
        sb.append(StringUtils.leftPad(Integer.toBinaryString(Byte.toUnsignedInt(aByte)),  8, "0"));
    }
    return tags;
}

/**
 * 根据tag获取位存储属性
 */
public static boolean getBitStorageAttribute(Long tag, TagCO tagCO) {
    if (Objects.isNull(tag) || Objects.isNull(tagCO)) {
        return false;
    }
    return (tag & (1L << tagCO.getBit())) != 0;
}

/**
 * 设置位存储属性到tag中并返回
 */
public static long setBitStorageAttribute(Long tag, TagCO tagCO) {
    tag = Objects.isNull(tag) ? 0L : tag;
    if (Objects.isNull(tagCO)) {
        return tag;
    }
    return tag | (1L << tagCO.getBit());
}

/**
 * 根据tag获取位存储属性
 */
public static boolean getBitStorageAttribute(Byte[] tag, TagCO tagCO) {
    if (Objects.isNull(tag) || Objects.isNull(tagCO)) {
        return false;
    }
    int bitIndex = tagCO.getBit();
    int index = bitIndex / 8;
    int bitPosition = bitIndex % 8;

    if (tag.length * 8 - 1 < bitIndex) {
        return false;
    }

    tag[index] = Objects.isNull(tag[index]) ? 0 : tag[index];
    return (tag[index] & (1L << bitPosition)) != 0;
}

/**
 * 设置位存储属性到tag中并返回
 */
public static Byte[] setBitStorageAttribute(Byte[] tag, TagCO tagCO) {
    if (Objects.isNull(tagCO)) {
        return tag;
    }
    int bitIndex = tagCO.getBit();
    int index = bitIndex / 8;
    int bitPosition = bitIndex % 8;
    int maxLength = index + 1;

    tag = Objects.isNull(tag) ? new Byte[maxLength] : tag;

    if (tag.length * 8 - 1 < bitIndex) {
        throw new RuntimeException("置位错误!");
    }

    tag[index] = Objects.isNull(tag[index]) ? 0 : tag[index];
    tag[index] = (byte) (tag[index] | (1 << bitPosition));

    return tag;
}

@Data
public static class TagCO {

    /**
     * 二进制位数 从右往左 第一位为0
     */
    private Integer bit;

    /**
     * 描述
     */
    private String desc;

    TagCO(){

    }

    TagCO(Integer bit, String desc) {
        this.bit = bit;
        this.desc = desc;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值