Java Code Examples for com.google.common.collect.Maps#asMap()

The following examples show how to use com.google.common.collect.Maps#asMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuavaFunctionalExamplesUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
/**
 * - 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));
}
 
Example 2
Source File: DruidSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: YamlIntegratedTest.java    From sharding-jdbc-1.5.1 with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example 4
Source File: YamlOrchestrationShardingWithMasterSlaveIntegrateTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 5
Source File: RedisSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 6
Source File: Graphs.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
  });
}
 
Example 7
Source File: Graphs.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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);
    }
  });
}
 
Example 8
Source File: ImmutableNetwork.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
  }
 
Example 9
Source File: Graphs.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
  });
}
 
Example 10
Source File: ImmutableNetwork.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
  }
 
Example 11
Source File: ImmutableNetwork.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
  }
 
Example 12
Source File: Graphs.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
  });
}
 
Example 13
Source File: Graphs.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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);
    }
  });
}
 
Example 14
Source File: ForeignKeyIndex.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example 15
Source File: DruidSchema.java    From Quicksql with MIT License 5 votes vote down vote up
@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;
}
 
Example 16
Source File: ObjectMapperProvider.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 17
Source File: ConsulTargetsCacheTest.java    From GomJabbar with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 18
Source File: TableView.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Map<R, V> column(C columnKey) {
    return Maps.asMap(Sets.filter(rowKeySet(), rowKey -> contains(rowKey, columnKey)),
                      rowKey -> map.get(rowKey).get(columnKey));
}
 
Example 19
Source File: EmailWithKeywords.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Boolean> getKeywords() {
    return Maps.asMap(keywords, keyword -> true);
}
 
Example 20
Source File: ThreadOverviewItem.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Boolean> getMailboxIds() {
    return Maps.asMap(mailboxes, id -> true);
}