下面列出了com.google.common.collect.ArrayTable#create ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void givenArrayTable_whenGet_returnsSuccessfully() {
final List<String> universityRowTable = Lists.newArrayList("Mumbai", "Harvard");
final List<String> courseColumnTables = Lists.newArrayList("Chemical", "IT", "Electrical");
final Table<String, String, Integer> universityCourseSeatTable = ArrayTable.create(universityRowTable, courseColumnTables);
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final int seatCount = universityCourseSeatTable.get("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
}
private Wp41HistoData parseData(DataMiningFacadeParams dmParams) throws IOException, InterruptedException {
int rowCount = histoClient.queryCount(dmParams.getInterval(), HistoDbHorizon.SN);
Set<HistoDbAttributeId> attributeIds = new LinkedHashSet<>((dmParams.getGensIds().size() +
dmParams.getLoadsIds().size() +
dmParams.getDanglingLinesIds().size()) * 2); // gens P, Q loads P, Q danglingLines P0, Q0
for (String genId : dmParams.getGensIds()) {
attributeIds.add(new HistoDbNetworkAttributeId(genId, HistoDbAttr.P));
attributeIds.add(new HistoDbNetworkAttributeId(genId, HistoDbAttr.Q));
}
for (String loadId : dmParams.getLoadsIds()) {
attributeIds.add(new HistoDbNetworkAttributeId(loadId, HistoDbAttr.P));
attributeIds.add(new HistoDbNetworkAttributeId(loadId, HistoDbAttr.Q));
}
for (String dlId : dmParams.getDanglingLinesIds()) {
attributeIds.add(new HistoDbNetworkAttributeId(dlId, HistoDbAttr.P0));
attributeIds.add(new HistoDbNetworkAttributeId(dlId, HistoDbAttr.Q0));
}
List<Integer> rowIndexes;
try (IntStream intStream = IntStream.range(0, rowCount)) {
rowIndexes = intStream.boxed().collect(Collectors.toList());
}
List<String> colIndexes = attributeIds.stream().map(Object::toString).collect(Collectors.toList());
ArrayTable<Integer, String, Float> hdTable = ArrayTable.create(rowIndexes, colIndexes);
// parse csv generators
try (InputStream is = histoClient.queryCsv(HistoQueryType.data, attributeIds, dmParams.getInterval(), HistoDbHorizon.SN, false, false)) {
parseCsv(is, attributeIds, hdTable, rowCount);
}
return new Wp41HistoData(dmParams.getGensIds(), dmParams.getLoadsIds(), dmParams.getDanglingLinesIds(), hdTable);
}
private Table<String, ResourceType, Response<Map<String, String>>> getAllFromRedis(
Set<String> userIds) {
if (userIds.size() == 0) {
return HashBasedTable.create();
}
try {
final Table<String, ResourceType, Response<Map<String, String>>> responseTable =
ArrayTable.create(
userIds,
new ArrayIterator<>(
resources.stream().map(Resource::getResourceType).toArray(ResourceType[]::new)));
for (List<String> userIdSubset : Lists.partition(new ArrayList<>(userIds), 10)) {
redisClientDelegate.withMultiKeyPipeline(
p -> {
for (String userId : userIdSubset) {
resources.stream()
.map(Resource::getResourceType)
.forEach(
r -> {
responseTable.put(userId, r, p.hgetAll(userKey(userId, r)));
});
}
p.sync();
});
}
return responseTable;
} catch (Exception e) {
log.error("Storage exception reading all entries.", e);
}
return null;
}
@Test
public void getColumnTypes() {
ArrayTable<Integer, Integer, Result<?>> table = ArrayTable.create(INDICES, INDICES);
table.put(0, 0, Result.success(1));
table.put(0, 1, Result.success("abc"));
table.put(1, 0, Result.success(2));
table.put(1, 1, Result.success("def"));
List<Class<?>> columnTypes = TradeReportFormatter.INSTANCE.getColumnTypes(report(table));
assertThat(columnTypes).isEqualTo(ImmutableList.of(Integer.class, String.class));
}
@Test
public void getColumnTypesWithSomeFailures() {
ImmutableList<Integer> indices = ImmutableList.of(0, 1);
ArrayTable<Integer, Integer, Result<?>> table = ArrayTable.create(indices, indices);
table.put(0, 0, Result.failure(FailureReason.ERROR, "fail"));
table.put(0, 1, Result.failure(FailureReason.ERROR, "fail"));
table.put(1, 0, Result.success(2));
table.put(1, 1, Result.success("def"));
List<Class<?>> columnTypes = TradeReportFormatter.INSTANCE.getColumnTypes(report(table));
assertThat(columnTypes).isEqualTo(ImmutableList.of(Integer.class, String.class));
}
@Test
public void getColumnTypesWithAllFailures() {
ImmutableList<Integer> indices = ImmutableList.of(0, 1);
ArrayTable<Integer, Integer, Result<?>> table = ArrayTable.create(indices, indices);
table.put(0, 0, Result.failure(FailureReason.ERROR, "fail"));
table.put(0, 1, Result.failure(FailureReason.ERROR, "fail"));
table.put(1, 0, Result.failure(FailureReason.ERROR, "fail"));
table.put(1, 1, Result.failure(FailureReason.ERROR, "fail"));
List<Class<?>> columnTypes = TradeReportFormatter.INSTANCE.getColumnTypes(report(table));
assertThat(columnTypes).isEqualTo(ImmutableList.of(Object.class, Object.class));
}
static
InlineTable createInlineTable(List<List<String>> rows, List<String> columns){
List<Integer> rowKeys = new ArrayList<>();
for(int i = 0; i < rows.size(); i++){
rowKeys.add(i + 1);
}
Table<Integer, String, String> table = ArrayTable.create(rowKeys, columns);
for(int i = 0; i < rows.size(); i++){
List<String> row = rows.get(i);
for(int j = 0; j < columns.size(); j++){
String column = columns.get(j);
String value = row.get(j);
if(value == null){
continue;
}
table.put(rowKeys.get(i), column, value);
}
}
return InlineTableUtil.format(table);
}