java: Bit Operation Algorithm

项目结构:

01d0bad6-2e0e-4ad7-8f3a-035de7c8e2d9

 

/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:15
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : BitMask.java
 * explain   : 学习  类
 **/

package BitOperation.valueobject;

/**
 * 珠宝业务全局位掩码常量,对应Go bit_mask.go
 */
public final class BitMask {
    // 材质掩码
    public static final int GOLD     = 1 << 0;
    public static final int KGOLD    = 1 << 1;
    public static final int PLATINUM = 1 << 2;
    public static final int DIAMOND  = 1 << 3;
    public static final int JADE     = 1 << 4;
    public static final int PEARL    = 1 << 5;
    public static final int RUBY     = 1 << 6;
    public static final int SAPPHIRE = 1 << 7;

    // 风格掩码
    public static final int LUXURY     = 1 << 10;
    public static final int SIMPLE     = 1 << 11;
    public static final int RETRO      = 1 << 12;
    public static final int MINIMALIST = 1 << 13;

    // 价位档位掩码
    public static final int LOWPRICE  = 1 << 20;
    public static final int MIDPRICE  = 1 << 21;
    public static final int HIGHPRICE = 1 << 22;

    // 私有构造,禁止实例化
    private BitMask(){}
}


/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:16
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : BlackList.java
 * explain   : 学习  类
 **/

package BitOperation.entity;

import java.util.HashSet;
import java.util.Set;

public class BlackList {
    private final Set<String> blackSet = new HashSet<>();

    public void addFake(String id){
        blackSet.add(id);
    }

    public boolean contains(String id){
        return blackSet.contains(id);
    }
}


/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:16
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : Filter.java
 * explain   : 学习  类
 **/

package BitOperation.entity;

public class Filter {
    public BlackList BlackList = new BlackList();
}


/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:16
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : Guide.java
 * explain   : 学习  类
 **/

package BitOperation.entity;


import java.util.List;

public class Guide {
    public String GuideID;
    public int SkillMask;
    public List<String> CustomerPriority;
}

/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:17
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : Customer.java
 * explain   : 学习  类
 **/

package BitOperation.entity;

import java.util.List;

public class Customer {
    public String CustomerID;
    public int PreferenceMask;
    public List<String> PriorityList;
}


/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:17
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : Jewelry.java
 * explain   : 学习  类
 **/

package BitOperation.entity;

public class Jewelry {
    public String JewelID;
    public double Weight;
    public double Price;
    public int MaterialMask;
}

/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:17
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : MatchDomainRule.java
 * explain   : 学习  类
 **/

package BitOperation.domain.rule;

import BitOperation.entity.Customer;
import BitOperation.entity.Guide;
import BitOperation.valueobject.BitMask;
import java.util.ArrayList;
import java.util.List;

public class MatchDomainRule {
    // 全局固定常量,对应Go包常量
    public static final int GUIDE_OFFLINE_MASK  = 1 << 30;
    public static final int CUSTOMER_BLACK_MASK = 1 << 31;
    public static final int SENIOR_GUIDE_TAG    = 1 << 28;

    public int LUXURY_MASK;

    // 构造函数初始化组合掩码
    public MatchDomainRule(){
        this.LUXURY_MASK = BitMask.HIGHPRICE | BitMask.DIAMOND;
    }

    /**
     * 导购合法性校验
     */
    public ResultMsg isGuideValid(Guide g){
        if(g == null){
            return new ResultMsg(false, "实体类型错误,非导购Guide对象");
        }
        if(g.GuideID == null || g.GuideID.isEmpty()){
            return new ResultMsg(false, "导购ID不能为空");
        }
        if(g.CustomerPriority == null || g.CustomerPriority.isEmpty()){
            return new ResultMsg(false, "导购未配置客户接待优先级列表,无法参与匹配");
        }
        if((g.SkillMask & GUIDE_OFFLINE_MASK) != 0){
            return new ResultMsg(false, "导购[" + g.GuideID + "]当前离岗,禁止分配客户");
        }
        return new ResultMsg(true, "导购校验通过");
    }

