Example usage for java.util ListIterator hasPrevious

List of usage examples for java.util ListIterator hasPrevious

Introduction

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

Prototype

boolean hasPrevious();

Source Link

Document

Returns true if this list iterator has more elements when traversing the list in the reverse direction.

Usage

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 ww  w .  java  2  s . c om*/
 * @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 ww  .  ja  va2 s . c o m
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 <E> Iterable<E> reverse(final List<E> list) {
    return new Iterable<E>() {
        @Override/* w  ww.jav  a2s  .co m*/
        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: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");
    }/*w w w . j  a v a2 s.  c  om*/

    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;
}

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

/**
 * Get the fields from the source object which are annotated with the
 * <code>WbxmlField</code> class.
 * /* www  .  j  av  a 2 s. c  om*/
 * Note, this search from the bottom up, which means fields in the base class
 * are found before those in the derived class.
 * 
 * NOTE: We should do some name clashing checks here, duplicate fields in classes
 *       will more than likely screw up the marshaling.
 * 
 * @param source the source object.
 * @return the annotated fields.
 */
public static Field[] getFields(final Object source) {
    if (source == null) {
        throw new NullPointerException("source object cannot be null");
    }

    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));

    final List<Field> fields = new LinkedList<Field>();

    // 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) {
                fields.add(field);
            }
        }
    }

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

    return fields.toArray(new Field[0]);
}

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  w w  .  j  a  v a  2 s  .co m*/
    } else if (p instanceof ArrayProjector) {
        if (field.matches(((ArrayProjector) p).getArrayFieldPattern())) {
            return (ArrayProjector) p;
        }
        return findArrayProjectorForField(p.getNestedProjector(), field);
    }
    return null;
}

From source file:Main.java

/**
 * Wraps an <code>ListIterator</code> and returns a <code>ListIterator</code>
 * that cannot modify the underlying list.
 * All methods that could be used to modify the list throw
 * <code>UnsupportedOperationException</code>
 * @param underlying original list iterator
 * @param <T> element type//w  ww  .j av a2  s . c  om
 * @return unmodifiable list iterator
 */
@Nonnull
public static <T> ListIterator<T> unmodifiableListIterator(final @Nonnull ListIterator<T> underlying) {
    return new ListIterator<T>() {
        public boolean hasNext() {
            return underlying.hasNext();
        }

        public T next() {
            return underlying.next();
        }

        public boolean hasPrevious() {
            return underlying.hasPrevious();
        }

        public T previous() {
            return underlying.previous();
        }

        public int nextIndex() {
            return underlying.nextIndex();
        }

        public int previousIndex() {
            return underlying.previousIndex();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void set(T t) {
            throw new UnsupportedOperationException();
        }

        public void add(T t) {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.apache.cocoon.forms.util.DomHelper.java

public static Map getInheritedNSDeclarations(Element elm) {
    List ancestorsAndSelf = new LinkedList();
    Element current = elm;/*ww  w. ja v  a  2 s . co  m*/
    while (current != null) {
        ancestorsAndSelf.add(current);
        Node parent = current.getParentNode();
        if (parent.getNodeType() == Node.ELEMENT_NODE)
            current = (Element) parent;
        else
            current = null;
    }

    Map nsDeclarations = null;
    ListIterator iter = ancestorsAndSelf.listIterator(ancestorsAndSelf.size());
    while (iter.hasPrevious()) {
        Element element = (Element) iter.previous();
        nsDeclarations = addLocalNSDeclarations(element, nsDeclarations);
    }

    return nsDeclarations;
}

From source file:org.apache.xml.security.keys.keyresolver.implementations.RetrievalMethodResolver.java

private static Element getDocumentElement(Set<Node> set) {
    Iterator<Node> it = set.iterator();
    Element e = null;//from   www  .  j a  va 2 s.  c  o  m
    while (it.hasNext()) {
        Node currentNode = it.next();
        if (currentNode != null && Node.ELEMENT_NODE == currentNode.getNodeType()) {
            e = (Element) currentNode;
            break;
        }
    }
    List<Node> parents = new ArrayList<Node>();

    // Obtain all the parents of the elemnt
    while (e != null) {
        parents.add(e);
        Node n = e.getParentNode();
        if (n == null || Node.ELEMENT_NODE != n.getNodeType()) {
            break;
        }
        e = (Element) n;
    }
    // Visit them in reverse order.
    ListIterator<Node> it2 = parents.listIterator(parents.size() - 1);
    Element ele = null;
    while (it2.hasPrevious()) {
        ele = (Element) it2.previous();
        if (set.contains(ele)) {
            return ele;
        }
    }
    return null;
}

From source file:com.offbynull.coroutines.instrumenter.asm.SearchUtils.java

/**
 * Find line number associated with an instruction.
 * @param insnList instruction list for method
 * @param insnNode instruction within method being searched against
 * @throws NullPointerException if any argument is {@code null} or contains {@code null}
 * @throws IllegalArgumentException if arguments aren't all from the same method
 * @return line number node associated with the instruction, or {@code null} if no line number exists
 *///  ww w.  j a va  2  s  .  c  om
public static LineNumberNode findLineNumberForInstruction(InsnList insnList, AbstractInsnNode insnNode) {
    Validate.notNull(insnList);
    Validate.notNull(insnNode);

    int idx = insnList.indexOf(insnNode);
    Validate.isTrue(idx != -1);

    // Get index of labels and insnNode within method
    ListIterator<AbstractInsnNode> insnIt = insnList.iterator(idx);
    while (insnIt.hasPrevious()) {
        AbstractInsnNode node = insnIt.previous();

        if (node instanceof LineNumberNode) {
            return (LineNumberNode) node;
        }
    }

    return null;
}