package uk.gov.admin;

import com.jcabi.http.Request;
import com.jcabi.http.Response;
import com.jcabi.http.request.JdkRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class Loader {

    private static final int BATCH_SIZE = 2000;

    private final String mintUrl;
    private long entryCount = 0;

    public Loader(String mintUrl) {
        this.mintUrl = mintUrl;
    }

    public void load(Iterator<String> fileEntries) throws IOException {

        List<String> entryBatch = new ArrayList<>();

        while (fileEntries.hasNext()) {

            entryBatch.add(fileEntries.next());

            if (++entryCount % BATCH_SIZE == 0) {
                send(entryBatch);
                entryBatch = new ArrayList<>();
            }

        }

        if (!entryBatch.isEmpty()) {
            send(entryBatch);
        }
    }

    private void send(List<String> batch) throws IOException {

        Response response = new JdkRequest(mintUrl)
                .method(Request.POST)
                .body()
                .set(String.join("\n", batch))
                .back()
                .fetch();
        if (response.status() != 200) {
            throw new RuntimeException("Exception while loading entries: statusCode -> " + response.status() + "\n" +
                    " entity -> " + response.body());
        }

        System.out.println("Loaded " + entryCount + " entries...");
    }
}

@@ -16,16 +16,60 @@
 admin;%0A%0A
+import com.google.common.collect.Iterables;%0A
 import c
@@ -198,36 +198,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -525,25 +525,24 @@
 ption %7B%0A
-%0A
         
 List%3CStr
@@ -533,16 +533,25 @@
         
+Iterable%3C
 List%3CStr
@@ -554,16 +554,17 @@
 %3CString%3E
+%3E
  entryBa
@@ -570,312 +570,117 @@
 atch
+es
  = 
-new ArrayList%3C%3E();%0A%0A        while (fileEntries.hasNext()) %7B%0A%0A            entryBatch.add(fileEntries.next());%0A%0A            if (++entryCount %25 BATCH_SIZE == 0) %7B%0A                send(entryBatch);%0A                entryBatch = new ArrayList%3C%3E();%0A            %7D%0A%0A        %7D%0A%0A        if (!entryBatch.isEmpty()
+Iterables.partition(() -%3E fileEntries, BATCH_SIZE);%0A%0A        for (List%3CString%3E entryBatch : entryBatches
 ) %7B%0A
@@ -1218,24 +1218,60 @@
 );%0A        %7D
+%0A        entryCount += batch.size();
 %0A%0A        Sy

use Iterables.partition to simplify code :golf: