Example usage for org.apache.commons.collections4 ListUtils partition

List of usage examples for org.apache.commons.collections4 ListUtils partition

Introduction

In this page you can find the example usage for org.apache.commons.collections4 ListUtils partition.

Prototype

public static <T> List<List<T>> partition(final List<T> list, final int size) 

Source Link

Document

Returns consecutive List#subList(int,int) sublists of a list, each of the same size (the final list may be smaller).

Usage

From source file:com.siva.javamultithreading.MultiThreadExecutor.java

public static void main(String[] args) throws ExecutionException, IOException {

    //Populate the data
    List<DomainObject> list = new ArrayList<>();
    DomainObject object = null;/*ww  w. j  a  v  a  2 s  . c o m*/
    for (int i = 0; i < 230000; i++) {
        object = new DomainObject();
        object.setId("ID" + i);
        object.setName("NAME" + i);
        object.setComment("COMMENT" + i);
        list.add(object);
    }

    int maxNoOfRows = 40000;
    int noOfThreads = 1;
    int remaining = 0;

    if (list.size() > 40000) {
        noOfThreads = list.size() / maxNoOfRows;
        remaining = list.size() % maxNoOfRows;
        if (remaining > 0) {
            noOfThreads++;
        }
    }

    List<List<DomainObject>> dos = ListUtils.partition(list, maxNoOfRows);

    ExecutorService threadPool = Executors.newFixedThreadPool(noOfThreads);
    CompletionService<HSSFWorkbook> pool = new ExecutorCompletionService<>(threadPool);

    // Excel creation through multiple threads
    long startTime = System.currentTimeMillis();

    for (List<DomainObject> listObj : dos) {
        pool.submit(new ExcelChunkSheetWriter(listObj));
    }

    HSSFWorkbook hSSFWorkbook = null;
    HSSFWorkbook book = new HSSFWorkbook();
    HSSFSheet sheet = book.createSheet("Report");

    try {
        for (int i = 0; i < 5; i++) {
            hSSFWorkbook = pool.take().get();
            System.out.println(
                    "sheet row count : sheet.PhysicalNumberOfRows() = " + sheet.getPhysicalNumberOfRows());
            int currentCount = sheet.getPhysicalNumberOfRows();
            int incomingCount = hSSFWorkbook.getSheetAt(0).getPhysicalNumberOfRows();
            if ((currentCount + incomingCount) > 60000) {
                sheet = book.createSheet("Report" + i);
            }
            ExcelUtil.copySheets(book, sheet, hSSFWorkbook.getSheetAt(0));
        }
    } catch (InterruptedException ex) {
        Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ExecutionException ex) {
        Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        writeFile(book, new FileOutputStream("Report.xls"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    //System.out.println("No of Threads : " + noOfThreads + " Size : " + list.size() + " remaining : " + remaining);
    long endTime = System.currentTimeMillis();
    System.out.println("Time taken: " + (endTime - startTime) + " ms");
    threadPool.shutdown();

    //startProcess();
}

From source file:com.rodaxsoft.mailgun.MailingListMemberManager.java

/**
 * Splits list into one list/1000 members
 * @param listMembers The member list to split
 * @return A list of member lists with sizes <code><=1000</code>
 *//*  w w w  . j  a va  2  s. com*/
private static List<List<ListMember>> splitList(List<ListMember> listMembers) {

    List<List<ListMember>> splitLists;

    //Split lists, Mailgun only supports adding 1000 members per API call
    if (listMembers.size() > 1000) {
        splitLists = ListUtils.partition(listMembers, 1000);
    }

    else {
        splitLists = new ArrayList<List<ListMember>>();
        splitLists.add(listMembers);
    }

    return splitLists;
}

From source file:de.kbs.acavis.integration.mas.odata.EntityHelper.java

public static List<List<Integer>> idPartitioner(Collection<Integer> ids, int partitionSize) {
    return ListUtils.partition(new LinkedList<Integer>(ids), partitionSize);
}

From source file:com.videaps.cube.solving.access.SplitColorsDelegate.java

public void execute(DelegateExecution execution) throws Exception {
    logger.info(execution.getCurrentActivityName());

    @SuppressWarnings("unchecked")
    List<String> brickColorsInner = (List<String>) execution.getVariable("brickColorsInner");

    StringBuffer colorBuf = new StringBuffer();
    ColorPicker colorPicker = new ColorPicker();

    int partitionSize = brickColorsInner.size() / ColorPicker.NO_OF_BRICKS_PER_FACE;
    logger.info("partitionSize=" + partitionSize);
    List<List<String>> partitions = ListUtils.partition(brickColorsInner, partitionSize);
    for (List<String> partition : partitions) {
        String colorStr = String.join("", partition);
        logger.info("colorStr=" + colorStr);
        String mostFrequentColor = colorPicker.mostFrequentColor(colorStr);
        logger.info("mostFrequentColor=" + mostFrequentColor);
        colorBuf.append(mostFrequentColor);
    }// w w w  .jav  a  2 s  .  c om

    execution.setVariable("brickColorsSplit", colorBuf.toString());
    logger.info("brickColorsSplit=" + colorBuf.toString());
}

From source file:com.twentyoneechoes.borges.task.GetFilesTask.java

@Override
protected void onPostExecute(List<SmbFile> files) {
    try {/* ww  w .  j a  va2s  .co  m*/
        final int cpuCount = Runtime.getRuntime().availableProcessors();
        final int maxPoolSize = cpuCount * 2 + 1;
        final int partitionSize = files.size() < maxPoolSize ? files.size() : (files.size() / maxPoolSize);

        List<List<SmbFile>> subSets = ListUtils.partition(files, partitionSize);

        mNumOfSets = subSets.size();

        for (List<SmbFile> subSet : subSets) {
            if (mIsMovie) {
                new DownloadMovieTask(mContext, mConfig, subSet, this).executeOnExecutor(THREAD_POOL_EXECUTOR);
            } else {
                new DownloadTvShowTask(mContext, mConfig, subSet, this).executeOnExecutor(THREAD_POOL_EXECUTOR);
            }
        }
    } catch (Exception e) {
        if (mCallback != null) {
            mCallback.failure();
        }
    }
}

From source file:io.chucknorris.app.facebook.messenger.cache.CategoriesCache.java

public List<List<String>> getCategoriesPaged(int pageSize) {
    return ListUtils.partition(getCategories(), pageSize);
}

From source file:com.nextdoor.bender.monitoring.cw.CloudwatchReporter.java

@Override
public void write(ArrayList<Stat> stats, long invokeTimeMs, Set<Tag> tags) {
    Date dt = new Date();
    dt.setTime(invokeTimeMs);/*  ww w .  j  a  v  a  2  s  .  co m*/

    Collection<Dimension> parentDims = tagsToDimensions(tags);
    List<MetricDatum> metrics = new ArrayList<MetricDatum>();

    /*
     * Create CW metric objects from bender internal Stat objects
     */
    for (Stat stat : stats) {
        /*
         * Dimension are CW's version of metric tags. A conversion must be done.
         */
        Collection<Dimension> metricDims = tagsToDimensions(stat.getTags());
        metricDims.addAll(parentDims);

        MetricDatum metric = new MetricDatum();
        metric.setMetricName(stat.getName());
        // TODO: add units to Stat object SYSTEMS-870
        metric.setUnit(StandardUnit.None);
        metric.setTimestamp(dt);
        metric.setDimensions(metricDims);
        metric.setValue((double) stat.getValue());

        metrics.add(metric);
    }

    /*
     * Not very well documented in java docs but CW only allows 20 metrics at a time.
     */
    List<List<MetricDatum>> chunks = ListUtils.partition(metrics, 20);
    for (List<MetricDatum> chunk : chunks) {
        PutMetricDataRequest req = new PutMetricDataRequest();
        req.withMetricData(chunk);
        req.setNamespace(namespace);

        this.client.putMetricData(req);
    }
}

From source file:com.jerrellmardis.amphitheatre.task.GetFilesTask.java

@Override
protected void onPostExecute(List<SmbFile> files) {
    try {/*from   ww w .ja  v a2 s . c om*/
        final int cpuCount = Runtime.getRuntime().availableProcessors();
        final int maxPoolSize = cpuCount * 2 + 1;
        final int partitionSize = files.size() < maxPoolSize ? files.size() : (files.size() / maxPoolSize);

        List<List<SmbFile>> subSets = ListUtils.partition(files, partitionSize);

        mNumOfSets = subSets.size();

        //TODO This is really stupid. Fix.
        for (List<SmbFile> subSet : subSets) {
            if (mIsMovie) {
                new DownloadMovieTask(mContext, mConfig, subSet, this).executeOnExecutor(THREAD_POOL_EXECUTOR);
            } else {
                new DownloadTvShowTask(mContext, mConfig, subSet, this).executeOnExecutor(THREAD_POOL_EXECUTOR);
            }
        }
    } catch (Exception e) {
        if (mCallback != null) {
            mCallback.failure();
        }
    }
}

From source file:integration.AbstractChuckNorrisCallbackHandlerIntegrationTests.java

@Before
public void setUp() {

    this.senderId = "12345";
    this.recipient = new IdMessageRecipient(senderId);

    this.categoriesCache = mock(CategoriesCache.class);
    this.chuckNorrisClient = mock(ChuckNorrisClient.class);
    this.messenger = mock(Messenger.class);
    this.sendOperations = mock(SendOperations.class);

    Injector injector = Guice.createInjector(Modules.override(new AppModule()).with(new AbstractModule() {

        @Override//from  w  ww  .  j ava 2  s.c o  m
        protected void configure() {
            bind(CategoriesCache.class).toInstance(categoriesCache);
            bind(ChuckNorrisClient.class).toInstance(chuckNorrisClient);
            bind(Messenger.class).toInstance(messenger);
        }
    }));
    this.callbackHandler = injector.getInstance(ChuckNorrisCallbackHandler.class);

    when(messenger.send()).thenReturn(sendOperations);

    List<String> categories;
    categories = new ArrayList<>();
    for (int i = 1; i <= 20; i++) {
        categories.add("category" + i);
    }
    when(categoriesCache.getCategories()).thenReturn(categories);

    when(categoriesCache.getCategoriesPaged(6)).thenReturn(ListUtils.partition(categories, 6));

    Joke joke = new Joke();
    joke.setValue("Chuck Norris once accidentally broke steel by touching it.");
    when(chuckNorrisClient.getRandomJoke()).thenReturn(joke);

    Joke jokeWithCategory = new Joke();
    jokeWithCategory.setValue("When Chuck Norris wants an egg, he cracks open a chicken.");
    when(chuckNorrisClient.getRandomJoke("category1")).thenReturn(jokeWithCategory);

    Joke jokeFromSearch = new Joke();
    jokeFromSearch.setValue("Putin: We have the best nuclear weapons Obama: We have Chuck Norris");
    when(chuckNorrisClient.searchJokes("obama")).thenReturn(Arrays.asList(jokeFromSearch));
}

From source file:com.feilong.core.util.CollectionsUtilTest.java

/**
 * Test partition./*from w  w w.  j ava 2s . co m*/
 */
@Test
public void testPartition() {
    List<String> list = toList("xinge", "feilong1", "feilong2");
    LOGGER.debug("list:{}", JsonUtil.format(ListUtils.partition(list, 2)));
}