use of com.google.common.collect.ImmutableTable in project opennms by OpenNMS.
the class RScriptExecutor method fromCsv.
/**
* Convert the CSV string to an immutable table.
*/
protected static ImmutableTable<Long, String, Double> fromCsv(final String csv) throws IOException {
ImmutableTable.Builder<Long, String, Double> builder = ImmutableTable.builder();
try (StringReader reader = new StringReader(csv);
CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180.withHeader())) {
long rowIndex = 0;
Map<String, Integer> headerMap = parser.getHeaderMap();
for (CSVRecord record : parser) {
for (String key : headerMap.keySet()) {
Double value;
try {
value = Double.valueOf(record.get(key));
} catch (NumberFormatException e) {
value = Double.NaN;
}
builder.put(rowIndex, key, value);
}
rowIndex++;
}
}
return builder.build();
}
use of com.google.common.collect.ImmutableTable in project querydsl by querydsl.
the class GroupBy4Test method test5.
@Test
public void test5() {
List<Table> data = Lists.newArrayList();
data.add(new Table("1", "abc", "111"));
data.add(new Table("1", "pqr", "222"));
data.add(new Table("2", "abc", "333"));
data.add(new Table("2", "pqr", "444"));
data.add(new Table("3", "abc", "555"));
data.add(new Table("3", "pqr", "666"));
data.add(new Table("3", "pqr", "777"));
QGroupBy4Test_Table table = QGroupBy4Test_Table.table;
com.google.common.collect.Table<String, String, List<String>> transform = CollQueryFactory.from(table, data).transform(groupBy(table.col1).asTable(table.col2, list(table.col3)));
ImmutableTable<String, String, List<String>> expected = ImmutableTable.<String, String, List<String>>builder().put("1", "abc", ImmutableList.of("111")).put("1", "pqr", ImmutableList.of("222")).put("2", "abc", ImmutableList.of("333")).put("2", "pqr", ImmutableList.of("444")).put("3", "abc", ImmutableList.of("555")).put("3", "pqr", ImmutableList.of("666", "777")).build();
assertEquals(expected, transform);
}
use of com.google.common.collect.ImmutableTable in project querydsl by querydsl.
the class GroupBy4Test method test8.
@Test
public void test8() {
List<Table> data = Lists.newArrayList();
data.add(new Table("1", "abc", "111"));
data.add(new Table("1", "pqr", "222"));
data.add(new Table("2", "abc", "333"));
data.add(new Table("2", "pqr", "444"));
data.add(new Table("3", "abc", "555"));
data.add(new Table("3", "pqr", "666"));
data.add(new Table("3", "pqr", "777"));
QGroupBy4Test_Table table = QGroupBy4Test_Table.table;
com.google.common.collect.Table<String, Map<String, String>, List<String>> transform = CollQueryFactory.from(table, data).transform(groupBy(table.col1).asTable(map(table.col1, table.col2), GuavaGroupBy.list(table.col3)));
ImmutableTable<String, Map<String, String>, List<String>> expected = ImmutableTable.<String, Map<String, String>, List<String>>builder().put("1", ImmutableMap.of("1", "abc"), ImmutableList.of("111")).put("1", ImmutableMap.of("1", "pqr"), ImmutableList.of("222")).put("2", ImmutableMap.of("2", "abc"), ImmutableList.of("333")).put("2", ImmutableMap.of("2", "pqr"), ImmutableList.of("444")).put("3", ImmutableMap.of("3", "abc"), ImmutableList.of("555")).put("3", ImmutableMap.of("3", "pqr"), ImmutableList.of("666", "777")).build();
assertEquals(expected, transform);
}
use of com.google.common.collect.ImmutableTable in project toolkit by googleapis.
the class StaticLangGapicSamplesTransformer method transform.
@Override
public List<ViewModel> transform(ProtoApiModel apiModel, GapicProductConfig productConfig) {
String packageName = productConfig.getPackageName();
SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
ModelTypeTable typeTable = newTypeTable.apply(packageName);
FeatureConfig featureConfig = newFeatureConfig.apply(productConfig);
List<InterfaceContext> interfaceContexts = Streams.stream(apiModel.getInterfaces(productConfig)).filter(iface -> productConfig.hasInterfaceConfig(iface)).map(iface -> GapicInterfaceContext.create(iface, productConfig, typeTable, namer, featureConfig)).collect(ImmutableList.toImmutableList());
ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
List<ViewModel> sampleFiles;
if (sampleConfigTable.isEmpty()) {
// We don't have sample configs. Continue to use gapic config.
sampleFiles = generateSamplesFromGapicConfigs(interfaceContexts, productConfig, namer);
} else {
// Generate samples using sample configs.
sampleFiles = generateSamplesFromSampleConfigs(interfaceContexts, productConfig);
}
if (!interfaceContexts.isEmpty() && !sampleFiles.isEmpty()) {
ViewModel entryPoint = generateSampleEntryPoint(interfaceContexts.get(0));
if (entryPoint != null) {
sampleFiles.add(entryPoint);
}
}
return ImmutableList.copyOf(sampleFiles);
}
use of com.google.common.collect.ImmutableTable in project toolkit by googleapis.
the class SampleConfig method createSampleConfigTable.
public static ImmutableTable<String, String, ImmutableList<SampleConfig>> createSampleConfigTable(@Nullable SampleConfigProto sampleConfigProto, final Map<String, InterfaceConfig> interfaceConfigMap) {
if (sampleConfigProto == null) {
sampleConfigProto = SampleConfigProto.getDefaultInstance();
}
// First, apply region tag as IDs if IDs are not given
List<SampleSpecProto> sampleSpecs = new ArrayList<>();
for (SampleSpecProto spec : sampleConfigProto.getSamplesList()) {
if (spec.getId().isEmpty()) {
spec = spec.toBuilder().setId(spec.getRegionTag()).build();
}
sampleSpecs.add(spec);
}
// Then, check user specified sample IDs do not clash
Set<String> distinctIds = new HashSet<>();
Set<String> duplicateIds = sampleSpecs.stream().map(s -> s.getId()).filter(id -> !id.isEmpty()).filter(s -> !distinctIds.add(s)).collect(Collectors.toSet());
Preconditions.checkArgument(duplicateIds.isEmpty(), "Found duplicate IDs: %s", duplicateIds.stream().collect(Collectors.joining(", ")));
// Next, flatten the calling pattern list so we have one per sample
// Note these are not the final calling pattern values, because the
// regexes specified in the config need to be matched against
// language-specific calling pattern definitions.
List<SampleSpecProto> flattenedSampleSpecs = new ArrayList<>();
for (SampleSpecProto spec : sampleSpecs) {
if (spec.getCallingPatternsList().isEmpty()) {
flattenedSampleSpecs.add(spec.toBuilder().addCallingPatterns(DEFAULT_CALLING_PATTERN).build());
}
for (String pattern : spec.getCallingPatternsList()) {
flattenedSampleSpecs.add(spec.toBuilder().clearCallingPatterns().addCallingPatterns(pattern).build());
}
}
// Construct the table.
HashBasedTable<String, String, ArrayList<SampleConfig>> table = HashBasedTable.create();
for (SampleSpecProto sampleSpec : flattenedSampleSpecs) {
SampleConfig config = createOneSampleConfig(sampleSpec, interfaceConfigMap);
if (!table.contains(sampleSpec.getService(), sampleSpec.getRpc())) {
table.put(sampleSpec.getService(), sampleSpec.getRpc(), new ArrayList<>());
}
table.get(sampleSpec.getService(), sampleSpec.getRpc()).add(config);
}
// Make an immutable copy.
return table.cellSet().stream().collect(ImmutableTable.toImmutableTable(Table.Cell::getRowKey, Table.Cell::getColumnKey, v -> ImmutableList.copyOf(v.getValue())));
}
Aggregations