Example usage for java.util List subList

List of usage examples for java.util List subList

Introduction

In this page you can find the example usage for java.util List subList.

Prototype

List<E> subList(int fromIndex, int toIndex);

Source Link

Document

Returns a view of the portion of this list between the specified fromIndex , inclusive, and toIndex , exclusive.

Usage

From source file:dhbw.clippinggorilla.external.solr.SOLR.java

/**
 * adds list of beans to solr/*from w w w  .  ja  v a2 s.  co m*/
 *
 * @param beans
 */
public static void addBeans(List<?> beans) {
    if (client == null) {
        setServer();
    }
    List<?> subBeans;
    for (int i = 0; i < beans.size(); i += 25) {
        if (beans.size() >= i + 25) {
            subBeans = beans.subList(i, i + 25);
        } else {
            subBeans = beans.subList(i, beans.size());
        }
        try {
            client.addBeans(subBeans);
        } catch (SolrServerException | IOException | RemoteSolrException e) {
            Log.error("Solr: Can't add or commit", e);
        }
    }
}

From source file:cloudlens.engine.CLBuilder.java

public static void spawn(CLElement element, List<ASTElement> astElements) {
    if (!astElements.isEmpty()) {
        final ASTElement e = astElements.get(0);
        final List<ASTElement> astTail = astElements.subList(1, astElements.size());
        switch (e.type) {
        case Declaration:
        case Block:
        case Lens:
            final CLElement dbl = new CLElement(e);
            element.children.add(dbl);/* w w  w.  ja v a  2 s  .  c  om*/
            spawn(dbl, astTail);
            break;
        case Process:
        case After:
        case Run:
        case Match:
        case Source:
            final CLElement srgmrs = new CLElement(e);
            element.children.add(srgmrs);
            spawn(element, astTail);
            break;
        }
    }
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

static public Map<String, UUID> fetch(List<String> names, int limitByRequest)
        throws IOException, InterruptedException {
    Map<String, UUID> UUIDs = new HashMap<String, UUID>();
    int requests = (names.size() / limitByRequest) + 1;

    List<String> tempNames;
    Map<String, UUID> tempUUIDs;

    for (int i = 0; i < requests; i++) {
        tempNames = names.subList(limitByRequest * i, Math.min((limitByRequest * (i + 1)) - 1, names.size()));
        tempUUIDs = fetch(tempNames);//from w ww  .  j  a  v  a2s . c  o  m
        UUIDs.putAll(tempUUIDs);
        Thread.sleep(400);
    }

    return UUIDs;
}

From source file:Main.java

public static <T> List<T> limit(List<T> list, Integer start, Integer numberOfRecords) {
    if (start != null && numberOfRecords != null && start >= 0 && numberOfRecords >= 0) {
        int indexFrom = start;
        int indexTo = start + numberOfRecords;
        if (indexFrom > list.size()) {
            indexFrom = list.size();//  w ww .  j av  a2 s .co m
        }
        if (indexTo > list.size()) {
            indexTo = list.size();
        }
        return list.subList(indexFrom, indexTo);
    } else {
        return list;
    }
}

From source file:de.unisb.cs.st.javalanche.mutation.run.task.MutationTaskCreator.java

/**
 * Generates given number of mutation task, where each task consists of a
 * given number of mutations. Note: The MutationProperties.PROJECT_PREFIX
 * variable has to be set.//from w w w.  java  2s  .c  o m
 * 
 * @param numberOfTasks
 *            number of tasks that should be created
 * @param mutationsPerTask
 *            number of mutations per task
 * @throws IOException
 */
public static void createMutationTasks(int numberOfTasks, int mutationsPerTask) throws IOException {
    String prefix = ConfigurationLocator.getJavalancheConfiguration().getProjectPrefix();
    int numberOfIds = numberOfTasks * mutationsPerTask;
    List<Long> mutationIds = getMutations(prefix, numberOfIds);
    Collections.shuffle(mutationIds);
    int i = 1;
    for (; i <= numberOfTasks; i++) {
        List<Long> idsForTask = new ArrayList<Long>();
        if (mutationIds.size() >= mutationsPerTask) {
            idsForTask.addAll(mutationIds.subList(0, mutationsPerTask));
        } else {
            logger.info("Not enough mutations fetched from db");
            idsForTask.addAll(mutationIds);
        }
        mutationIds.removeAll(idsForTask);
        if (idsForTask.size() > 0) {
            writeListToFile(idsForTask, i, numberOfTasks);
        } else {
            logger.info("No more mutations. Finishing after file " + (i - 1));

            break;
        }
    }
    i = i - 1;
    System.out.println("Created " + i + " mutation tasks");
}

From source file:Main.java

/**
 * Splits list into several sub-lists according to predicate.
 * @param <T>/*from www. j  a  v  a 2  s.c o m*/
 * @param list
 * @param predicate
 * @return 
 */
public static final <T> List<List<T>> split(List<T> list, Predicate<T> predicate) {
    if (list.isEmpty()) {
        return Collections.EMPTY_LIST;
    }
    List<List<T>> lists = new ArrayList<>();
    boolean b = predicate.test(list.get(0));
    int len = list.size();
    int start = 0;
    for (int ii = 1; ii < len; ii++) {
        boolean t = predicate.test(list.get(ii));
        if (b != t) {
            lists.add(list.subList(start, ii));
            start = ii;
            b = t;
        }
    }
    lists.add(list.subList(start, len));
    return lists;
}

From source file:Main.java

/**
 * Creates a {@link JMenu} from the given list and action map.
 * /*from ww w.  j a v  a 2s  .  c om*/
 * @param actionMap
 * @param list
 * @return
 */
public static JMenu createMenu(ActionMap actionMap, List<?> list) {
    // The first item will be the action for the JMenu
    final Action titleAction = actionMap.get(list.get(0));
    if (titleAction == null) {
        return null;
    }
    final JMenu menu = new JMenu();
    menu.setAction(titleAction);

    // The rest of the items represent the menu items.
    for (Object element : list.subList(1, list.size())) {
        if (element == null) {
            menu.addSeparator();
        } else if (element instanceof List<?>) {
            JMenu newMenu = createMenu(actionMap, (List<?>) element);
            if (newMenu != null) {
                menu.add(newMenu);
            }
        } else if (element.getClass().isArray()) {
            JMenu newMenu = createMenu(actionMap, (Object[]) element);
            if (newMenu != null) {
                menu.add(newMenu);
            }
        } else {
            final Action action = actionMap.get(element);
            if (action == null) {
                continue;
            } else {
                menu.add(createMenuItem(action));
            }
        }
    }
    return menu;
}

From source file:br.com.diegosilva.jsfcomponents.util.Utils.java

/**
 * Moves the element at <tt>oldPosition</tt> to <tt>newPosition</tt>.
 * <p>/*from w  w  w. ja  va 2s  .c  om*/
 * Correctly handles forward and backward shifting of previous or subsequent
 * elements.
 * 
 * @param list
 *            the list that is affected
 * @param oldPosition
 *            the position of the element to move
 * @param newPosition
 *            the new position of the element
 */
public static void shiftListElement(List list, int oldPosition, int newPosition) {
    if (oldPosition > newPosition) {
        Collections.rotate(list.subList(newPosition, oldPosition + 1), +1);
    } else if (oldPosition < newPosition) {
        Collections.rotate(list.subList(oldPosition, newPosition + 1), -1);
    }
}

From source file:com.google.android.apps.santatracker.games.cityquiz.CityQuizUtil.java

private static List<City> getCities(Context context) {
    List<City> cities = new ArrayList<>();
    JSONArray jCities = getCitiesFromFile(context);
    for (int i = 0; jCities != null && i < jCities.length(); i++) {
        try {//from   www .jav a 2s  .  co  m
            JSONObject jCity = jCities.getJSONObject(i);
            double lat = jCity.getDouble("lat");
            double lng = jCity.getDouble("lng");
            String cityResourceName = jCity.getString("name");
            int cityNameResourceId = context.getResources().getIdentifier(cityResourceName, "string",
                    context.getPackageName());
            String cityName;
            // Check if city name string resource is found.
            if (cityNameResourceId != 0) {
                // Use string resource for city name.
                cityName = context.getResources().getString(cityNameResourceId);
            } else {
                // Use default English city name.
                cityName = jCity.getString("default_name");
            }
            String imageUrl = jCity.getString("image_name");
            String imageAuthor = jCity.getString("image_author");
            City city = new City(lat, lng, imageUrl, imageAuthor, cityName);
            cities.add(city);
        } catch (JSONException e) {
            Log.e(TAG, "Unable to get city from json, " + e);
        }
    }

    // Check if there are enough cities to set fake ones.
    if (cities.size() > 3) {
        // Set fake locations for each city.
        for (int i = 0; i < cities.size(); i++) {
            City city = cities.get(i);
            List<City> tempCities = new ArrayList<>(cities);
            // Sort tempCities in order of closest to the current city.
            Collections.sort(tempCities, new CityLocationComparator(city));

            // Get the closest three cities, excluding the current city.
            List<City> closestCities = tempCities.subList(1, 4);
            Collections.shuffle(closestCities);

            // Choose the first two of the three cities from the closestCities list.
            city.setIncorrectLoaciton(City.FIRST_FAKE_INDEX, closestCities.get(0).getCorrectLocation());
            city.setIncorrectLoaciton(City.SECOND_FAKE_INDEX, closestCities.get(1).getCorrectLocation());
        }
    }

    return cities;
}

From source file:org.jboss.aerogear.unifiedpush.utils.perf.BatchUtils.java

public static void registerApplicationsViaBatchEndpoint(MassPushApplication massive, Session session) {

    List<PushApplication> pushApplications = new ArrayList<PushApplication>(massive.getApplications());

    while (!pushApplications.isEmpty()) {
        int toIndex = APPLICATION_PAGING;

        if (pushApplications.size() < APPLICATION_PAGING) {
            toIndex = pushApplications.size();
        }/*from   w  w  w  . jav  a2s. c  o m*/

        List<PushApplication> sublist = pushApplications.subList(0, toIndex);

        MassPushApplication mass = new MassPushApplication();
        mass.setApplications(sublist);

        Response response = session.givenAuthorized().contentType(ContentTypes.json())
                .header(Headers.acceptJson()).body(mass).post("/rest/mass/applications/generated");

        UnexpectedResponseException.verifyResponse(response, HttpStatus.SC_OK);

        pushApplications.removeAll(sublist);
    }

}