下面列出了com.google.common.collect.Maps#asMap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(YamlIntegratedTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
} else {
dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
}
}), yamlFile);
}
try (Connection conn = dataSource.getConnection();
Statement stm = conn.createStatement()) {
stm.executeQuery("SELECT * FROM t_order");
stm.executeQuery("SELECT * FROM t_order_item");
stm.executeQuery("SELECT * FROM config");
}
}
/**
* - see: http://code.google.com/p/guava-libraries/issues/detail?id=56
*/
@Test
public final void whenMapIsBackedBySetAndFunction_thenCorrect() {
final Function<Integer, Integer> powerOfTwo = new Function<Integer, Integer>() {
@Override
public final Integer apply(final Integer input) {
return (int) Math.pow(input, 2);
}
};
final Set<Integer> lowNumbers = Sets.newHashSet(2, 3, 4);
final Map<Integer, Integer> numberToPowerOfTwoMuttable = Maps.asMap(lowNumbers, powerOfTwo);
final Map<Integer, Integer> numberToPowerOfTwoImuttable = Maps.toMap(lowNumbers, powerOfTwo);
assertThat(numberToPowerOfTwoMuttable.get(2), equalTo(4));
assertThat(numberToPowerOfTwoImuttable.get(2), equalTo(4));
}
@Override protected Map<String, Table> getTableMap() {
if (!discoverTables) {
return ImmutableMap.of();
}
if (tableMap == null) {
final DruidConnectionImpl connection = new DruidConnectionImpl(url, coordinatorUrl);
Set<String> tableNames = connection.tableNames();
tableMap = Maps.asMap(
ImmutableSet.copyOf(tableNames),
CacheBuilder.newBuilder()
.build(CacheLoader.from(name -> table(name, connection))));
}
return tableMap;
}
@Test
public void assertWithDataSource() throws Exception {
File yamlFile = new File(YamlOrchestrationShardingWithMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = YamlOrchestrationShardingSphereDataSourceFactory.createDataSource(yamlFile);
} else {
Map<String, DataSource> dataSourceMap = Maps.asMap(Sets.newHashSet("db0_master", "db0_slave", "db1_master", "db1_slave"), AbstractYamlDataSourceTest::createDataSource);
Map<String, DataSource> result = new HashMap<>();
for (Entry<String, DataSource> each : dataSourceMap.entrySet()) {
result.put(each.getKey(), each.getValue());
}
dataSource = YamlOrchestrationShardingSphereDataSourceFactory.createDataSource(result, yamlFile);
}
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.execute(String.format("INSERT INTO t_order(user_id,status) values(%d, %s)", 10, "'insert'"));
statement.executeQuery("SELECT * FROM t_order");
statement.executeQuery("SELECT * FROM t_order_item");
statement.executeQuery("SELECT * FROM config");
}
((OrchestrationShardingSphereDataSource) dataSource).close();
}
@Override protected Map<String, Table> getTableMap() {
if (!discoverTables) {
return ImmutableMap.of();
}
if (tableMap == null) {
final DruidConnectionImpl connection = new DruidConnectionImpl(url, coordinatorUrl);
Set<String> tableNames = connection.tableNames();
tableMap = Maps.asMap(
ImmutableSet.copyOf(tableNames),
CacheBuilder.newBuilder()
.build(new CacheLoader<String, Table>() {
@Override public Table load(@Nonnull String tableName) throws Exception {
final Map<String, SqlTypeName> fieldMap = new LinkedHashMap<>();
final Set<String> metricNameSet = new LinkedHashSet<>();
final Map<String, List<ComplexMetric>> complexMetrics = new HashMap<>();
connection.metadata(tableName, DruidTable.DEFAULT_TIMESTAMP_COLUMN,
null, fieldMap, metricNameSet, complexMetrics);
return DruidTable.create(DruidSchema.this, tableName, null,
fieldMap, metricNameSet, DruidTable.DEFAULT_TIMESTAMP_COLUMN,
complexMetrics);
}
}));
}
return tableMap;
}
@Override
public Map<Key<ForeignKeyIndex<?>>, Optional<ForeignKeyIndex<?>>> loadAll(
Iterable<? extends Key<ForeignKeyIndex<?>>> keys) {
ImmutableSet<Key<ForeignKeyIndex<?>>> typedKeys = ImmutableSet.copyOf(keys);
Map<Key<ForeignKeyIndex<?>>, ForeignKeyIndex<?>> existingFkis =
tm().doTransactionless(() -> ofy().load().keys(typedKeys));
// ofy() omits keys that don't have values in Datastore, so re-add them in
// here with Optional.empty() values.
return Maps.asMap(
typedKeys,
(Key<ForeignKeyIndex<?>> key) ->
Optional.ofNullable(existingFkis.getOrDefault(key, null)));
}
@Override protected Map<String, Table> getTableMap() {
JsonCustomTable[] jsonCustomTables = new JsonCustomTable[tables.size()];
Set<String> tableNames = Arrays.stream(tables.toArray(jsonCustomTables))
.map(e -> e.name).collect(Collectors.toSet());
tableMap = Maps.asMap(
ImmutableSet.copyOf(tableNames),
CacheBuilder.newBuilder()
.build(CacheLoader.from(this::table)));
return tableMap;
}
/**
* Returns a map that is a live view of {@code graph}, with nodes as keys
* and the set of incident edges as values.
*/
private static <N, E> Map<N, Set<E>> nodeToIncidentEdges(final Network<N, E> graph) {
checkNotNull(graph, "graph");
return Maps.asMap(
graph.nodes(),
new Function<N, Set<E>>() {
@Override
public Set<E> apply(N node) {
return graph.incidentEdges(node);
}
});
}
private static <N> Map<N, Set<N>> nodeToAdjacentNodes(final Graph<N> graph) {
checkNotNull(graph, "graph");
return Maps.asMap(
graph.nodes(),
new Function<N, Set<N>>() {
@Override
public Set<N> apply(N node) {
return graph.adjacentNodes(node);
}
});
}
private static <N, E> NodeConnections<N, E> nodeConnectionsOf(Network<N, E> graph, N node) {
if (graph.isDirected()) {
Map<E, N> inEdgeMap = Maps.asMap(graph.inEdges(node), sourceNodeFn(graph));
Map<E, N> outEdgeMap = Maps.asMap(graph.outEdges(node), targetNodeFn(graph));
return graph.allowsParallelEdges()
? DirectedMultiNodeConnections.ofImmutable(inEdgeMap, outEdgeMap)
: DirectedNodeConnections.ofImmutable(inEdgeMap, outEdgeMap);
}
else {
Map<E, N> incidentEdgeMap = Maps.asMap(graph.incidentEdges(node), oppositeNodeFn(graph, node));
return graph.allowsParallelEdges()
? UndirectedMultiNodeConnections.ofImmutable(incidentEdgeMap)
: UndirectedNodeConnections.ofImmutable(incidentEdgeMap);
}
}
private static <N, E> NodeConnections<N, E> nodeConnectionsOf(Network<N, E> graph, N node) {
if (graph.isDirected()) {
Map<E, N> inEdgeMap = Maps.asMap(graph.inEdges(node), sourceNodeFn(graph));
Map<E, N> outEdgeMap = Maps.asMap(graph.outEdges(node), targetNodeFn(graph));
return graph.allowsParallelEdges()
? DirectedMultiNodeConnections.ofImmutable(inEdgeMap, outEdgeMap)
: DirectedNodeConnections.ofImmutable(inEdgeMap, outEdgeMap);
}
else {
Map<E, N> incidentEdgeMap = Maps.asMap(graph.incidentEdges(node), oppositeNodeFn(graph, node));
return graph.allowsParallelEdges()
? UndirectedMultiNodeConnections.ofImmutable(incidentEdgeMap)
: UndirectedNodeConnections.ofImmutable(incidentEdgeMap);
}
}
private static <N> Map<N, Set<N>> nodeToAdjacentNodes(final Graph<N> graph) {
checkNotNull(graph, "graph");
return Maps.asMap(
graph.nodes(),
new Function<N, Set<N>>() {
@Override
public Set<N> apply(N node) {
return graph.adjacentNodes(node);
}
});
}
private static <N, E> NodeConnections<N, E> nodeConnectionsOf(Network<N, E> graph, N node) {
if (graph.isDirected()) {
Map<E, N> inEdgeMap = Maps.asMap(graph.inEdges(node), sourceNodeFn(graph));
Map<E, N> outEdgeMap = Maps.asMap(graph.outEdges(node), targetNodeFn(graph));
return graph.allowsParallelEdges()
? DirectedMultiNodeConnections.ofImmutable(inEdgeMap, outEdgeMap)
: DirectedNodeConnections.ofImmutable(inEdgeMap, outEdgeMap);
}
else {
Map<E, N> incidentEdgeMap = Maps.asMap(graph.incidentEdges(node), oppositeNodeFn(graph, node));
return graph.allowsParallelEdges()
? UndirectedMultiNodeConnections.ofImmutable(incidentEdgeMap)
: UndirectedNodeConnections.ofImmutable(incidentEdgeMap);
}
}
/**
* Returns a map that is a live view of {@code graph}, with nodes as keys
* and the set of incident edges as values.
*/
private static <N, E> Map<N, Set<E>> nodeToIncidentEdges(final Network<N, E> graph) {
checkNotNull(graph, "graph");
return Maps.asMap(graph.nodes(), new Function<N, Set<E>>() {
@Override
public Set<E> apply(N node) {
return graph.incidentEdges(node);
}
});
}
private static <N> Map<N, Set<N>> nodeToAdjacentNodes(final Graph<N> graph) {
checkNotNull(graph, "graph");
return Maps.asMap(
graph.nodes(),
new Function<N, Set<N>>() {
@Override
public Set<N> apply(N node) {
return graph.adjacentNodes(node);
}
});
}
@Override
public Map<String, Boolean> getMailboxIds() {
return Maps.asMap(mailboxes, id -> true);
}
@Override
public Map<String, Boolean> getKeywords() {
return Maps.asMap(keywords, keyword -> true);
}
@Override
public Map<R, V> column(C columnKey) {
return Maps.asMap(Sets.filter(rowKeySet(), rowKey -> contains(rowKey, columnKey)),
rowKey -> map.get(rowKey).get(columnKey));
}
private ComposableFuture<Map<String, Set<String>>> createServicesMap() {
final Map<String, Set<String>> services = Maps.asMap(Sets.newHashSet(EXCLUDED_SERVICE, INCLUDED_MODULE), __ -> Collections.emptySet());
return ComposableFutures.fromValue(services);
}
@Override
public void serialize(final ImmutableSet value, final JsonGenerator jgen,
final SerializerProvider provider) throws IOException {
final Map map = (value == null) ? null : Maps.asMap(value, EMPTY_MAP);
OBJECT_MAPPER.writeValue(jgen, map);
}