1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
52
53
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
61
62
63
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
71
72
73
74
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 }