Example usage for java.util ListIterator previous

List of usage examples for java.util ListIterator previous

Introduction

In this page you can find the example usage for java.util ListIterator previous.

Prototype

E previous();

Source Link

Document

Returns the previous element in the list and moves the cursor position backwards.

Usage

From source file:org.mule.util.ExceptionUtils.java

/**
 * This method returns the throwable closest to the root cause that matches the
 * specified class or subclass. Any null argument will make the method return
 * null./*from   w w w.  j a v  a  2s. com*/
 * 
 * @param throwable the throwable to inspect, may be null
 * @param type the type to search for, subclasses match, null returns null
 * @return the throwablethat is closest to the root in the throwable chain that
 *         matches the type or subclass of that type.
 */
public static Throwable getDeepestOccurenceOfType(Throwable throwable, Class<?> type) {
    if (throwable == null || type == null) {
        return null;
    }
    @SuppressWarnings("unchecked")
    List<Throwable> throwableList = getThrowableList(throwable);
    ListIterator<Throwable> listIterator = throwableList.listIterator(throwableList.size());
    while (listIterator.hasPrevious()) {
        Throwable candidate = listIterator.previous();
        if (type.isAssignableFrom(candidate.getClass())) {
            return candidate;
        }
    }
    return null;
}

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.// w  w w . ja  v a  2  s.  co 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:Main.java

public static <T> Iterator<T> getReverseIterator(final ListIterator<T> iter) {
    return new Iterator<T>() {
        public boolean hasNext() {
            return iter.hasPrevious();
        }//from  ww w .j a  v  a2 s  .  co  m

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

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

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)));
        }//  ww  w  .  j  av  a2s  . c om
    } 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);
        }
    }
}

From source file:Main.java

public static <E> void swapAll(final List<E> list) {
    if (list instanceof RandomAccess) {
        for (int i = 0, j = list.size() - 1; i < j; i++, j--) {
            list.set(i, list.set(j, list.get(i)));
        }//from www  . jav  a 2 s.c  o m
    } else {
        final ListIterator<E> iteratorI = list.listIterator();
        final ListIterator<E> iteratorJ = list.listIterator(list.size());
        E tmp;

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

From source file:Main.java

/**
 * Returns the indexth element from the list by using the list iterator to navigate.
 * @param <T> the element type/*from  ww w.j  a  v a 2s  .co m*/
 * @param it the list iterator
 * @param index the target index
 * @return the value
 */
private static <T> T get(ListIterator<? extends T> it, int index) {
    T obj = null;
    int pos = it.nextIndex();
    if (pos <= index) {
        do {
            obj = it.next();
        } while (pos++ < index);
    } else {
        do {
            obj = it.previous();
        } while (--pos > index);
    }
    return obj;
}

From source file:com.thruzero.common.core.locator.LocatorUtils.java

/**
 * For a given childClass, start at the root base class and read the sections of each sub-class, walking down the class hierarchy
 * to the given childClass. The result is that all parameters from all inherited sections, including the given childClass,
 * are read with each subclass overwriting the common keys of any parent section. If given, optionalBaseInterface will be used as the
 * root base and placed at the top of the inheritance stack.
 *
 * @param sectionName//from  w  w w  .  j  a  va 2s.c  o  m
 * @param initStrategy
 * @param childClass
 */
public static StringMap getInheritedParameters(final InitializationStrategy initStrategy,
        final Class<?> childClass, final Class<?> optionalBaseInterface) {
    StringMap result = new StringMap();
    List<Class<?>> inheritanceList = new ArrayList<Class<?>>();

    Class<?> nextClass = childClass;
    while (nextClass != null) {
        inheritanceList.add(nextClass);
        nextClass = nextClass.getSuperclass();
    }
    inheritanceList.remove(inheritanceList.size() - 1); // remove java.lang.Object

    if (optionalBaseInterface != null) {
        inheritanceList.add(optionalBaseInterface);
    }

    ListIterator<Class<?>> iter = inheritanceList.listIterator(inheritanceList.size());
    StringMap params;
    while (iter.hasPrevious()) {
        params = initStrategy.getSectionAsStringMap(iter.previous().getName());
        if (params != null) {
            result.putAll(params);
        }
    }

    return result;
}

From source file:Main.java

/**
 * Returns a list iterator that swaps all previous/next calls.
 * <p><b>Important:</b> The returned iterator violates the {@link ListIterator#nextIndex()} and {@link ListIterator#previousIndex()} specifications.
 *//*from  w  w w.j a v a2 s. c om*/
public static <E> ListIterator<E> reverse(ListIterator<E> iterator) {
    return new ListIterator<E>() {
        @Override
        public boolean hasNext() {
            return iterator.hasPrevious();
        }

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

        @Override
        public boolean hasPrevious() {
            return iterator.hasNext();
        }

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

        @Override
        public int nextIndex() {
            return iterator.previousIndex();
        }

        @Override
        public int previousIndex() {
            return iterator.nextIndex();
        }

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

        @Override
        public void set(E e) {
            iterator.set(e);
        }

        @Override
        public void add(E e) {
            iterator.add(e);
        }
    };
}

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();
                }//  w w  w .j a va  2  s .  c  o m

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

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

From source file:org.synku4j.wbxml.util.WbxmlUtil.java

public static Map<Integer, Field> getFieldsMappedByToken(final Object source) {
    final Map<Integer, Field> fieldMap = new HashMap<Integer, Field>();

    if (source == null) {
        throw new NullPointerException("source object cannot be null");
    }//from ww  w  .  j a  v  a 2s . c o  m

    final ArrayList<Class<?>> heirachy = new ArrayList<Class<?>>();
    Class<? extends Object> clazz = (source instanceof Class<?>) ? (Class<?>) source : source.getClass();
    do {
        heirachy.add(clazz);
        clazz = clazz.getSuperclass();
    } while (!Object.class.equals(clazz));

    // from the bottom up scan.
    final ListIterator<Class<?>> liter = heirachy.listIterator(heirachy.size());
    while (liter.hasPrevious()) {
        clazz = liter.previous();
        for (Field field : clazz.getDeclaredFields()) {
            final WbxmlField wbxmlField = field.getAnnotation(WbxmlField.class);
            if (wbxmlField != null) {
                fieldMap.put(wbxmlField.index(), field);
            }
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("Object class :" + source + " has (" + fieldMap.size() + ") annotated fields");
    }

    return fieldMap;
}