View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * YAML rule configuration swapper engine.
32   */
33  public final class YamlRuleConfigurationSwapperEngine {
34      
35      /**
36       * Swap to YAML rule configuration.
37       *
38       * @param ruleConfig rule configuration
39       * @return YAML rule configuration
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       * Swap to YAML rule configurations.
49       *
50       * @param ruleConfigs rule configurations
51       * @return YAML rule configurations
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       * Swap from YAML rule configurations to rule configuration.
61       *
62       * @param yamlRuleConfig YAML rule configuration
63       * @return rule configuration
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       * Swap from YAML rule configurations to rule configurations.
74       *
75       * @param yamlRuleConfigs YAML rule configurations
76       * @return rule configurations
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  }