Example usage for com.google.common.collect ImmutableTable builder

List of usage examples for com.google.common.collect ImmutableTable builder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableTable builder.

Prototype

public static <R, C, V> Builder<R, C, V> builder() 

Source Link

Document

Returns a new builder.

Usage

From source file:com.opengamma.strata.report.cashflow.CashFlowReportRunner.java

private ImmutableTable<Integer, Integer, Object> getData(List<ExplainMap> flatMap, List<ExplainKey<?>> keys) {
    ImmutableTable.Builder<Integer, Integer, Object> builder = ImmutableTable.builder();

    for (int rowIdx = 0; rowIdx < flatMap.size(); rowIdx++) {
        ExplainMap rowMap = flatMap.get(rowIdx);

        for (int colIdx = 0; colIdx < keys.size(); colIdx++) {
            builder.put(rowIdx, colIdx, rowMap.get(keys.get(colIdx)));
        }//from   www  .j  ava 2s  .c  o  m
    }
    return builder.build();
}

From source file:me.yanaga.guava.stream.MoreCollectors.java

public static <T, R, C, V> Collector<T, ?, ImmutableTable<R, C, V>> toImmutableSortedTable(
        Function<? super T, ? extends R> rowMapper, Function<? super T, ? extends C> columnMapper,
        Function<? super T, ? extends V> valueMapper, Comparator<R> rowComparator,
        Comparator<C> columnComparator) {
    return Collector.of(new Supplier<ImmutableTable.Builder<R, C, V>>() {
        @Override//ww  w  .  ja  va2  s  .com
        public ImmutableTable.Builder<R, C, V> get() {
            return ImmutableTable.<R, C, V>builder().orderRowsBy(rowComparator)
                    .orderColumnsBy(columnComparator);
        }
    }, new BiConsumer<ImmutableTable.Builder<R, C, V>, T>() {
        @Override
        public void accept(ImmutableTable.Builder<R, C, V> rcvBuilder, T t) {
            rcvBuilder.put(rowMapper.apply(t), columnMapper.apply(t), valueMapper.apply(t));
        }
    }, new BinaryOperator<ImmutableTable.Builder<R, C, V>>() {
        @Override
        public ImmutableTable.Builder<R, C, V> apply(ImmutableTable.Builder<R, C, V> rcvBuilder,
                ImmutableTable.Builder<R, C, V> rcvBuilder2) {
            return rcvBuilder.putAll(rcvBuilder2.build());
        }
    }, new Function<ImmutableTable.Builder<R, C, V>, ImmutableTable<R, C, V>>() {
        @Override
        public ImmutableTable<R, C, V> apply(ImmutableTable.Builder<R, C, V> rcvBuilder) {
            return rcvBuilder.build();
        }
    }, CONCURRENT);
}

From source file:org.apache.beam.runners.fnexecution.control.ProcessBundleDescriptors.java

private static Map<String, Map<String, BagUserStateSpec>> forBagUserStates(ExecutableStage stage,
        Components components) throws IOException {
    ImmutableTable.Builder<String, String, BagUserStateSpec> idsToSpec = ImmutableTable.builder();
    for (UserStateReference userStateReference : stage.getUserStates()) {
        FullWindowedValueCoder<KV<?, ?>> coder = (FullWindowedValueCoder) WireCoders
                .instantiateRunnerWireCoder(userStateReference.collection(), components);
        idsToSpec.put(userStateReference.transform().getId(), userStateReference.localName(),
                BagUserStateSpec.of(userStateReference.transform().getId(), userStateReference.localName(),
                        // We use the ByteString coder to save on encoding and decoding the actual key.
                        ByteStringCoder.of(),
                        // Usage of the ByteStringCoder provides a significant simplification for handling
                        // a logical stream of values by not needing to know where the element boundaries
                        // actually are. See StateRequestHandlers.java for further details.
                        ByteStringCoder.of(), coder.getWindowCoder()));
    }//from w  ww. j av  a 2 s.  co  m
    return idsToSpec.build().rowMap();
}

From source file:edu.mit.streamjit.impl.interp.Interpreter.java

@Override
public DrainData getDrainData() {
    ImmutableMap.Builder<Token, ImmutableList<Object>> dataBuilder = ImmutableMap.builder();
    for (IOInfo info : IOInfo.allEdges(workers))
        dataBuilder.put(info.token(), ImmutableList.copyOf(info.channel()));

    ImmutableTable.Builder<Integer, String, Object> stateBuilder = ImmutableTable.builder();
    for (Worker<?, ?> worker : workers) {
        if (!(worker instanceof StatefulFilter))
            continue;
        int id = Workers.getIdentifier(worker);
        for (Class<?> klass = worker.getClass(); !klass.equals(StatefulFilter.class); klass = klass
                .getSuperclass()) {//from   ww  w . ja v a  2 s . c o m
            for (Field f : klass.getDeclaredFields()) {
                if ((f.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) != 0)
                    continue;
                f.setAccessible(true);
                try {
                    stateBuilder.put(id, f.getName(), f.get(worker));
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                    throw new AssertionError(ex);
                }
            }
        }
    }

    return new DrainData(dataBuilder.build(), stateBuilder.build());
}

