List of usage examples for com.google.common.collect Iterables partition
public static <T> Iterable<List<T>> partition(final Iterable<T> iterable, final int size)
From source file:ome.services.graphs.ModelObjectSequencer.java
/** * Sort a list of original file IDs such that files precede containing directories. * @param session the Hibernate session//from ww w . j a v a2 s .co m * @param unorderedIds the IDs of original files * @return a batching of the given IDs such that a batch containing a file precedes a batch containing a containing directory */ public static Collection<Collection<Long>> sortOriginalFileIds(Session session, Collection<Long> unorderedIds) { if (unorderedIds.size() < 2) { /* no need to rearrange anything, as there are not multiple original files */ return Collections.<Collection<Long>>singleton(new ArrayList<Long>(unorderedIds)); } final String hql = "SELECT id, length(path) FROM OriginalFile WHERE id IN (:ids)"; final SortedMap<Integer, List<Long>> filesByPathLength = new TreeMap<Integer, List<Long>>( Ordering.natural().reverse()); for (final Collection<Long> idBatch : Iterables.partition(unorderedIds, 256)) { for (final Object[] result : (List<Object[]>) session.createQuery(hql).setParameterList("ids", idBatch) .list()) { final Long id = (Long) result[0]; final Integer length = (Integer) result[1]; List<Long> idList = filesByPathLength.get(length); if (idList == null) { idList = new ArrayList<Long>(); filesByPathLength.put(length, idList); } idList.add(id); } } final Collection<Collection<Long>> orderedIds = new ArrayList<Collection<Long>>(); for (final List<Long> ids : filesByPathLength.values()) { orderedIds.add(ids); } return orderedIds; }
From source file:io.crate.operation.ThreadPools.java
/** * runs each runnable of the runnableCollection in it's own thread unless there aren't enough threads available. * In that case it will partition the runnableCollection to match the number of available threads. * * @throws RejectedExecutionException in case all threads are busy and overloaded. *//* ww w . j av a2 s . c o m*/ public static void runWithAvailableThreads(ThreadPoolExecutor executor, Collection<Runnable> runnableCollection) throws RejectedExecutionException { int availableThreads = Math.max(executor.getMaximumPoolSize() - executor.getActiveCount(), 2); if (availableThreads < runnableCollection.size()) { Iterable<List<Runnable>> partition = Iterables.partition(runnableCollection, runnableCollection.size() / availableThreads); for (final List<Runnable> runnableList : partition) { executor.execute(new Runnable() { @Override public void run() { for (Runnable runnable : runnableList) { runnable.run(); } } }); } } else { for (Runnable runnable : runnableCollection) { executor.execute(runnable); } } }
From source file:de.iteratec.iteraplan.persistence.util.CriteriaUtil.java
public static <T> Criterion createInRestrictions(String propName, final Iterable<T> elements) { final Iterable<List<T>> partitions = Iterables.partition(elements, Constants.DB_ORACLE_MAX_IN); final Disjunction disjunction = Restrictions.disjunction(); for (List<T> partition : partitions) { disjunction.add(Restrictions.in(propName, partition)); }/*from www. j a v a2 s . c o m*/ return disjunction; }
From source file:cosmos.results.PagedResults.java
public PagedResults(CloseableIterable<T> results, Paging limits) { checkNotNull(results);/*from w ww. j a v a2 s. co m*/ checkNotNull(limits); source = results; pagedLimitedResults = Iterables.partition(Iterables.limit(results, limits.maxResults()), limits.pageSize()); }
From source file:com.palantir.atlasdb.keyvalue.rdbms.utils.AtlasSqlUtils.java
public static <V> void batch(Iterable<V> items, Function<Collection<V>, Void> runWithBatch) { for (List<V> chunk : Iterables.partition(items, CHUNK_SIZE)) { runWithBatch.apply(chunk);/* w w w . j a va 2 s . co m*/ } }
From source file:org.asoem.greyfish.utils.collect.LinearSequences.java
/** * Create a crossover product between two {@code Iterables} with crossovers at given {@code indices}. This means * that both {@code Iterable}s in the returned product are a combination of the input iterables in such a way, that * the constructed iterable switches the input iterable at each given position. Both input {@code Iterable}s will be * zipped with {@link Products#zip(Iterable, Iterable)}. Therefore the returned {@code Iterable}s will have the same * size equal to the size of the input iterable with the fewest elements. * * @param x The first Iterable// w w w .j a va2 s .c o m * @param y The second Iterable * @param indices the indices at which to do the crossovers * @param <E> the type of the elements in {@code Iterable}s * @return a product of Iterables with crossovers at the given indices */ public static <E> Product2<Iterable<E>, Iterable<E>> crossover(final Iterable<E> x, final Iterable<E> y, final Set<Integer> indices) { checkNotNull(x); checkNotNull(y); checkNotNull(indices); final Iterable<Product2<E, E>> zipped = Products.zip(x, y); if (indices.isEmpty()) { return Products.unzip(zipped); } else { final FunctionalList<Range<Integer>> ranges = ImmutableFunctionalList.copyOf( Iterables.transform(Iterables.partition(Ordering.natural().immutableSortedCopy(indices), 2), new Function<List<Integer>, Range<Integer>>() { @Nullable @Override public Range<Integer> apply(@Nullable final List<Integer> input) { assert input != null; return input.size() == 2 ? Range.closed(input.get(0), input.get(1)) : Range.atLeast(input.get(0)); } })); return Products.unzip(Iterables.transform(Products.zipWithIndex(zipped), new Function<Product2<Product2<E, E>, Integer>, Product2<E, E>>() { @Nullable @Override public Product2<E, E> apply(@Nullable final Product2<Product2<E, E>, Integer> input) { assert input != null; return ranges.any(new Predicate<Range<Integer>>() { @Override public boolean apply(@Nullable final Range<Integer> range) { assert range != null; return range.contains(input.second()); } }) ? Products.swap(input.first()) : input.first(); } })); } }
From source file:com.github.fhuss.storm.cassandra.bolt.GroupingBatchBuilder.java
private Iterable<PairBatchStatementTuples> build() { Iterable<List<PairStatementTuple>> partition = Iterables.partition(statements, batchSizeRows); return Iterables.transform(partition, new Function<List<PairStatementTuple>, PairBatchStatementTuples>() { @Override//from ww w.j a v a 2s .c o m public PairBatchStatementTuples apply(List<PairStatementTuple> l) { final List<Tuple> inputs = new LinkedList<>(); final BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED); for (PairStatementTuple pair : l) { batch.add(pair.getStatement()); inputs.add(pair.getTuple()); } return new PairBatchStatementTuples(inputs, batch); } }); }
From source file:de.iteratec.iteraplan.persistence.util.CriteriaUtil.java
public static <T> Criterion createNotInRestrictions(String propName, final Iterable<T> elements) { final Iterable<List<T>> partitions = Iterables.partition(elements, Constants.DB_ORACLE_MAX_IN); final Disjunction disjunction = Restrictions.disjunction(); for (List<T> partition : partitions) { disjunction.add(Restrictions.in(propName, partition)); }//from w w w. j av a2 s . c o m return Restrictions.not(disjunction); }
From source file:io.crate.execution.support.ThreadPools.java
/** * runs each runnable of the runnableCollection in it's own thread unless there aren't enough threads available. * In that case it will partition the runnableCollection to match the number of available threads. * * @param executor executor that is used to execute the callableList * @param poolSize the corePoolSize of the given executor * @param suppliers a collection of callable that should be executed * @param mergeFunction function that will be applied to merge the results of multiple callable in case that they are * executed together if the threadPool is exhausted * @param <T> type of the final result * @return a future that will return a list of the results of the callableList * @throws RejectedExecutionException in case all threads are busy and overloaded. *//* www. ja v a2s .co m*/ public static <T> CompletableFuture<List<T>> runWithAvailableThreads(ThreadPoolExecutor executor, int poolSize, Collection<Supplier<T>> suppliers, final Function<List<T>, T> mergeFunction) throws RejectedExecutionException { List<CompletableFuture<T>> futures; int availableThreads = Math.max(poolSize - executor.getActiveCount(), 1); if (availableThreads < suppliers.size()) { Iterable<List<Supplier<T>>> partition = Iterables.partition(suppliers, suppliers.size() / availableThreads); futures = new ArrayList<>(availableThreads + 1); for (final List<Supplier<T>> callableList : partition) { CompletableFuture<T> future = CompletableFuture.supplyAsync(() -> { List<T> results = new ArrayList<T>(callableList.size()); for (Supplier<T> supplier : callableList) { results.add(supplier.get()); } return mergeFunction.apply(results); }, executor); futures.add(future); } } else { futures = new ArrayList<>(suppliers.size()); for (Supplier<T> supplier : suppliers) { futures.add(CompletableFuture.supplyAsync(supplier, executor)); } } return CompletableFutures.allAsList(futures); }
From source file:com.myGengo.alfresco.polling.MyGengoJobsBatchProcessWorker.java
@Override public void process(MyGengoJobBatchEntry entry) throws Throwable { AuthenticationUtil.setFullyAuthenticatedUser(runAsUser); Collection<NodeRef> jobRefs = entry.getJobRefs(); Iterable<List<NodeRef>> partition = Iterables.partition(jobRefs, 50); for (List<NodeRef> partitionJobs : partition) { translationService.refreshTranslations(entry.getAccount(), partitionJobs); }//from ww w .j a v a2 s .c o m }