aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/textserialize/item/ListUtil.java
blob: 72b08de8826588420cbcc59cd293b74e6069e8ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.textserialize.item;

import java.util.*;

/**
 * @author Tony Vaagenes
 */
public class ListUtil {
    public static <T> List<T> rest(List<T> list) {
        return list.subList(1, list.size());
    }

    public static <T> T first(Collection<T> collection) {
        return collection.iterator().next();
    }

    public static boolean firstInstanceOf(Collection<?> collection, @SuppressWarnings("rawtypes") Class c) {
        return !collection.isEmpty() && c.isInstance(first(collection));
    }

    public static <T> List<T> butFirst(List<T> list) {
        return list.subList(1, list.size());
    }

    public static <T> Iterable<T> butFirst(final Collection<T> collection) {
        return () -> {
            Iterator<T> i = collection.iterator();
            i.next();
            return i;
        };
    }
}