Example usage for java.util List listIterator

List of usage examples for java.util List listIterator

Introduction

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

Prototype

ListIterator<E> listIterator(int index);

Source Link

Document

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Usage

From source file:com.nettyhttpserver.server.NettyChannelTrafficShapingHandler.java

/**
 * // w w  w.j a v  a2s.co  m
 * @return the backwards (from the last element) iterator of the list
 */
public static ListIterator<ConnectionInfoDTO> getServerConnectionListIterator() {
    /*
     * The list copy is returned so no Concurrency exceptions will be thrown
     * wgile iterating over changing list.
     */
    List<ConnectionInfoDTO> list = new ArrayList<>(serverConnectionList);
    return list.listIterator(list.size());
}

From source file:Main.java

public static <T> Iterable<T> reverse(final List<T> list) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            final ListIterator<T> listIterator = list.listIterator(list.size());

            return new Iterator<T>() {
                public boolean hasNext() {
                    return listIterator.hasPrevious();
                }//from   w w w  . j  ava  2  s. c o m

                public T next() {
                    return listIterator.previous();
                }

                public void remove() {
                    listIterator.remove();
                }
            };
        }
    };
}

From source file:Main.java

/**
 * Gets the reverse iterable of an Iterable (i.e. a collection).
 * If this collection implements ListIterator, then this operation is far more efficent.
 * Otherwise, the iterable is iterated through, where the results are stored, before
 * returning a iterable that iterates through the resulting list.
 * /*from w  w  w. j a va  2  s. c  o  m*/
 * @param it
 * @return
 */
public static <T> Iterable<T> getReverse(Iterable<T> it) {
    if (it instanceof List) { //list takes up less computation and memory - use built in list iterator
        final ListIterator<T> iter = ((List<T>) it).listIterator(((List<T>) it).size());
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                return getReverseIterator(iter);
            }
        };
    }

    int size = 10;
    if (it instanceof Collection) { //if not a list, but is a collection
        size = ((Collection<T>) it).size();
    }
    List<T> list = new Vector<>(size);
    for (T t : it) {
        list.add(t);
    }
    final ListIterator<T> iter = list.listIterator(list.size());

    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return getReverseIterator(iter);
        }
    };
}

From source file:Main.java

public static <E> Iterable<E> reverse(final List<E> list) {
    return new Iterable<E>() {
        @Override/*from w  w w.j av a 2 s.c om*/
        public Iterator<E> iterator() {
            final ListIterator<E> it = list.listIterator(list.size());

            return new Iterator<E>() {
                @Override
                public boolean hasNext() {
                    return it.hasPrevious();
                }

                @Override
                public E next() {
                    return it.previous();
                }

                @Override
                public void remove() {
                    it.remove();
                }
            };
        }
    };
}

From source file:Main.java

public static <E> void swap(final List<E> list, final int i, final int j) {
    if (list instanceof RandomAccess) {
        list.set(i, list.set(j, list.get(i)));
    } else {//from   www  .ja va  2  s.  c  o  m
        final ListIterator<E> iterator = list.listIterator(i);
        iterator.set(list.set(j, iterator.next()));
    }
}

From source file:org.grails.datastore.mapping.query.order.ManualEntityOrdering.java

/**
 * Reverses the list.  The result is a new List with the identical contents
 * in reverse order./*from ww w . java 2 s  .  c  o m*/
 *
 * @param list a List
 * @return a reversed List
 */
private static List reverse(List list) {
    int size = list.size();
    List answer = new ArrayList(size);
    ListIterator iter = list.listIterator(size);
    while (iter.hasPrevious()) {
        answer.add(iter.previous());
    }
    return answer;
}

From source file:org.apache.hadoop.hbase.master.assignment.AssignmentManagerUtil.java

private static void unlock(List<RegionStateNode> regionNodes) {
    for (ListIterator<RegionStateNode> iter = regionNodes.listIterator(regionNodes.size()); iter
            .hasPrevious();) {/*  ww w .  j  a  v  a  2 s  . c  om*/
        iter.previous().unlock();
    }
}

From source file:com.redhat.lightblue.eval.Projector.java

private static ArrayProjector findArrayProjectorForField(Projector p, Path field) {
    if (p instanceof ListProjector) {
        List<Projector> items = ((ListProjector) p).getItems();
        ListIterator<Projector> itemsItr = items.listIterator(items.size());
        while (itemsItr.hasPrevious()) {
            Projector projector = itemsItr.previous();
            ArrayProjector x = findArrayProjectorForField(projector, field);
            if (x != null)
                return x;
        }//w  ww.j a  v a2 s.  c  om
    } else if (p instanceof ArrayProjector) {
        if (field.matches(((ArrayProjector) p).getArrayFieldPattern())) {
            return (ArrayProjector) p;
        }
        return findArrayProjectorForField(p.getNestedProjector(), field);
    }
    return null;
}

From source file:org.apache.axis2.transport.testkit.tests.TestResourceSet.java

static void tearDown(List<TestResource> resources) throws Exception {
    resources = filterOnHasLifecycle(resources);
    if (!resources.isEmpty()) {
        log.info("Tearing down: " + resources);
        for (ListIterator<TestResource> it = resources.listIterator(resources.size()); it.hasPrevious();) {
            it.previous().tearDown();//from  ww  w  . java2  s.  c o m
        }
    }
}

From source file:Main.java

public static <E> void swapAll(final List<E> list, final int off, final int len) {
    if (list instanceof RandomAccess) {
        final int last = off + len - 1;
        for (int i = off, j = last; i < j; i++, j--) {
            list.set(i, list.set(j, list.get(i)));
        }/*from   ww  w .  ja va 2  s  .  c o m*/
    } else {
        final int end = off + len;
        final ListIterator<E> iteratorI = list.listIterator(off);
        final ListIterator<E> iteratorJ = list.listIterator(end);
        E tmp;

        while (iteratorI.nextIndex() < iteratorJ.nextIndex()) {
            tmp = iteratorI.next();
            iteratorI.set(iteratorJ.previous());
            iteratorJ.set(tmp);
        }
    }
}