RouteRuleUtils { public static <T extends Collection<String>> Map<String, RouteRule.MatchPair> expandCondition( Map<String, RouteRule.MatchPair> condition, String srcKeyName, String destKeyName, Map<String, T> expandName2Set) { if(null == condition || StringUtils.isEmpty(srcKeyName) || StringUtils.isEmpty(destKeyName)) { return condition; } RouteRule.MatchPair matchPair = condition.get(srcKeyName); if(matchPair == null) { return condition; } Map<String, RouteRule.MatchPair> ret = new HashMap<String, RouteRule.MatchPair>(); Iterator<Entry<String, RouteRule.MatchPair>> iterator = condition.entrySet().iterator(); for(; iterator.hasNext();) { Entry<String, RouteRule.MatchPair> entry = iterator.next(); String condName = entry.getKey(); if(!condName.equals(srcKeyName) && !condName.equals(destKeyName)) { RouteRule.MatchPair p = entry.getValue(); if(p != null) ret.put(condName, p); } else if(condName.equals(srcKeyName)) { RouteRule.MatchPair from = condition.get(srcKeyName); RouteRule.MatchPair to = condition.get(destKeyName); if(from == null || from.getMatches().isEmpty() && from.getUnmatches().isEmpty()) { if(to != null) ret.put(destKeyName, to); continue; } Set<String> matches = new HashSet<String>(); Set<String> unmatches = new HashSet<String>(); for(String s : from.getMatches()) { if(expandName2Set == null || !expandName2Set.containsKey(s)) continue; matches.addAll(expandName2Set.get(s)); } for(String s : from.getUnmatches()) { if(expandName2Set == null || !expandName2Set.containsKey(s)) continue; unmatches.addAll(expandName2Set.get(s)); } if(to != null) { matches.addAll(to.getMatches()); unmatches.addAll(to.getUnmatches()); } ret.put(destKeyName, new RouteRule.MatchPair(matches, unmatches)); } } return ret; } private  RouteRuleUtils(); static Map<String, RouteRule.MatchPair> expandCondition(
            Map<String, RouteRule.MatchPair> condition, String srcKeyName, String destKeyName,
            Map<String, T> expandName2Set); static boolean isMatchCondition(Map<String, RouteRule.MatchPair> condition,
            Map<String, String> valueParams, Map<String, String> kv); static Set<String> filterServiceByRule(List<String> services, RouteRule rule);  }
@Test public void test_expandCondition()throws Exception { Map<String, RouteRule.MatchPair> condition = new HashMap<String, RouteRule.MatchPair>(); { Set<String> matches = new HashSet<String>(); matches.add("value1_1"); matches.add("value1_2"); matches.add("value1_3"); Set<String> unmatches = new HashSet<String>(); unmatches.add("v1_1"); unmatches.add("v1_2"); unmatches.add("v1_3"); RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches); condition.put("key1", p); matches = new HashSet<String>(); matches.add("value2_1"); matches.add("value2_2"); matches.add("value2_3"); unmatches = new HashSet<String>(); unmatches.add("v2_1"); unmatches.add("v2_2"); unmatches.add("v2_3"); p = new RouteRule.MatchPair(matches, unmatches); condition.put("key2", p); matches = new HashSet<String>(); matches.add("value3_1"); matches.add("value3_2"); matches.add("value3_3"); unmatches = new HashSet<String>(); unmatches.add("v3_1"); unmatches.add("v3_2"); unmatches.add("v3_3"); p = new RouteRule.MatchPair(matches, unmatches); condition.put("key3 ", p); } Map<String, Set<String>> expandName2Set = new HashMap<String, Set<String>>(); { expandName2Set.put("value1_1", new HashSet<String>(Arrays.asList("cat1", "dog1"))); expandName2Set.put("value1_2", new HashSet<String>(Arrays.asList("cat2", "dog2"))); expandName2Set.put("value1_3", new HashSet<String>(Arrays.asList("cat3", "dog3"))); expandName2Set.put("v1_1", new HashSet<String>(Arrays.asList("pig1", "rat1"))); expandName2Set.put("v1_2", new HashSet<String>(Arrays.asList("pig2", "rat2"))); } Map<String, RouteRule.MatchPair> output = RouteRuleUtils.expandCondition(condition, "key1", "key2", expandName2Set); Map<String, RouteRule.MatchPair> expected = new HashMap<String, RouteRule.MatchPair>(); { Set<String> matches = new HashSet<String>(); matches.add("value2_1"); matches.add("value2_2"); matches.add("value2_3"); matches.add("cat1"); matches.add("cat2"); matches.add("cat3"); matches.add("dog1"); matches.add("dog2"); matches.add("dog3"); Set<String> unmatches = new HashSet<String>(); unmatches.add("v2_1"); unmatches.add("v2_2"); unmatches.add("v2_3"); unmatches.add("v2_3"); unmatches.add("pig1"); unmatches.add("pig2"); unmatches.add("rat1"); unmatches.add("rat2"); RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches); expected.put("key2", p); matches = new HashSet<String>(); matches.add("value3_1"); matches.add("value3_2"); matches.add("value3_3"); unmatches = new HashSet<String>(); unmatches.add("v3_1"); unmatches.add("v3_2"); unmatches.add("v3_3"); p = new RouteRule.MatchPair(matches, unmatches); expected.put("key3 ", p); } assertEquals(expected, output); output = RouteRuleUtils.expandCondition(condition, "key1", "keyX", expandName2Set); expected = new HashMap<String, RouteRule.MatchPair>(); { Set<String> matches = new HashSet<String>(); matches.add("cat1"); matches.add("cat2"); matches.add("cat3"); matches.add("dog1"); matches.add("dog2"); matches.add("dog3"); Set<String> unmatches = new HashSet<String>(); unmatches.add("pig1"); unmatches.add("pig2"); unmatches.add("rat1"); unmatches.add("rat2"); RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches); expected.put("keyX", p); matches = new HashSet<String>(); matches.add("value2_1"); matches.add("value2_2"); matches.add("value2_3"); unmatches = new HashSet<String>(); unmatches.add("v2_1"); unmatches.add("v2_2"); unmatches.add("v2_3"); p = new RouteRule.MatchPair(matches, unmatches); expected.put("key2", p); matches = new HashSet<String>(); matches.add("value3_1"); matches.add("value3_2"); matches.add("value3_3"); unmatches = new HashSet<String>(); unmatches.add("v3_1"); unmatches.add("v3_2"); unmatches.add("v3_3"); p = new RouteRule.MatchPair(matches, unmatches); expected.put("key3 ", p); } output = RouteRuleUtils.expandCondition(condition, "keyNotExsited", "key3", expandName2Set); assertSame(condition, output); }