Example usage for java.util ListIterator nextIndex

List of usage examples for java.util ListIterator nextIndex

Introduction

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

Prototype

int nextIndex();

Source Link

Document

Returns the index of the element that would be returned by a subsequent call to #next .

Usage

From source file:samples.com.axiomine.largecollections.util.TurboListSample.java

public static void workOnTurboList(TurboList<Integer> lst) {
    try {//  www .j a  v  a 2 s . c o m
        ListIterator<Integer> it = lst.listIterator();

        for (int i = 0; i < 10; i++) {
            boolean b = lst.add(i);
            Assert.assertEquals(true, true);
        }
        System.out.println("Size of map=" + lst.size());
        System.out.println("Value for key 0=" + lst.get(0));
        ;
        System.out.println("Now remove key 0");
        try {
            int i = lst.remove(0);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        int i = lst.remove(9);
        System.out.println("Value for deleted key=" + i);
        ;
        System.out.println("Size of map=" + lst.size());

        lst.close();
        boolean b = false;
        try {
            lst.add(9);
        } catch (Exception ex) {
            System.out.println("Exception because acces after close");
            b = true;
        }
        lst.open();
        System.out.println("Open again");
        b = lst.add(9);

        Assert.assertEquals(true, b);
        i = lst.set(9, 99);
        Assert.assertEquals(99, i);
        i = lst.set(5, 55);
        Assert.assertEquals(55, i);
        i = lst.set(0, 100);
        Assert.assertEquals(100, i);
        System.out.println(lst.get(0));
        System.out.println(lst.get(5));
        System.out.println(lst.get(9));
        System.out.println("Now put worked. Size of map should be 10. Size of the map =" + lst.size());

        Iterator<Integer> iter = lst.iterator();
        try {
            while (iter.hasNext()) {
                i = iter.next();
                System.out.println("From ITerator = " + i);
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) iter).close();
        }

        ListIterator<Integer> lstIter = lst.listIterator();
        try {
            while (lstIter.hasNext()) {
                i = lstIter.next();
                System.out.println("From List Iterator = " + i);
                System.out.println("From List Iterator Next Index= " + lstIter.nextIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        lstIter = lst.listIterator(5);
        try {
            while (lstIter.hasNext()) {
                i = lstIter.next();
                System.out.println("From List Iterator = " + i);
                System.out.println("From List Iterator Next Index= " + lstIter.nextIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("---");
        lstIter = lst.listIterator(9);
        try {
            while (lstIter.hasPrevious()) {
                i = lstIter.previous();
                System.out.println("From List Iterator Previous= " + i);
                System.out.println("From List Iterator Previous Index= " + lstIter.previousIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("----------------------------------");
        lstIter = lst.listIterator();
        try {
            while (lstIter.hasNext()) {
                i = lstIter.next();
                System.out.println("Iterating Forward = " + i);
            }

        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("Now Serialize the List");
        File serFile = new File(System.getProperty("java.io.tmpdir") + "/x.ser");
        FileSerDeUtils.serializeToFile(lst, serFile);
        System.out.println("Now De-Serialize the List");
        lst = (TurboList<Integer>) FileSerDeUtils.deserializeFromFile(serFile);
        System.out.println("After De-Serialization " + lst);
        System.out.println("After De-Serialization Size of map should be 10. Size of the map =" + lst.size());
        printListCharacteristics(lst);
        System.out.println("Now calling lst.clear()");
        lst.clear();
        System.out.println("After clear list size should be 0 and =" + lst.size());
        lst.add(0);
        System.out.println("Just added a record and lst size =" + lst.size());
        System.out.println("Finally Destroying");
        lst.destroy();

        System.out.println("Cleanup serialized file");
        FileUtils.deleteQuietly(serFile);

    } catch (Exception ex) {
        throw Throwables.propagate(ex);
    }

}

From source file:Alias2.java

public static void verify(List output, List expected) {
    verifyLength(output.size(), expected.size(), Test.EXACT);
    if (!expected.equals(output)) {
        //find the line of mismatch
        ListIterator it1 = expected.listIterator();
        ListIterator it2 = output.listIterator();
        while (it1.hasNext() && it2.hasNext() && it1.next().equals(it2.next()))
            ;/*  w  w w.  j  a v  a  2 s.c o  m*/
        throw new LineMismatchException(it1.nextIndex(), it1.previous().toString(), it2.previous().toString());
    }
}

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/*from w  ww .j a va  2  s . c o  m*/
 * @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:samples.com.axiomine.largecollections.util.WritableListSample.java

public static void workOnWritableList(WritableList<IntWritable> lst) {
    try {/*w w  w .j a v a  2s.  c  om*/
        ListIterator<Writable> it = lst.listIterator();

        for (int i = 0; i < 10; i++) {
            boolean b = lst.add(new IntWritable(i));
            Assert.assertEquals(true, true);
        }
        System.out.println("Size of map=" + lst.size());
        System.out.println("Value for key 0=" + lst.get(0));
        ;
        System.out.println("Now remove key 0");
        try {
            //int i = lst.remove(0);
            Writable w = lst.remove(0);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        int i = ((IntWritable) lst.remove(9)).get();
        System.out.println("Value for deleted key=" + i);
        ;
        System.out.println("Size of map=" + lst.size());

        lst.close();
        boolean b = false;
        try {
            lst.add(new IntWritable(9));
        } catch (Exception ex) {
            System.out.println("Exception because acces after close");
            b = true;
        }
        lst.open();
        System.out.println("Open again");
        b = lst.add(new IntWritable(9));

        Assert.assertEquals(true, b);
        i = ((IntWritable) lst.set(9, new IntWritable(99))).get();
        Assert.assertEquals(99, i);
        i = ((IntWritable) lst.set(5, new IntWritable(55))).get();
        Assert.assertEquals(55, i);
        i = ((IntWritable) lst.set(0, new IntWritable(100))).get();
        Assert.assertEquals(100, i);
        System.out.println(lst.get(0));
        System.out.println(lst.get(5));
        System.out.println(lst.get(9));
        System.out.println("Now put worked. Size of map should be 10. Size of the map =" + lst.size());

        Iterator<Writable> iter = lst.iterator();
        try {
            while (iter.hasNext()) {
                i = ((IntWritable) iter.next()).get();
                System.out.println("From ITerator = " + i);
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) iter).close();
        }

        ListIterator<Writable> lstIter = lst.listIterator();
        try {
            while (lstIter.hasNext()) {
                i = ((IntWritable) lstIter.next()).get();
                System.out.println("From List Iterator = " + i);
                System.out.println("From List Iterator Next Index= " + lstIter.nextIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        lstIter = lst.listIterator(5);
        try {
            while (lstIter.hasNext()) {
                i = ((IntWritable) lstIter.next()).get();
                System.out.println("From List Iterator = " + i);
                System.out.println("From List Iterator Next Index= " + lstIter.nextIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("---");
        lstIter = lst.listIterator(9);
        try {
            while (lstIter.hasPrevious()) {
                i = ((IntWritable) lstIter.next()).get();
                System.out.println("From List Iterator Previous= " + i);
                System.out.println("From List Iterator Previous Index= " + lstIter.previousIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("----------------------------------");
        lstIter = lst.listIterator();
        try {
            while (lstIter.hasNext()) {
                i = ((IntWritable) lstIter.next()).get();
                System.out.println("Iterating Forward = " + i);
            }

        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("Now Serialize the List");
        File serFile = new File(System.getProperty("java.io.tmpdir") + "/x.ser");
        FileSerDeUtils.serializeToFile(lst, serFile);
        System.out.println("Now De-Serialize the List");
        lst = (WritableList<IntWritable>) FileSerDeUtils.deserializeFromFile(serFile);
        System.out.println("After De-Serialization " + lst);
        System.out.println("After De-Serialization Size of map should be 10. Size of the map =" + lst.size());
        printListCharacteristics(lst);
        System.out.println("Now calling lst.clear()");
        lst.clear();
        System.out.println("After clear list size should be 0 and =" + lst.size());
        lst.add(new IntWritable(0));
        System.out.println("Just added a record and lst size =" + lst.size());
        System.out.println("Finally Destroying");
        lst.destroy();

        System.out.println("Cleanup serialized file");
        FileUtils.deleteQuietly(serFile);

    } catch (Exception ex) {
        throw Throwables.propagate(ex);
    }

}

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 .j  ava2  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:ListOfLists.java

public int lastIndexOf(Object o) {
    ListIterator e = listIterator(size());
    if (o == null) {
        while (e.hasPrevious()) {
            if (e.previous() == null)
                return e.nextIndex();
        }// w w  w . ja va 2s.  c o  m
    } else {
        Object el;
        while (e.hasPrevious()) {
            el = e.previous();
            if (el != null && el.equals(o))
                return e.nextIndex();
        }
    }
    return -1;
}

From source file:io.bibleget.VersionsSelect.java

public VersionsSelect() throws ClassNotFoundException {
    biblegetDB = BibleGetDB.getInstance();
    String bibleVersionsStr = biblegetDB.getMetaData("VERSIONS");
    JsonReader jsonReader = Json.createReader(new StringReader(bibleVersionsStr));
    JsonObject bibleVersionsObj = jsonReader.readObject();
    Set<String> versionsabbrev = bibleVersionsObj.keySet();
    bibleVersions = new BasicEventList<>();
    if (!versionsabbrev.isEmpty()) {
        for (String s : versionsabbrev) {
            String versionStr = bibleVersionsObj.getString(s); //store these in an array
            String[] array;/*from  w w w.j a va 2s .  c o  m*/
            array = versionStr.split("\\|");
            bibleVersions.add(new BibleVersion(s, array[0], array[1],
                    StringUtils.capitalize(new Locale(array[2]).getDisplayLanguage())));
        }
    }

    versionsByLang = new SeparatorList<>(bibleVersions, new VersionComparator(), 1, 1000);

    int listLength = versionsByLang.size();
    enabledFlags = new boolean[listLength];

    ListIterator itr = versionsByLang.listIterator();
    while (itr.hasNext()) {
        int idx = itr.nextIndex();
        Object next = itr.next();
        enabledFlags[idx] = !(next.getClass().getSimpleName().equals("GroupSeparator"));
        if (enabledFlags[idx]) {
            versionCount++;
        } else {
            versionLangs++;
        }
    }

    this.setModel(new DefaultEventListModel<>(versionsByLang));
    this.setCellRenderer(new VersionCellRenderer());
    this.setSelectionModel(new DisabledItemSelectionModel());
}

From source file:org.xwiki.platform.patchservice.plugin.PatchservicePlugin.java

/** This method is inclusive: from and to patches are included */
public List<Patch> getDelta(PatchId fromPatch, PatchId toPatch) {
    List<Patch> patches = getStorage().loadAllDocumentPatchesSince(fromPatch);
    ListIterator<Patch> it = patches.listIterator();
    while (it.hasNext()) {
        if (it.next().getId().equals(toPatch)) {
            break;
        }//  ww w.  ja  va 2 s.  com
    }
    return patches.subList(0, it.nextIndex());
}

From source file:com.mirth.connect.server.util.DatabaseConnection.java

public CachedRowSet executeCachedQuery(String expression, List<Object> parameters) throws SQLException {
    PreparedStatement statement = null;

    try {/*from w w w.ja v  a 2  s .  c o  m*/
        statement = connection.prepareStatement(expression);
        logger.debug("executing prepared statement:\n" + expression);

        ListIterator<Object> iterator = parameters.listIterator();

        while (iterator.hasNext()) {
            int index = iterator.nextIndex() + 1;
            Object value = iterator.next();
            logger.debug("adding parameter: index=" + index + ", value=" + value);
            statement.setObject(index, value);
        }

        ResultSet result = statement.executeQuery();
        CachedRowSetImpl crs = new CachedRowSetImpl();
        crs.populate(result);
        DbUtils.closeQuietly(result);
        return crs;
    } catch (SQLException e) {
        throw e;
    } finally {
        DbUtils.closeQuietly(statement);
    }
}

From source file:com.mirth.connect.server.util.DatabaseConnection.java

public CachedRowSet executeUpdateAndGetGeneratedKeys(String expression, List<Object> parameters)
        throws SQLException {
    PreparedStatement statement = null;

    try {//from w w w .ja v a 2s.c o  m
        statement = connection.prepareStatement(expression, Statement.RETURN_GENERATED_KEYS);
        logger.debug("executing prepared statement:\n" + expression);

        ListIterator<Object> iterator = parameters.listIterator();

        while (iterator.hasNext()) {
            int index = iterator.nextIndex() + 1;
            Object value = iterator.next();
            logger.debug("adding parameter: index=" + index + ", value=" + value);
            statement.setObject(index, value);
        }

        statement.executeUpdate();
        CachedRowSetImpl crs = new CachedRowSetImpl();
        crs.populate(statement.getGeneratedKeys());
        return crs;
    } catch (SQLException e) {
        throw e;
    } finally {
        DbUtils.closeQuietly(statement);
    }
}