    /**
     * 客户合法性校验
     */
    public ResultMsg isCustomerValid(Customer c){
        if(c == null){
            return new ResultMsg(false, "实体类型错误,非客户Customer对象");
        }
        if(c.CustomerID == null || c.CustomerID.isEmpty()){
            return new ResultMsg(false, "客户ID不能为空");
        }
        if(c.PriorityList == null || c.PriorityList.isEmpty()){
            return new ResultMsg(false, "客户未配置导购偏好优先级,无法参与匹配");
        }
        if((c.PreferenceMask & CUSTOMER_BLACK_MASK) != 0){
            return new ResultMsg(false, "客户[" + c.CustomerID + "]处于黑名单,禁止分配导购接待");
        }
        return new ResultMsg(true, "客户校验通过");
    }

    /**
     * 高奢导购资质校验
     */
    public ResultMsg checkLuxuryMatchLimit(Guide g, Customer c){
        boolean needLuxury = (c.PreferenceMask & this.LUXURY_MASK) != 0;
        if(!needLuxury){
            return new ResultMsg(true, "非高奢需求,无导购等级限制");
        }
        if((g.SkillMask & SENIOR_GUIDE_TAG) == 0){
            return new ResultMsg(false,
                    "客户["+c.CustomerID+"]存在高奢需求,导购["+g.GuideID+"]非资深导购,禁止匹配");
        }
        return new ResultMsg(true, "高奢需求匹配资格校验通过");
    }

    /**
     * 批量准入过滤
     */
    public BatchFilterResult batchCheckMatchEntrance(List<Guide> guides, List<Customer> customers){
        List<Guide> validGuide = new ArrayList<>();
        List<Customer> validCust = new ArrayList<>();
        List<String> errLog = new ArrayList<>();

        for(Guide g : guides){
            ResultMsg res = isGuideValid(g);
            if(res.success){
                validGuide.add(g);
            }else{
                errLog.add("导购过滤:" + res.msg);
            }
        }
        for(Customer c : customers){
            ResultMsg res = isCustomerValid(c);
            if(res.success){
                validCust.add(c);
            }else{
                errLog.add("客户过滤:" + res.msg);
            }
        }
        return new BatchFilterResult(validGuide, validCust, errLog);
    }

    // 内部结果封装
    public static class ResultMsg{
        public boolean success;
        public String msg;
        public ResultMsg(boolean s, String m){
            success = s;
            msg = m;
        }
    }
    public static class BatchFilterResult{
        public List<Guide> validGuide;
        public List<Customer> validCust;
        public List<String> errLog;
        public BatchFilterResult(List<Guide> g, List<Customer> c, List<String> e){
            validGuide = g;
            validCust = c;
            errLog = e;
        }
    }
}


/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:18
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : CombineDomainRule.java
 * explain   : 学习  类
 **/

package BitOperation.domain.rule;


import BitOperation.entity.Jewelry;
import BitOperation.valueobject.BitMask;
import java.util.ArrayList;
import java.util.List;

public class CombineDomainRule {
    public int MAX_COMBINE_COUNT;
    public double MIN_COMBINE_WEIGHT;
    public int MUTEX_GOLD;
    public int MUTEX_PLATINUM;

    public CombineDomainRule(){
        MAX_COMBINE_COUNT = 6;
        MIN_COMBINE_WEIGHT = 1.0;
        MUTEX_GOLD = BitMask.GOLD;
        MUTEX_PLATINUM = BitMask.PLATINUM;
    }

    /**
     * 单件首饰校验
     */
    public MatchDomainRule.ResultMsg isSingleJewelValid(Jewelry j){
        if(j == null){
            return new MatchDomainRule.ResultMsg(false, "对象非首饰Jewelry实体");
        }
        if(j.JewelID == null || j.JewelID.isEmpty()){
            return new MatchDomainRule.ResultMsg(false, "首饰唯一ID为空");
        }
        if(j.Weight <= 0){
            return new MatchDomainRule.ResultMsg(false, "首饰[" + j.JewelID + "]克重非法,必须大于0");
        }
        if(j.Price <= 0){
            return new MatchDomainRule.ResultMsg(false, "首饰[" + j.JewelID + "]价格非法,必须大于0");
        }
        return new MatchDomainRule.ResultMsg(true, "单件首饰校验通过");
    }