From source file:org.apache.beam.runners.fnexecution.control.ProcessBundleDescriptors.java

private static Map<String, Map<String, TimerSpec>> forTimerSpecs(ApiServiceDescriptor dataEndpoint,
        ExecutableStage stage, Components.Builder components,
        ImmutableMap.Builder<String, RemoteInputDestination<WindowedValue<?>>> remoteInputsBuilder,
        ImmutableMap.Builder<Target, Coder<WindowedValue<?>>> outputTargetCodersBuilder) throws IOException {
    ImmutableTable.Builder<String, String, TimerSpec> idsToSpec = ImmutableTable.builder();
    for (TimerReference timerReference : stage.getTimers()) {
        RunnerApi.ParDoPayload payload = RunnerApi.ParDoPayload
                .parseFrom(timerReference.transform().getTransform().getSpec().getPayload());
        RunnerApi.TimeDomain.Enum timeDomain = payload.getTimerSpecsOrThrow(timerReference.localName())
                .getTimeDomain();/*from  ww  w .  ja v a 2s  .c  o m*/
        org.apache.beam.sdk.state.TimerSpec spec;
        switch (timeDomain) {
        case EVENT_TIME:
            spec = TimerSpecs.timer(TimeDomain.EVENT_TIME);
            break;
        case PROCESSING_TIME:
            spec = TimerSpecs.timer(TimeDomain.PROCESSING_TIME);
            break;
        case SYNCHRONIZED_PROCESSING_TIME:
            spec = TimerSpecs.timer(TimeDomain.SYNCHRONIZED_PROCESSING_TIME);
            break;
        default:
            throw new IllegalArgumentException(String.format("Unknown time domain %s", timeDomain));
        }

        String mainInputName = timerReference.transform().getTransform()
                .getInputsOrThrow(Iterables.getOnlyElement(Sets.difference(
                        timerReference.transform().getTransform().getInputsMap().keySet(),
                        Sets.union(payload.getSideInputsMap().keySet(), payload.getTimerSpecsMap().keySet()))));
        String timerCoderId = keyValueCoderId(
                components.getCodersOrThrow(components.getPcollectionsOrThrow(mainInputName).getCoderId())
                        .getComponentCoderIds(0),
                payload.getTimerSpecsOrThrow(timerReference.localName()).getTimerCoderId(), components);
        RunnerApi.PCollection timerCollectionSpec = components.getPcollectionsOrThrow(mainInputName).toBuilder()
                .setCoderId(timerCoderId).build();

        // "Unroll" the timers into PCollections.
        String inputTimerPCollectionId = SyntheticComponents.uniqueId(
                String.format("%s.timer.%s.in", timerReference.transform().getId(), timerReference.localName()),
                components.getPcollectionsMap()::containsKey);
        components.putPcollections(inputTimerPCollectionId, timerCollectionSpec);
        remoteInputsBuilder.put(inputTimerPCollectionId, addStageInput(dataEndpoint,
                PipelineNode.pCollection(inputTimerPCollectionId, timerCollectionSpec), components));
        String outputTimerPCollectionId = SyntheticComponents.uniqueId(String.format("%s.timer.%s.out",
                timerReference.transform().getId(), timerReference.localName()),
                components.getPcollectionsMap()::containsKey);
        components.putPcollections(outputTimerPCollectionId, timerCollectionSpec);
        TargetEncoding targetEncoding = addStageOutput(dataEndpoint, components,
                PipelineNode.pCollection(outputTimerPCollectionId, timerCollectionSpec));
        outputTargetCodersBuilder.put(targetEncoding.getTarget(), targetEncoding.getCoder());
        components.putTransforms(timerReference.transform().getId(),
                // Since a transform can have more then one timer, update the transform inside components and not the original
                components.getTransformsOrThrow(timerReference.transform().getId()).toBuilder()
                        .putInputs(timerReference.localName(), inputTimerPCollectionId)
                        .putOutputs(timerReference.localName(), outputTimerPCollectionId).build());

        idsToSpec.put(timerReference.transform().getId(), timerReference.localName(),
                TimerSpec.of(timerReference.transform().getId(), timerReference.localName(),
                        inputTimerPCollectionId, outputTimerPCollectionId, targetEncoding.getTarget(), spec));
    }
    return idsToSpec.build().rowMap();
}

From source file:co.turnus.analysis.bottlenecks.util.HotspotsDataAnalyser.java

