1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.yaml.config.swapper.rule;
19
20 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
21 import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
22 import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.LinkedList;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
29
30
31
32
33 public final class YamlRuleConfigurationSwapperEngine {
34
35
36
37
38
39
40
41 @SuppressWarnings({"unchecked", "rawtypes"})
42 public YamlRuleConfiguration swapToYamlRuleConfiguration(final RuleConfiguration ruleConfig) {
43 YamlRuleConfigurationSwapper yamlSwapper = OrderedSPILoader.getServices(YamlRuleConfigurationSwapper.class, Collections.singleton(ruleConfig)).get(ruleConfig);
44 return (YamlRuleConfiguration) yamlSwapper.swapToYamlConfiguration(ruleConfig);
45 }
46
47
48
49
50
51
52
53 @SuppressWarnings("unchecked")
54 public Collection<YamlRuleConfiguration> swapToYamlRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
55 return OrderedSPILoader.getServices(YamlRuleConfigurationSwapper.class, ruleConfigs).entrySet().stream()
56 .map(entry -> (YamlRuleConfiguration) entry.getValue().swapToYamlConfiguration(entry.getKey())).collect(Collectors.toList());
57 }
58
59
60
61
62
63
64
65 @SuppressWarnings({"unchecked", "rawtypes"})
66 public RuleConfiguration swapToRuleConfiguration(final YamlRuleConfiguration yamlRuleConfig) {
67 Class<?> ruleConfigType = yamlRuleConfig.getRuleConfigurationType();
68 YamlRuleConfigurationSwapper swapper = OrderedSPILoader.getServicesByClass(YamlRuleConfigurationSwapper.class, Collections.singleton(ruleConfigType)).get(ruleConfigType);
69 return (RuleConfiguration) swapper.swapToObject(yamlRuleConfig);
70 }
71
72
73
74
75
76
77
78 @SuppressWarnings("rawtypes")
79 public Collection<RuleConfiguration> swapToRuleConfigurations(final Collection<YamlRuleConfiguration> yamlRuleConfigs) {
80 Collection<RuleConfiguration> result = new LinkedList<>();
81 Collection<Class<?>> ruleConfigTypes = yamlRuleConfigs.stream().map(YamlRuleConfiguration::getRuleConfigurationType).collect(Collectors.toList());
82 for (Entry<Class<?>, YamlRuleConfigurationSwapper> entry : OrderedSPILoader.getServicesByClass(YamlRuleConfigurationSwapper.class, ruleConfigTypes).entrySet()) {
83 result.addAll(swapToRuleConfigurations(yamlRuleConfigs, entry.getKey(), entry.getValue()));
84 }
85 return result;
86 }
87
88 @SuppressWarnings({"unchecked", "rawtypes"})
89 private Collection<RuleConfiguration> swapToRuleConfigurations(final Collection<YamlRuleConfiguration> yamlRuleConfigs,
90 final Class<?> ruleConfigType, final YamlRuleConfigurationSwapper swapper) {
91 return yamlRuleConfigs.stream()
92 .filter(each -> each.getRuleConfigurationType().equals(ruleConfigType)).map(each -> (RuleConfiguration) swapper.swapToObject(each)).collect(Collectors.toList());
93 }
94 }