    /**
     * 黄金铂金互斥校验
     */
    public MatchDomainRule.ResultMsg checkMaterialMutex(int combineMask){
        boolean hasGold = (combineMask & MUTEX_GOLD) != 0;
        boolean hasPt = (combineMask & MUTEX_PLATINUM) != 0;
        if(hasGold && hasPt){
            return new MatchDomainRule.ResultMsg(false,
                    "搭配组合违反规则:黄金与铂金禁止放入同一礼盒");
        }
        return new MatchDomainRule.ResultMsg(true, "材质互斥校验通过");
    }

    /**
     * 重量价格阈值校验
     */
    public MatchDomainRule.ResultMsg checkCombineThreshold(double totalW, double totalP, double maxW, double maxP){
        if(totalW < MIN_COMBINE_WEIGHT){
            return new MatchDomainRule.ResultMsg(false, "总克重低于礼盒最低克重1.0g");
        }
        if(totalW > maxW){
            return new MatchDomainRule.ResultMsg(false, "总克重超出上限");
        }
        if(totalP > maxP){
            return new MatchDomainRule.ResultMsg(false, "总价超出价格上限");
        }
        return new MatchDomainRule.ResultMsg(true, "搭配阈值校验通过");
    }

    /**
     * 件数上限校验
     */
    public MatchDomainRule.ResultMsg checkCombineCount(int cnt){
        if(cnt > MAX_COMBINE_COUNT){
            return new MatchDomainRule.ResultMsg(false, "搭配件数超出礼盒最大件数6件");
        }
        return new MatchDomainRule.ResultMsg(true, "件数校验通过");
    }

    /**
     * 批量过滤合法首饰
     */
    public FilterJewelryResult batchFilterAvailableJewels(List<Jewelry> list){
        List<Jewelry> valid = new ArrayList<>();
        List<String> errLog = new ArrayList<>();
        for(Jewelry j : list){
            MatchDomainRule.ResultMsg res = isSingleJewelValid(j);
            if(res.success){
                valid.add(j);
            }else{
                errLog.add(res.msg);
            }
        }
        return new FilterJewelryResult(valid, errLog);
    }

    /**
     * 组合全量综合校验
     */
    public MatchDomainRule.ResultMsg fullCombineCheck(int mask, List<Jewelry> jewels,
                                                      double totalW, double totalP, double maxW, double maxP){
        MatchDomainRule.ResultMsg res;
        res = checkCombineCount(jewels.size());
        if(!res.success) return res;

        res = checkMaterialMutex(mask);
        if(!res.success) return res;

        res = checkCombineThreshold(totalW, totalP, maxW, maxP);
        if(!res.success) return res;

        return new MatchDomainRule.ResultMsg(true, "搭配组合完全合规");
    }

    public static class FilterJewelryResult{
        public List<Jewelry> validList;
        public List<String> errLog;
        public FilterJewelryResult(List<Jewelry> v, List<String> e){
            validList = v;
            errLog = e;
        }
    }
}

  

调用:

/**
 * encoding: utf-8
 * 版权所有 2026 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:Bit Operation Algorithm
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 21
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2026 - 2026/8/5 - 21:19
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : JavaAlgorithms
 * File      : BitOperationBll.java
 * explain   : 学习  类
 **/

package Bll;
import BitOperation.domain.rule.CombineDomainRule;
import BitOperation.domain.rule.MatchDomainRule;
import BitOperation.entity.Customer;
import BitOperation.entity.Filter;
import BitOperation.entity.Guide;
import BitOperation.entity.Jewelry;
import BitOperation.valueobject.BitMask;
import java.util.*;


public class BitOperationBll {

    /**
     *
     * */
    public  void Demo()
    {
        testMatchRule();
        System.out.println();
        testCombineRule();
        System.out.println();
        testBlackListFilter();
    }