@SuppressWarnings("unchecked")
public <T> Table<T, Action, ExtendExecData> getSumDataTable(Class<T> type, Key key, Order order) {
    if (type.isAssignableFrom(Actor.class)) {
        // create a temporary table
        Table<Actor, Action, ExtendExecData> table = HashBasedTable.create();
        for (ActionHotspotsData aData : data.getActionsData()) {
            table.put(aData.getActor(), aData.getAction(), aData);
        }//from   www .j a v  a 2 s  . c o m

        // sort the table
        tableComparator.setSorting(key, order);
        ImmutableTable.Builder<Actor, Action, ExtendExecData> sortedBuilder = ImmutableTable.builder(); // preserves insertion order
        for (Table.Cell<Actor, Action, ExtendExecData> cell : tableComparator.sortedCopy(table.cellSet())) {
            sortedBuilder.put(cell);
        }

        // return a sorted table
        return (Table<T, Action, ExtendExecData>) sortedBuilder.build();

    } else if (type.isAssignableFrom(ActorClass.class)) {
        // create a temporary table
        Table<ActorClass, Action, ExtendExecData> table = HashBasedTable.create();
        for (ActionHotspotsData aData : data.getActionsData()) {
            ActorClass clazz = aData.getActor().getActorClass();
            Action action = aData.getAction();
            ExtendExecData actionData = table.get(clazz, action);
            if (actionData == null) {
                actionData = aData;
                table.put(clazz, action, actionData);
            } else {
                DataUtil.merge(actionData, aData);
            }

        }

        // sort the table
        tableComparator.setSorting(key, order);
        ImmutableTable.Builder<ActorClass, Action, ExtendExecData> sortedBuilder = ImmutableTable.builder(); // preserves insertion order
        for (Table.Cell<ActorClass, Action, ExtendExecData> cell : tableComparator
                .sortedCopy(table.cellSet())) {
            sortedBuilder.put(cell);
        }

        // return a sorted table
        return (Table<T, Action, ExtendExecData>) sortedBuilder.build();
    }
    return null;
}

From source file:google.registry.bigquery.BigqueryConnection.java

/**
 * Returns the query results for the given job as an ImmutableTable, row-keyed by row number
 * (indexed from 1), column-keyed by the TableFieldSchema for that field, and with the value
 * object as the cell value.  Note that null values will not actually be null (since we're using
 * ImmutableTable) but they can be checked for using Data.isNull().
 *
 * <p>This table is fully materialized in memory (not lazily loaded), so it should not be used
 * with queries expected to return large results.
 *///ww w  . j  a  v  a2  s.  c o  m
private ImmutableTable<Integer, TableFieldSchema, Object> getQueryResults(Job job) {
    try {
        ImmutableTable.Builder<Integer, TableFieldSchema, Object> builder = new ImmutableTable.Builder<>();
        String pageToken = null;
        int rowNumber = 1;
        while (true) {
            GetQueryResultsResponse queryResults = bigquery.jobs()
                    .getQueryResults(getProjectId(), job.getJobReference().getJobId()).setPageToken(pageToken)
                    .execute();
            // If the job isn't complete yet, retry; getQueryResults() waits for up to 10 seconds on
            // each invocation so this will effectively poll for completion.
            if (queryResults.getJobComplete()) {
                List<TableFieldSchema> schemaFields = queryResults.getSchema().getFields();
                for (TableRow row : queryResults.getRows()) {
                    Iterator<TableFieldSchema> fieldIterator = schemaFields.iterator();
                    Iterator<TableCell> cellIterator = row.getF().iterator();
                    while (fieldIterator.hasNext() && cellIterator.hasNext()) {
                        builder.put(rowNumber, fieldIterator.next(), cellIterator.next().getV());
                    }
                    rowNumber++;
                }
                pageToken = queryResults.getPageToken();
                if (pageToken == null) {
                    break;
                }
            }
        }
        return builder.build();
    } catch (IOException e) {
        throw BigqueryJobFailureException.create(e);
    }
}

From source file:co.turnus.profiling.util.ProfilingDataAnalyser.java

