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:io.github.retz.web.JobRequestHandler.java

static String listJob(spark.Request req, spark.Response res) throws IOException {
    Optional<AuthHeader> authHeaderValue = WebConsole.getAuthInfo(req);
    LOG.debug("list jobs owned by {}", authHeaderValue.get().key());
    ListJobRequest listJobRequest = MAPPER.readValue(req.body(), ListJobRequest.class);
    LOG.debug("q: state={}, tag={}", listJobRequest.state(), listJobRequest.tag());
    String user = Objects.requireNonNull(authHeaderValue.get().key());
    try {// ww  w  .j a v  a2s  .c  o m
        List<Job> jobs = JobQueue.list(user, listJobRequest.state(), listJobRequest.tag(), MAX_LIST_JOB_SIZE);

        boolean more = false;
        if (jobs.size() > ListJobResponse.MAX_JOB_NUMBER) {
            more = true;
            jobs = jobs.subList(0, ListJobResponse.MAX_JOB_NUMBER);
        }
        ListJobResponse listJobResponse = new ListJobResponse(jobs, more);
        listJobResponse.ok();
        res.status(200);
        res.type("application/json");
        return MAPPER.writeValueAsString(listJobResponse);
    } catch (SQLException e) {
        LOG.error(e.toString(), e);
        res.status(500);
        return "\"Internal Error\"";
    }
}

From source file:ListUtil.java

/**
 * Splits the list./* w w  w  .j  a va2s .  co  m*/
 * 
 * @param <T>
 *            the type of list element
 * @param list
 *            the list
 * @param size
 *            the piece size
 * @return the split lists.
 * @throws NullPointerException
 *             if the list parameter is null
 * @throws IllegalArgumentException
 *             if the size parameter is less than 1
 */
public static <T> List<List<T>> split(List<T> list, int size)
        throws NullPointerException, IllegalArgumentException {
    if (list == null) {
        throw new NullPointerException("The list parameter is null.");
    }
    if (size <= 0) {
        throw new IllegalArgumentException("The size parameter must be more than 0.");
    }
    int num = list.size() / size;
    int mod = list.size() % size;
    List<List<T>> ret = new ArrayList<List<T>>(mod > 0 ? num + 1 : num);
    for (int i = 0; i < num; i++) {
        ret.add(list.subList(i * size, (i + 1) * size));
    }
    if (mod > 0) {
        ret.add(list.subList(num * size, list.size()));
    }
    return ret;
}

From source file:de.tudarmstadt.lt.lm.lucenebased.CountingStringLM.java

static List<String> getNgramHistory(List<String> ngram) {
    assert ngram.size() > 0 : "Ngram must be longer than 0!";
    return ngram.subList(0, ngram.size() - 1);
}

From source file:de.tudarmstadt.lt.lm.lucenebased.CountingStringLM.java

static List<String> getLowerOrderNgram(List<String> ngram) {
    assert ngram.size() > 0 : "Ngram must be longer than 0!";
    return ngram.subList(1, ngram.size());
}

From source file:com.addthis.bundle.util.AutoField.java

public static AutoField newAutoField(List<String> names, boolean parseIndex) {
    checkNotNull(names);//  www. j  a va2s .  c  om
    checkArgument(!names.isEmpty(), "list of field names must not be empty (usually >=2)");
    String name = names.get(0);
    AutoField baseAutoField;
    if (parseIndex) {
        baseAutoField = new IndexField(name);
    } else {
        baseAutoField = new CachingField(name);
    }
    if (names.size() == 1) {
        return baseAutoField;
    } else {
        String[] subNames = names.subList(1, names.size()).toArray(new String[names.size() - 1]);
        return new FullAutoField(baseAutoField, subNames);
    }
}

From source file:Main.java

public static <T> List<T> sub(List<T> list, int start, int end) {
    if (list == null || list.isEmpty()) {
        return new ArrayList<>();
    }/*from  www  .j av a2  s  .c o  m*/

    if (start < 0) {
        start = 0;
    }
    if (end < 0) {
        end = 0;
    }

    if (start > end) {
        int tmp = start;
        start = end;
        end = tmp;
    }

    final int size = list.size();
    if (end > size) {
        if (start >= size) {
            return new ArrayList<>();
        }
        end = size;
    }

    return list.subList(start, end);
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.coreference.CoreferenceFeatures.java

private static String glueCoreferenceLinkTokens(CoreferenceLink link) {
    final int maxTokens = 5;

    List<String> tokens = new ArrayList<>();
    for (Token token : JCasUtil.selectCovered(Token.class, link)) {
        tokens.add(token.getLemma().getCoveredText().toLowerCase());
    }//  w w w  .  j  a  v  a2s  .co m

    tokens = tokens.subList(0, tokens.size() > maxTokens ? maxTokens : tokens.size() - 1);

    return StringUtils.join(tokens, " ");
}

From source file:Main.java

public static <T> List<T> pageList(List<T> list, int page, int size) {
    if (page < 1) {
        throw new IllegalArgumentException("page should not little than 1,page:" + page);
    }/*from   w w  w .j a va2  s  .c o m*/
    if (size < 1) {
        throw new IllegalArgumentException("size should not little than 1,size:" + size);
    }
    if (isEmpty(list)) {
        return Collections.emptyList();
    }
    int totalSize = list.size();

    int fromIndex = (page - 1) * size > (totalSize - 1) ? 0 : (page - 1) * size;

    int endIndex = (fromIndex + size) > (totalSize) ? (totalSize) : (fromIndex + size);

    return list.subList(fromIndex, endIndex);

}

From source file:Main.java

public static <T> List<T> sublist(List<T> result, long startIndex, long size) {
    long endIndex = result.size();
    if (size < endIndex - startIndex) {
        endIndex = startIndex + size;//from   w  ww.j  a v a 2 s  .  c  o m
    }
    if (startIndex > Integer.MAX_VALUE) {
        throw new RuntimeException("StartIndex " + startIndex
                + " is bigger than maxInt. You shouldn't use a InMemoryRawMessagingDao.");
    }
    if (endIndex > Integer.MAX_VALUE) {
        throw new RuntimeException("EndIndex " + endIndex
                + " is bigger than maxInt. You shouldn't use a InMemoryRawMessagingDao.");
    }
    return result.subList(Ints.checkedCast(startIndex), Ints.checkedCast(endIndex));
}

From source file:com.ecbeta.common.util.JSONUtils.java

public static List<JSONObject> limit(List<JSONObject> jsonArray, JSONObject json) {
    if (json != null && !json.isEmpty()) {
        try {//from  ww  w  .ja v  a  2s  . c  om
            int offset = json.getInt("offset");
            int limit = json.getInt("limit");
            jsonArray = jsonArray.subList(offset, Math.min(offset + limit, jsonArray.size()));
        } catch (Exception e) {
            logger.log(Level.INFO, "{0}", e);
        }
    }
    return jsonArray;
}