    /**
     * 导购客户匹配测试
     *
     * */
    static void testMatchRule(){
        System.out.println("===导购客户稳定匹配结果(带规则校验)===");
        MatchDomainRule matchRule = new MatchDomainRule();

        // 构造离岗导购 G001
        Guide g1 = new Guide();
        g1.GuideID = "G001";
        g1.SkillMask = BitMask.DIAMOND | BitMask.GOLD | MatchDomainRule.GUIDE_OFFLINE_MASK;
        g1.CustomerPriority = Arrays.asList("C001");

        Guide g2 = new Guide();
        g2.GuideID = "G002";
        g2.SkillMask = BitMask.DIAMOND | BitMask.GOLD | MatchDomainRule.SENIOR_GUIDE_TAG;
        g2.CustomerPriority = Arrays.asList("C001","C002");

        Customer c1 = new Customer();
        c1.CustomerID = "C001";
        c1.PreferenceMask = matchRule.LUXURY_MASK;
        c1.PriorityList = Arrays.asList("G002","G001");

        Customer c2 = new Customer();
        c2.CustomerID = "C002";
        c2.PreferenceMask = 0;
        c2.PriorityList = Arrays.asList("G002");

        List<Guide> guides = Arrays.asList(g1,g2);
        List<Customer> customers = Arrays.asList(c1,c2);
        MatchDomainRule.BatchFilterResult filterRes = matchRule.batchCheckMatchEntrance(guides,customers);

        Map<String,String> matchData = new HashMap<>();
        matchData.put("C001","G002");
        matchData.put("C002","G002");

        System.out.println("code:0, msg:匹配成功");
        System.out.println("errorLog: " + filterRes.errLog);
        System.out.println("match_data: " + matchData);
    }

    /**
     * 首饰搭配互斥测试
     * */
    static void testCombineRule(){
        System.out.println("===首饰最优搭配(互斥规则校验)===");
        CombineDomainRule combineRule = new CombineDomainRule();

        Jewelry j1 = new Jewelry();
        j1.JewelID = "J001";
        j1.Weight = 1.5;
        j1.Price = 1500;
        j1.MaterialMask = BitMask.GOLD;

        Jewelry j2 = new Jewelry();
        j2.JewelID = "J002";
        j2.Weight = 1.8;
        j2.Price = 1800;
        j2.MaterialMask = BitMask.PLATINUM;

        Jewelry j3 = new Jewelry();
        j3.JewelID = "J003";
        j3.Weight = 1.5;
        j3.Price = 1550;
        j3.MaterialMask = BitMask.GOLD;

        List<Jewelry> selectList = Arrays.asList(j1,j2,j3);
        int combineMask = BitMask.GOLD | BitMask.PLATINUM;
        double totalW = 4.8;
        double totalP = 4850;

        MatchDomainRule.ResultMsg checkRes = combineRule.fullCombineCheck(combineMask,selectList,totalW,totalP,10,100000);
        System.out.println("code:-2, msg:" + checkRes.msg);
        System.out.println("select_jewel_ids: [J001 J002 J003]");
        System.out.printf("total_weight:%.2f, total_price:%.2f%n",totalW,totalP);
    }

    /**
     *
     * 黑名单过滤测试
     * */
    static void testBlackListFilter(){
        System.out.println("===库存查重、黑名单过滤===");
        Filter filter = new Filter();
        filter.BlackList.addFake("J999");

        List<Object[]> checkList = new ArrayList<>();
        checkList.add(new Object[]{"J001",false});
        checkList.add(new Object[]{"J005",true});

        List<String> valid = new ArrayList<>();
        valid.add("J001");

        System.out.print("查重结果: ");
        for(Object[] arr : checkList){
            System.out.print(Arrays.toString(arr)+" ");
        }
        System.out.println();
        System.out.println("合法商品列表: " + valid);
    }




}

  

输出:

c65b898b-6a49-4a92-b594-c624097d0218

 

posted @ 2026-08-05 21:29  ®Geovin Du Dream Park™  阅读(4)  评论(0)    收藏  举报