@SuppressWarnings("unchecked")
public <T> Table<T, Action, ComData> getSortedComData(Class<T> type, Key key, Order order) {
    if (type.isAssignableFrom(Actor.class)) {
        // create a temporary table
        Table<Actor, Action, ComData> table = HashBasedTable.create();
        for (Cell<Actor, Action, ActionProfilingData> cell : data.asTable().cellSet()) {
            table.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
        }/*from ww w  . j a  v  a 2  s . c o  m*/

        // sort the table
        tableComComparator.setSorting(key, order);
        ImmutableTable.Builder<Actor, Action, ComData> sortedBuilder = ImmutableTable.builder(); // preserves insertion order
        for (Table.Cell<Actor, Action, ComData> cell : tableComComparator.sortedCopy(table.cellSet())) {
            sortedBuilder.put(cell);
        }

        // return a sorted table
        return (Table<T, Action, ComData>) sortedBuilder.build();
    } else if (type.isAssignableFrom(ActorClass.class)) {
        // create a temporary table
        Table<ActorClass, Action, ComData> table = HashBasedTable.create();
        for (Cell<Actor, Action, ActionProfilingData> cell : data.asTable().cellSet()) {
            ActorClass clazz = cell.getRowKey().getActorClass();
            Action action = cell.getColumnKey();
            ComData actionData = table.get(clazz, action);
            if (actionData == null) {
                actionData = cell.getValue();
            } else {
                actionData = ProfilingDataUtil.sum(actionData, cell.getValue());
            }
            table.put(clazz, action, actionData);
        }

        // sort the table
        tableComComparator.setSorting(key, order);
        ImmutableTable.Builder<ActorClass, Action, ComData> sortedBuilder = ImmutableTable.builder(); // preserves insertion order
        for (Table.Cell<ActorClass, Action, ComData> cell : tableComComparator.sortedCopy(table.cellSet())) {
            sortedBuilder.put(cell);
        }

        // return a sorted table
        return (Table<T, Action, ComData>) sortedBuilder.build();
    }
    return null;
}

From source file:org.apache.beam.runners.dataflow.worker.BeamFnMapTaskExecutorFactory.java

/**
 * Returns a table where the row key is the PTransform id, the column key is the side input id,
 * and the value is the corresponding PCollectionView.
 *///from   w  ww. j  a  v a  2  s .c o  m
private static ImmutableTable<String, String, PCollectionView<?>> buildPTransformIdToSideInputIdToPCollectionView(
        RegisterRequestNode registerRequestNode) {
    ImmutableTable.Builder<String, String, PCollectionView<?>> ptransformIdToSideInputIdToPCollectionViewBuilder = ImmutableTable
            .builder();
    for (Map.Entry<String, Iterable<PCollectionView<?>>> ptransformIdToPCollectionViews : registerRequestNode
            .getPTransformIdToPCollectionViewMap().entrySet()) {
        for (PCollectionView<?> pCollectionView : ptransformIdToPCollectionViews.getValue()) {
            ptransformIdToSideInputIdToPCollectionViewBuilder.put(ptransformIdToPCollectionViews.getKey(),
                    pCollectionView.getTagInternal().getId(), pCollectionView);
        }
    }

    return ptransformIdToSideInputIdToPCollectionViewBuilder.build();
}

From source file:co.turnus.profiling.util.ProfilingDataAnalyser.java

@SuppressWarnings("unchecked")
public <T> Table<T, Action, ExtendExecData> getSortedExecData(Class<T> type, Key key, Order order) {
    if (type.isAssignableFrom(Actor.class)) {
        // create a temporary table
        Table<Actor, Action, ExtendExecData> table = HashBasedTable.create();
        for (Cell<Actor, Action, ActionProfilingData> cell : data.asTable().cellSet()) {
            table.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
        }//ww w  . ja  v  a 2  s. c o  m

        // sort the table
        tableExecComparator.setSorting(key, order);
        ImmutableTable.Builder<Actor, Action, ExtendExecData> sortedBuilder = ImmutableTable.builder(); // preserves insertion order
        for (Table.Cell<Actor, Action, ExtendExecData> cell : tableExecComparator.sortedCopy(table.cellSet())) {
            sortedBuilder.put(cell);
        }

        // return a sorted table
        return (Table<T, Action, ExtendExecData>) sortedBuilder.build();
    } else if (type.isAssignableFrom(ActorClass.class)) {
        // create a temporary table
        Table<ActorClass, Action, ExtendExecData> table = HashBasedTable.create();
        for (Cell<Actor, Action, ActionProfilingData> cell : data.asTable().cellSet()) {
            ActorClass clazz = cell.getRowKey().getActorClass();
            Action action = cell.getColumnKey();
            ExtendExecData actionData = table.get(clazz, action);
            if (actionData == null) {
                actionData = cell.getValue();
            } else {
                actionData = ProfilingDataUtil.sum(actionData, cell.getValue());
            }
            table.put(clazz, action, actionData);
        }

        // sort the table
        tableExecComparator.setSorting(key, order);
        ImmutableTable.Builder<ActorClass, Action, ExtendExecData> sortedBuilder = ImmutableTable.builder(); // preserves insertion order
        for (Table.Cell<ActorClass, Action, ExtendExecData> cell : tableExecComparator
                .sortedCopy(table.cellSet())) {
            sortedBuilder.put(cell);
        }

        // return a sorted table
        return (Table<T, Action, ExtendExecData>) sortedBuilder.build();
    }
    return null;
}