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:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Oracle");
    list.add("SQL");
    list.add("CSS");
    list.add("XML");
    System.out.println("List: " + list);
    // Get the list iterator
    ListIterator<String> iterator = list.listIterator();
    while (iterator.hasNext()) {
        int index = iterator.nextIndex();
        String element = iterator.next();
        System.out.println("Index=" + index + ", Element=" + element);
    }//w w  w .jav a2  s  .  c  o m
    // Reuse the iterator to iterate from the end to the beginning
    while (iterator.hasPrevious()) {
        int index = iterator.previousIndex();
        String element = iterator.previous();
        System.out.println("Index=" + index + ",  Element=" + element);
    }
}

From source file:IteratorDemo.java

public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    al.add("C");/*  w  w  w.j  a  v a  2s  . co  m*/
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
        String element = litr.next();
        litr.set(element + "+");
    }

    itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    while (litr.hasPrevious()) {
        String element = litr.previous();
        System.out.print(element + " ");
    }
}

From source file:org.deegree.tools.alkis.BackReferenceFixer.java

public static void main(String[] args) {
    Options opts = initOptions();/*from   w w w  .  ja v  a2 s . c o m*/
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        CommandLine line = new PosixParser().parse(opts, args);
        String input = line.getOptionValue('i');
        String output = line.getOptionValue('o');
        String schema = line.getOptionValue('s');
        fis = new FileInputStream(input);
        fos = new FileOutputStream(output);
        XMLInputFactory xifac = XMLInputFactory.newInstance();
        XMLOutputFactory xofac = XMLOutputFactory.newInstance();
        XMLStreamReader xreader = xifac.createXMLStreamReader(input, fis);
        IndentingXMLStreamWriter xwriter = new IndentingXMLStreamWriter(xofac.createXMLStreamWriter(fos));
        GMLStreamReader reader = GMLInputFactory.createGMLStreamReader(GMLVersion.GML_32, xreader);

        AppSchema appSchema = new GMLAppSchemaReader(null, null, schema).extractAppSchema();
        reader.setApplicationSchema(appSchema);

        GMLStreamWriter writer = GMLOutputFactory.createGMLStreamWriter(GMLVersion.GML_32, xwriter);
        XlinkedObjectsHandler handler = new XlinkedObjectsHandler(true, null, new GmlXlinkOptions());
        writer.setReferenceResolveStrategy(handler);

        QName prop = new QName(ns601, "dientZurDarstellungVon");

        Map<String, List<String>> refs = new HashMap<String, List<String>>();
        Map<String, List<String>> types = new HashMap<String, List<String>>();
        Map<String, String> bindings = null;

        for (Feature f : reader.readFeatureCollectionStream()) {
            if (bindings == null) {
                bindings = f.getType().getSchema().getNamespaceBindings();
            }
            for (Property p : f.getProperties(prop)) {
                FeatureReference ref = (FeatureReference) p.getValue();
                List<String> list = refs.get(ref.getId());
                if (list == null) {
                    list = new ArrayList<String>();
                    refs.put(ref.getId(), list);
                }
                list.add(f.getId());
                list = types.get(ref.getId());
                if (list == null) {
                    list = new ArrayList<String>();
                    types.put(ref.getId(), list);
                }
                list.add("inversZu_dientZurDarstellungVon_" + f.getType().getName().getLocalPart());
            }
        }

        QName[] inversePropNames = new QName[] {
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_Darstellung"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_LTO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_PTO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_FPO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_KPO_3D"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_LPO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_PPO") };

        reader.close();
        fis.close();
        writer.setNamespaceBindings(bindings);

        fis = new FileInputStream(input);
        xreader = xifac.createXMLStreamReader(input, fis);
        reader = GMLInputFactory.createGMLStreamReader(GMLVersion.GML_32, xreader);
        reader.setApplicationSchema(appSchema);

        if (bindings != null) {
            for (Map.Entry<String, String> e : bindings.entrySet()) {
                if (!e.getKey().isEmpty()) {
                    xwriter.setPrefix(e.getValue(), e.getKey());
                }
            }
        }
        xwriter.writeStartDocument();
        xwriter.setPrefix("gml", "http://www.opengis.net/gml/3.2");
        xwriter.writeStartElement("http://www.opengis.net/gml/3.2", "FeatureCollection");
        xwriter.writeNamespace("gml", "http://www.opengis.net/gml/3.2");

        GmlDocumentIdContext ctx = new GmlDocumentIdContext(GMLVersion.GML_32);

        for (Feature f : reader.readFeatureCollectionStream()) {
            if (refs.containsKey(f.getId())) {
                List<Property> props = new ArrayList<Property>(f.getProperties());
                ListIterator<Property> iter = props.listIterator();
                String name = iter.next().getName().getLocalPart();
                while (name.equals("lebenszeitintervall") || name.equals("modellart") || name.equals("anlass")
                        || name.equals("zeigtAufExternes") || name.equals("istTeilVon")
                        || name.equals("identifier")) {
                    if (iter.hasNext()) {
                        name = iter.next().getName().getLocalPart();
                    } else {
                        break;
                    }
                }
                if (iter.hasPrevious()) {
                    iter.previous();
                }
                for (QName propName : inversePropNames) {
                    Iterator<String> idIter = refs.get(f.getId()).iterator();
                    Iterator<String> typeIter = types.get(f.getId()).iterator();
                    while (idIter.hasNext()) {
                        String id = idIter.next();
                        if (typeIter.next().equals(propName.getLocalPart())) {
                            PropertyType pt = f.getType().getPropertyDeclaration(propName);
                            Property p = new GenericProperty(pt, new FeatureReference(ctx, "#" + id, null));
                            iter.add(p);
                        }
                    }
                }
                f.setProperties(props);

            }
            xwriter.writeStartElement("http://www.opengis.net/gml/3.2", "featureMember");
            writer.write(f);
            xwriter.writeEndElement();
        }

        xwriter.writeEndElement();
        xwriter.close();
    } catch (Throwable e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

/**
 * Returns the last non-null element in the given list; or, if
 * there is no non-null element, it returns null.
 *
 * @param l The list. It may be null, in which case null is
 * returned.//  w w w.  j a  v a 2  s.  c o  m
 *
 * @return The element.
 */

public static <E> E getLastNonNull(List<E> l) {
    if (l == null) {
        return null;
    }
    ListIterator<E> i = l.listIterator(l.size());
    while (i.hasPrevious()) {
        E e = i.previous();
        if (e != null) {
            return e;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Remove all null elements at the end of the list.
 *///from   www  . ja  v a2  s .c om
public static <T> void trimTail(List<T> l) {
    final ListIterator<T> it = l.listIterator(l.size());
    while (it.hasPrevious()) {
        if (it.previous() != null)
            return;
        it.remove();
    }
}

From source file:Main.java

private static <T> T findPrevious(ListIterator<T> iter) {
    T prev = null;/*from w w  w .  ja  v  a  2s  .  co m*/
    iter.previous(); // rewind
    if (iter.hasPrevious()) {
        prev = iter.previous();
        iter.next(); // come back
    }
    iter.next(); // come back
    return prev;
}

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.// w  w  w  . j a va 2  s  . c o m
 * 
 * @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:Main.java

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

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

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

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.//ww  w.j a v a 2  s. com
 *
 * @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> 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();
                }//  ww w.j  a  v a2  s  . co m

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

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