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.route.context;
19  
20  import lombok.EqualsAndHashCode;
21  import lombok.Getter;
22  import lombok.ToString;
23  import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
24  
25  import java.util.Collection;
26  import java.util.HashSet;
27  import java.util.Optional;
28  import java.util.Set;
29  import java.util.stream.Collectors;
30  
31  /**
32   * Route unit.
33   */
34  @Getter
35  @EqualsAndHashCode
36  @ToString
37  public final class RouteUnit {
38      
39      private final RouteMapper dataSourceMapper;
40      
41      private final Collection<RouteMapper> tableMappers;
42      
43      public RouteUnit(final RouteMapper dataSourceMapper, final Collection<RouteMapper> tableMappers) {
44          ShardingSpherePreconditions.checkNotNull(dataSourceMapper, () -> new IllegalArgumentException("`dataSourceMapper` is required"));
45          ShardingSpherePreconditions.checkNotNull(tableMappers, () -> new IllegalArgumentException("`tableMappers` is required"));
46          this.dataSourceMapper = dataSourceMapper;
47          this.tableMappers = tableMappers;
48      }
49      
50      /**
51       * Get logic table names.
52       *
53       * @return logic table names
54       */
55      public Set<String> getLogicTableNames() {
56          return tableMappers.stream().map(RouteMapper::getLogicName).collect(Collectors.toCollection(() -> new HashSet<>(tableMappers.size(), 1L)));
57      }
58      
59      /**
60       * Get actual table names.
61       *
62       * @param logicTableName logic table name
63       * @return actual table names
64       */
65      public Set<String> getActualTableNames(final String logicTableName) {
66          return tableMappers.stream().filter(each -> logicTableName.equalsIgnoreCase(each.getLogicName())).map(RouteMapper::getActualName).collect(Collectors.toSet());
67      }
68      
69      /**
70       * Find table mapper.
71       *
72       * @param logicDataSourceName logic data source name
73       * @param actualTableName actual table name
74       * @return table mapper
75       */
76      public Optional<RouteMapper> findTableMapper(final String logicDataSourceName, final String actualTableName) {
77          for (RouteMapper each : tableMappers) {
78              if (logicDataSourceName.equalsIgnoreCase(dataSourceMapper.getLogicName()) && actualTableName.equalsIgnoreCase(each.getActualName())) {
79                  return Optional.of(each);
80              }
81          }
82          return Optional.empty();
83      }
84  }