Example usage for com.google.common.collect Iterables paddedPartition

List of usage examples for com.google.common.collect Iterables paddedPartition

Introduction

In this page you can find the example usage for com.google.common.collect Iterables paddedPartition.

Prototype

public static <T> Iterable<List<T>> paddedPartition(final Iterable<T> iterable, final int size) 

Source Link

Document

Divides an iterable into unmodifiable sublists of the given size, padding the final iterable with null values if necessary.

Usage

From source file:org.calrissian.mango.collect.CloseableIterables.java

/**
 * Divides a closeable iterable into unmodifiable sublists of the given size, padding
 * the final list with null values if necessary. For example, partitioning
 * a closeable iterable containing {@code [a, b, c, d, e]} with a partition size of 3
 * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterable containing
 * two inner lists of three elements each, all in the original order.
 *
 * <p>Iterators returned by the returned closeableiterable do not support the {@link
 * Iterator#remove()} method./*from   www.jav a2  s . co  m*/
 */
public static <T> CloseableIterable<List<T>> paddedParition(final CloseableIterable<T> iterable,
        final int size) {
    return wrap(Iterables.paddedPartition(iterable, size), iterable);
}

From source file:utils.teamcity.wallt.view.wall.WallView.java

private GridPane buildScreenPane(final Iterable<Object> buildsInScreen, final int nbColums,
        final int byColums) {
    final GridPane screenPane = new GridPane();
    screenPane.setHgap(GAP_SPACE);/* w  ww.j  ava  2 s  .  c o m*/
    screenPane.setVgap(GAP_SPACE);
    screenPane.setPadding(new Insets(GAP_SPACE));
    screenPane.setStyle("-fx-background-color:black;");
    screenPane.setAlignment(Pos.CENTER);

    final Iterable<List<Object>> partition = Iterables.paddedPartition(buildsInScreen, byColums);
    for (int x = 0; x < nbColums; x++) {
        final List<Object> buildList = x < size(partition) ? Iterables.get(partition, x)
                : Collections.emptyList();
        for (int y = 0; y < byColums; y++) {
            if (buildList.isEmpty()) {
                createEmptyTile(screenPane, x, y, nbColums, byColums);
                continue;
            }

            final Object build = Iterables.get(buildList, y);
            if (build == null)
                createEmptyTile(screenPane, x, y, nbColums, byColums);
            else
                createTileFromModel(screenPane, build, x, y, nbColums, byColums);
        }
    }

    return screenPane;
}

From source file:com.eucalyptus.reporting.modules.backend.DescribeSensorsListener.java

@Override
public void fireEvent(final Hertz event) {
    if (!Bootstrap.isOperational() || !BootstrapArgs.isCloudController()
            || !event.isAsserted(DEFAULT_POLL_INTERVAL_MINS)) {
        return;//from  www  .j a  v a  2s.c o  m
    } else {
        if (DEFAULT_POLL_INTERVAL_MINS >= 1) {
            COLLECTION_INTERVAL_TIME_MS = ((int) TimeUnit.MINUTES.toMillis(DEFAULT_POLL_INTERVAL_MINS) / 2);
        } else {
            COLLECTION_INTERVAL_TIME_MS = 0;
        }

        if (COLLECTION_INTERVAL_TIME_MS == 0 || HISTORY_SIZE > 15 || HISTORY_SIZE < 1) {
            LOG.debug("The instance usage report is disabled");
        } else if (COLLECTION_INTERVAL_TIME_MS <= MAX_WRITE_INTERVAL_MS) {

            try {

                if (event.isAsserted(TimeUnit.MINUTES.toSeconds(DEFAULT_POLL_INTERVAL_MINS))) {
                    if (Bootstrap.isFinished() && Hosts.isCoordinator()) {
                        if (busy.compareAndSet(false, true)) {
                            Threads.lookup(Reporting.class).limitTo(REPORTING_NUM_THREADS)
                                    .submit(new Callable<Object>() {

                                        @Override
                                        public Object call() throws Exception {
                                            try {

                                                List<VmInstance> instList = VmInstances.list(VmState.RUNNING);

                                                List<String> instIdList = Lists.newArrayList();

                                                for (final VmInstance inst : instList) {
                                                    instIdList.add(inst.getInstanceId());
                                                }
                                                Iterable<List<String>> processInts = Iterables
                                                        .paddedPartition(instIdList, SENSOR_QUERY_BATCH_SIZE);

                                                for (final ServiceConfiguration ccConfig : Topology
                                                        .enabledServices(ClusterController.class)) {
                                                    for (List<String> instIds : processInts) {

                                                        ArrayList<String> instanceIds = Lists
                                                                .newArrayList(instIds);
                                                        Iterables.removeIf(instanceIds, Predicates.isNull());
                                                        //                  LOG.info("DecribeSensorCallback about to be sent");
                                                        /**
                                                         * Here this is hijacking the sensor callback in order to control the thread of execution used when invoking the
                                                         */
                                                        final DescribeSensorCallback msgCallback = new DescribeSensorCallback(
                                                                HISTORY_SIZE, COLLECTION_INTERVAL_TIME_MS,
                                                                instanceIds) {
                                                            @Override
                                                            public void fireException(Throwable e) {
                                                            }

                                                            @Override
                                                            public void fire(DescribeSensorsResponse msg) {
                                                            }
                                                        };
                                                        /**
                                                         * Here we actually get the future reference to the result and, from this thread, invoke .fire().
                                                         */
                                                        Future<DescribeSensorsResponse> ret = AsyncRequests
                                                                .newRequest(msgCallback).dispatch(ccConfig);
                                                        try {
                                                            new DescribeSensorCallback(HISTORY_SIZE,
                                                                    COLLECTION_INTERVAL_TIME_MS, instanceIds)
                                                                            .fire(ret.get());
                                                            //                  LOG.info("DecribeSensorCallback has been successfully executed");
                                                        } catch (Exception e) {
                                                            Exceptions.maybeInterrupted(e);
                                                        }
                                                    }
                                                }
                                            } finally {
                                                /**
                                                 * Only and finally set the busy bit back to false.
                                                 */
                                                busy.set(false);
                                            }
                                            return null;
                                        }
                                    });
                        }
                    }

                }
            } catch (Exception ex) {
                LOG.error("Unable to listen for describe sensors events", ex);
            }

        } else {
            LOG.error("DEFAULT_POLL_INTERVAL_MINS : " + DEFAULT_POLL_INTERVAL_MINS
                    + " must be less than 1440 minutes");
        }
    }
}