标签:初始 collect main static nfa author group continue filter
public class CIConfig { //唯一id public int id; //条件类型 public ConditionType conditionType; //条件参数 public String param; public CIConfig(int id, ConditionType conditionType, String param) { this.id = id; this.conditionType = conditionType; this.param = param; } import java.util.HashMap; import java.util.Map; /** * 条件注册:模拟配置 */ public class ConditionConfig { /** * key=条件类型,Value=配置 */ public static Map<Integer, CIConfig> conditionConfigs = new HashMap<Integer, CIConfig>(); static { //玩家等级 put(new CIConfig(1, ConditionType.PLAYER_LEVEL, "20"), "玩家等級大于等于20级"); put(new CIConfig(2, ConditionType.PLAYER_LEVEL, "50"), "玩家等級大于等于50级"); put(new CIConfig(3, ConditionType.PLAYER_LEVEL, "80"), "玩家等級大于等于80级"); put(new CIConfig(4, ConditionType.PLAYER_LEVEL, "120"), "玩家等級大于等于120级"); //玩家vip等级 put(new CIConfig(5, ConditionType.PLAYER_VIP_LEVEL, "1"), "玩家vip等級大于等于1级"); put(new CIConfig(6, ConditionType.PLAYER_VIP_LEVEL, "5"), "玩家vip等級大于等于5级"); put(new CIConfig(7, ConditionType.PLAYER_VIP_LEVEL, "8"), "玩家vip等級大于等于8级"); put(new CIConfig(8, ConditionType.PLAYER_VIP_LEVEL, "10"), "玩家vip等級大于等于10级"); //玩家战力 put(new CIConfig(9, ConditionType.PLAYER_FOWER, "12000"), "玩家战力于等于12000"); put(new CIConfig(10, ConditionType.PLAYER_FOWER, "30000"), "玩家战力大于等于30000"); put(new CIConfig(11, ConditionType.PLAYER_FOWER, "80000"), "玩家战力大于等于80000"); put(new CIConfig(12, ConditionType.PLAYER_FOWER, "180000"), "玩家战力大于等于180000"); } public static void put(CIConfig iConfig, String des) { conditionConfigs.put(iConfig.id, iConfig); } public static CIConfig getiConfig(int id) { return conditionConfigs.get(id); } } /** * linxu */ public interface Condition { boolean check(ICondition iCondition); } /** * @author linxu * 判断玩家等级条件处理器 */ public class PlayerLevelCondition implements Condition { @Override public boolean check(ICondition iCondition) { CIConfig ciConfig = ConditionConfig.getiConfig(iCondition.getId()); if (ciConfig != null) { int level = Integer.parseInt(ciConfig.param); return level >= 100; } return false; } } /** * @author linxu * 判断玩家战力条件处理器 */ public class PlayerPowerCondtiton implements Condition { @Override public boolean check(ICondition iCondition) { CIConfig ciConfig = ConditionConfig.getiConfig(iCondition.getId()); if (ciConfig != null) { int power = Integer.parseInt(ciConfig.param); return power >= 50000; } return false; } } /** * @author linxu * 判断玩家vip等级条件处理器 */ public class PlayerVipLevelCondtiton implements Condition { @Override public boolean check(ICondition iCondition) { CIConfig ciConfig = ConditionConfig.getiConfig(iCondition.getId()); if (ciConfig != null) { int vipLevel = Integer.parseInt(ciConfig.param); return vipLevel >= 8; } return false; } } /** * linxu */ public enum ConditionType { PLAYER_LEVEL(1,"玩家等级条件判断:大于等于指定等级"), PLAYER_VIP_LEVEL(2,"玩家vip等级条件判断:大于等级指定vip等级"), PLAYER_FOWER(3,"玩家战斗力条件判断:大于等于指定战斗力") ; private int type; private String des; ConditionType(int type, String des){ this.type=type; this.des=des; } /** * 条件类型处理去初始化工厂 */ public class ConditionFactory { /** * 存储玩家条件处理器容器 */ public static Map<ConditionType, Condition> conditionMap = new HashMap<>(); static { registered(ConditionType.PLAYER_LEVEL, new PlayerLevelCondition(), "玩家等級條件處理器"); registered(ConditionType.PLAYER_VIP_LEVEL, new PlayerVipLevelCondtiton(), "玩家vip條件處理器"); registered(ConditionType.PLAYER_FOWER, new PlayerPowerCondtiton(), "玩家戰力條件處理器"); /** * 后续要增加,往后注册 */ } public static void registered(ConditionType conditionType, Condition condition, String des) { conditionMap.put(conditionType, condition); } public static Condition getCondition(ConditionType conditionType) { return conditionMap.get(conditionType); } } /** * 条件组 */ public class ConnditionGroup { /** * 当前组所属条件 */ public List<ICondition>iConditions=new ArrayList<>(); public List<ICondition> getiConditions() { return iConditions; } public void setiConditions(List<ICondition> iConditions) { this.iConditions = iConditions; } /** * 必须是所有条件都满足&【没有未达成的条件==条件符合】 * @return */ public boolean check(){ System.out.println(); return iConditions.stream().filter(s->!s.chcek()).collect(Collectors.toList()).size()<=0; } } public class ICondition { /** * 条件id */ private int id; /** * 所属条件类型 */ private ConditionType conditionType; public int getId() { return id; } public void setId(int id) { this.id = id; } public ConditionType getConditionType() { return conditionType; } public void setConditionType(ConditionType conditionType) { this.conditionType = conditionType; } /** * 检查条件 * @return */ public boolean chcek() { Condition condition = ConditionFactory.getCondition(this.getConditionType()); return condition == null ? false : condition.check(this); } } /** * linxu */ public class ConditionMgr { private static ConditionMgr conditionMgr = new ConditionMgr(); public static ConditionMgr getInstance() { return conditionMgr; } public boolean check(String value) { List<int[]> list = paserList(value); //条件组 List<ConnditionGroup> connditionGroups = new ArrayList<>(); for (int[] c : list) { ConnditionGroup connditionGroup = new ConnditionGroup(); for (int i : c) { //条件组里的的单个条件 CIConfig ciConfig = ConditionConfig.getiConfig(i); if (ciConfig == null) { continue; } ICondition iCondition = new ICondition(); iCondition.setId(i); iCondition.setConditionType(ciConfig.conditionType); connditionGroup.getiConditions().add(iCondition); } connditionGroups.add(connditionGroup); } //只要有一个符合,条件就满足 return connditionGroups.isEmpty() ? false : connditionGroups.stream().filter(s -> s.check()).collect(Collectors.toList()).size() > 0; } public List<int[]> paserList(String value) { String strVlaue = (String) value; List<int[]> list = new ArrayList<>(); if (!StringUtils.isEmpty(strVlaue)) { String[] allArray = strVlaue.split("\\|"); for (String s : allArray) { String[] typeArray = s.split("&"); list.add(StringUtil.strArrToIntArr(typeArray)); } } return list; } } public class ConditionTest { public static void main(String[] args) { /** * 注意&是且,|值得是或 */ //必须都满足 String con1="4&7";//玩家等級大于等于120级,且玩家vip等級大于等于8级 System.out.println(ConditionMgr.getInstance().check(con1)); //必须都满足 String con2="4&7&9";////玩家等級大于等于120级,且玩家vip等級大于等于8级,且玩家战力于等于12000 System.out.println(ConditionMgr.getInstance().check(con2)); //只要满足一个即可 String con3="1&7|11";////玩家等級大于等于20级,且玩家vip等級大于等于8级,或则玩家战力大于等于80000 System.out.println(ConditionMgr.getInstance().check(con3)); } } 测试结果: true false true Process finished with exit code 0
标签:初始 collect main static nfa author group continue filter
原文地址:https://www.cnblogs.com/mature1021/p/13373280.html