Search in sources :

Example 1 with ImmutableTable

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();
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVRecord(org.apache.commons.csv.CSVRecord) ImmutableTable(com.google.common.collect.ImmutableTable)

Example 2 with ImmutableTable

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);
}
Also used : ImmutableTable(com.google.common.collect.ImmutableTable) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 3 with ImmutableTable

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);
}
Also used : ImmutableTable(com.google.common.collect.ImmutableTable) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ImmutableTable

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);
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) SampleEntryPointView(com.google.api.codegen.viewmodel.SampleEntryPointView) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) MethodModel(com.google.api.codegen.config.MethodModel) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Map(java.util.Map) StaticLangSampleClassView(com.google.api.codegen.viewmodel.StaticLangSampleClassView) StaticLangFileView(com.google.api.codegen.viewmodel.StaticLangFileView) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Streams(com.google.common.collect.Streams) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) List(java.util.List) Paths(java.nio.file.Paths) SampleSpec(com.google.api.codegen.config.SampleSpec) ImmutableTable(com.google.common.collect.ImmutableTable) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) ImmutableList(com.google.common.collect.ImmutableList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) ViewModel(com.google.api.codegen.viewmodel.ViewModel)

Example 5 with ImmutableTable

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())));
}
Also used : SampleValueSet(com.google.api.codegen.SampleValueSet) CallingForm(com.google.api.codegen.viewmodel.CallingForm) RequestFieldProto(com.google.api.codegen.samplegen.v1p2.RequestFieldProto) ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) Set(java.util.Set) HashBasedTable(com.google.common.collect.HashBasedTable) SampleConfigProto(com.google.api.codegen.samplegen.v1p2.SampleConfigProto) Collectors(java.util.stream.Collectors) SampleSpecProto(com.google.api.codegen.samplegen.v1p2.SampleSpecProto) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableTable(com.google.common.collect.ImmutableTable) Map(java.util.Map) AutoValue(com.google.auto.value.AutoValue) Preconditions(com.google.common.base.Preconditions) Table(com.google.common.collect.Table) Nullable(javax.annotation.Nullable) HashBasedTable(com.google.common.collect.HashBasedTable) ImmutableTable(com.google.common.collect.ImmutableTable) Table(com.google.common.collect.Table) ArrayList(java.util.ArrayList) SampleSpecProto(com.google.api.codegen.samplegen.v1p2.SampleSpecProto) HashSet(java.util.HashSet)

Aggregations

ImmutableTable (com.google.common.collect.ImmutableTable)7 ImmutableList (com.google.common.collect.ImmutableList)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)4 Test (org.junit.Test)4 CallingForm (com.google.api.codegen.viewmodel.CallingForm)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Nullable (javax.annotation.Nullable)2 SampleValueSet (com.google.api.codegen.SampleValueSet)1 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 InterfaceContext (com.google.api.codegen.config.InterfaceContext)1 MethodContext (com.google.api.codegen.config.MethodContext)1 MethodModel (com.google.api.codegen.config.MethodModel)1 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)1 SampleConfig (com.google.api.codegen.config.SampleConfig)1 SampleContext (com.google.api.codegen.config.SampleContext)1 SampleSpec (com.google.api.codegen.config.SampleSpec)1 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)1