class ListUtils
{
// Java program to demonstrate Guava's Lists.partition() method
public static void main(String[] args)
{
List<Integer> mutableList = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = Lists.partition(mutableList, 2);
for (List<Integer> sublist: lists)
System.out.println(sublist);
}
}
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a List of Integers
List<Integer> myList
= Arrays.asList(1, 2, 3, 4, 5);
// Using Lists.partition() method to divide
// the original list into sublists of the same
// size, which are just views of the original list.
// The final list may be smaller.
List<List<Integer> > lists
= Lists.partition(myList, 2);
// Displaying the sublists
for (List<Integer> sublist: lists)
System.out.println(sublist);
}
}
public void givenList_whenParitioningIntoNSublists_thenCorrect() {
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subSets = Lists.partition(intList, 3);
List<Integer> lastPartition = subSets.get(2);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
public void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChangeAsWell() {
// Given
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subSets = Lists.partition(intList, 3);
// When
intList.add(9);
// Then
List<Integer> lastPartition = subSets.get(2);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
assertThat(lastPartition, equalTo(expectedLastPartition));
}
List<Foo> foos = ...
for (List<Foo> partition : Lists.partition(foos, n)) {
// do something with partition
}
public int reindex(List<K> keys, int batchSize, ReindexOperation<E> reindexOperation) {
int count = 0;
List<List<K>> batches = Lists.partition(keys, batchSize);
for (List<K> batchKeys : batches) {
List<E> batch = get(batchKeys);
batch = reindexOperation == null ? batch : reindexOperation.apply(batch);
if (reindexOperation != null) {
// we only re-save the batch when a re-index op is supplied, otherwise the data can't have changed.
ofy().save().entities(batch).now();
}
if (shouldSearch()) {
index(batch).complete();
}
count += batch.size();
ofy().clear(); // Clear the Objectify cache to free memory for next batch
Logger.info("Reindexed %d entities of type %s, %d of %d", batch.size(), entityType.getSimpleName(), count, keys.size());
}
return count;
}
private void updateAndDeleteCache() {
List<Long> ids = Lists.newArrayList(appNamespaceIdCache.keySet());
if (CollectionUtils.isEmpty(ids)) {
return;
}
List<List<Long>> partitionIds = Lists.partition(ids, 500);
for (List<Long> toRebuild : partitionIds) {
Iterable<AppNamespace> appNamespaces = appNamespaceRepository.findAll(toRebuild);
if (appNamespaces == null) {
continue;
}
//handle updated
Set<Long> foundIds = handleUpdatedAppNamespaces(appNamespaces);
//handle deleted
handleDeletedAppNamespaces(Sets.difference(Sets.newHashSet(toRebuild), foundIds));
}
}
public int reindex(List<K> keys, int batchSize, ReindexOperation<E> reindexOperation) {
int count = 0;
List<List<K>> batches = Lists.partition(keys, batchSize);
for (List<K> batchKeys : batches) {
List<E> batch = get(batchKeys);
batch = reindexOperation == null ? batch : reindexOperation.apply(batch);
if (reindexOperation != null) {
// we only re-save the batch when a re-index op is supplied, otherwise the data can't have changed.
ofy().save().entities(batch).now();
}
if (shouldSearch()) {
index(batch).complete();
}
count += batch.size();
ofy().clear(); // Clear the Objectify cache to free memory for next batch
Logger.info("Reindexed %d entities of type %s, %d of %d", batch.size(), entityType.getSimpleName(), count, keys.size());
}
return count;
}
private static List<String> generateHtmlChunks(List<ReportItem> reportItemList) {
List<String> htmlChunks = new ArrayList<>();
VelocityEngine velocityEngine = new VelocityEngine();
Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.init(p);
Template template = velocityEngine.getTemplate("template/report_template.html");
int maxItemsInReport = CliHelper.getMaxItemsInReport();
List<List<ReportItem>> reportItemsChunks = Lists.partition(reportItemList, maxItemsInReport);
for (List<ReportItem> reportItemsChunk : reportItemsChunks ) {
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("jarPath", CliHelper.getPathToAnalyze());
velocityContext.put("ruleName", reportItemsChunk.get(0).getRuleName());
velocityContext.put("reportItems", reportItemsChunk);
StringWriter stringWriter = new StringWriter();
template.merge(velocityContext, stringWriter);
htmlChunks.add(stringWriter.toString());
}
return htmlChunks;
}
private static void export(final Configuration configuration,
final TableInfo table) throws IOException {
warnOnMultipleScans(configuration, table);
final List<Attribute> attributes = CommonObjects.getTableToAttribute().get(table);
final List<List<Attribute>> groups = Lists.partition(attributes, configuration.getOpenFileNr());
int startIndex = 0;
for (final List<Attribute> group : groups) {
final List<Writer> writers = writeToDisk(configuration, table, group, startIndex);
uniqueAndSort(configuration, writers);
startIndex += group.size();
}
}