aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/collections/Iterables.java
blob: dfbf4024ce4a89006b9c128dfef1183679ab25c7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.collections;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author jonmv
 */
public class Iterables {

    private Iterables() { }

    /** Returns a reverse-order iterable view of the given elements. */
    public static <T> Iterable<T> reversed(List<T> elements) {
        return () -> new Iterator<T>() {
            final ListIterator<T> wrapped = elements.listIterator(elements.size());
            @Override public boolean hasNext() { return wrapped.hasPrevious(); }
            @Override public T next() { return wrapped.previous(); }
            @Override public void remove() { wrapped.remove(); }
        };
    }

}