Example usage for java.util ListIterator hasNext

List of usage examples for java.util ListIterator hasNext

Introduction

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

Prototype

boolean hasNext();

Source Link

Document

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

Usage

From source file:org.apache.cxf.cwiki.SiteExporter.java

public static void main(String[] args) throws Exception {
    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password.toCharArray());
        }/*from   ww w  .ja  va2s.c  o m*/
    });
    ListIterator<String> it = Arrays.asList(args).listIterator();
    List<String> files = new ArrayList<String>();
    boolean forceAll = false;
    int maxThreads = -1;
    while (it.hasNext()) {
        String s = it.next();
        if ("-debug".equals(s)) {
            debug = true;
        } else if ("-user".equals(s)) {
            userName = it.next();
        } else if ("-password".equals(s)) {
            password = it.next();
        } else if ("-d".equals(s)) {
            rootOutputDir = new File(it.next());
        } else if ("-force".equals(s)) {
            forceAll = true;
        } else if ("-svn".equals(s)) {
            svn = true;
        } else if ("-commit".equals(s)) {
            commit = true;
        } else if ("-maxThreads".equals(s)) {
            maxThreads = Integer.parseInt(it.next());
        } else if (s != null && s.length() > 0) {
            files.add(s);
        }
    }

    List<SiteExporter> exporters = new ArrayList<SiteExporter>();
    for (String file : files) {
        exporters.add(new SiteExporter(file, forceAll));
    }
    List<SiteExporter> modified = new ArrayList<SiteExporter>();
    for (SiteExporter exporter : exporters) {
        if (exporter.initialize()) {
            modified.add(exporter);
        }
    }

    // render stuff only if needed
    if (!modified.isEmpty()) {
        setSiteExporters(exporters);

        if (maxThreads <= 0) {
            maxThreads = modified.size();
        }

        ExecutorService executor = Executors.newFixedThreadPool(maxThreads, new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
            }
        });
        List<Future<?>> futures = new ArrayList<Future<?>>(modified.size());
        for (SiteExporter exporter : modified) {
            futures.add(executor.submit(exporter));
        }
        for (Future<?> t : futures) {
            t.get();
        }
    }

    if (commit) {
        File file = FileUtils.createTempFile("svncommit", "txt");
        FileWriter writer = new FileWriter(file);
        writer.write(svnCommitMessage.toString());
        writer.close();
        callSvn(rootOutputDir, "commit", "-F", file.getAbsolutePath(), rootOutputDir.getAbsolutePath());
        svnCommitMessage.setLength(0);
    }
}

From source file:Main.java

public static <E> int indexOf(List<E> c, E item, BiPredicate<? super E, ? super E> equalTester) {
    ListIterator<E> iter = c.listIterator();
    while (iter.hasNext()) {
        int index = iter.nextIndex();
        if (equalTester.test(iter.next(), item)) {
            return index;
        }//  w  w  w .  j a  v a 2 s. c o  m
    }
    return -1;
}

From source file:Main.java

public static <T> void symmetricSort(List<T> list) {
    @SuppressWarnings("unchecked")
    T[] array = (T[]) new Object[list.size()];
    for (int i = 0; i < list.size(); i++) {
        if (i % 2 == 0) {
            array[(list.size() - i) / 2 + (list.size() % 2 - 1)] = list.get(i);
        } else {/*  ww w .  jav  a 2 s  .  com*/
            array[(list.size() + i) / 2] = list.get(i);
        }
    }

    int j = 0;
    ListIterator<T> it = list.listIterator();
    while (it.hasNext()) {
        it.next();
        it.set(array[j++]);
    }
}

From source file:Main.java

public static <E> E setInSortedList(List<E> list, int index, E item, Comparator<? super E> comparator,
        BiPredicate<? super E, ? super E> distincter) {
    if (distincter != null) {
        ListIterator<E> iter = list.listIterator();
        while (iter.hasNext()) {
            int currIndex = iter.nextIndex();
            E currItem = iter.next();/*from w w  w. ja v  a2s. c  om*/
            if (index != currIndex && distincter.test(currItem, item)) {
                return null;
            }
        }
    }
    E previousItem = list.set(index, item);
    list.sort(comparator);
    return previousItem;
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.fields.FieldUtils.java

public static List<Individual> removeIndividualsAlreadyInRange(List<Individual> individuals,
        List<ObjectPropertyStatement> stmts, String predicateUri, String objectUriBeingEdited) {
    HashSet<String> range = new HashSet<String>();

    for (ObjectPropertyStatement ops : stmts) {
        if (ops.getPropertyURI().equals(predicateUri))
            range.add(ops.getObjectURI());
    }/*from www .ja va 2 s  . c om*/

    int removeCount = 0;
    ListIterator<Individual> it = individuals.listIterator();
    while (it.hasNext()) {
        Individual ind = it.next();
        if (range.contains(ind.getURI()) && !(ind.getURI().equals(objectUriBeingEdited))) {
            it.remove();
            ++removeCount;
        }
    }

    return individuals;
}

From source file:Main.java

/**
 * Keeps and returns the original list.//from w ww  .  ja  v a 2  s  .c o m
 * 
 * @param <T>
 * @param list
 * @return removes any duplicates in the list. Keeps the first occurrence of duplicates.
 */
public static <T> List<T> removeDuplicates(List<T> list) {
    HashSet<T> set = new HashSet<T>(list.size());
    ListIterator<T> i = list.listIterator();
    while (i.hasNext()) {
        T cur = i.next();
        if (set.contains(cur)) {
            i.remove();
        } else {
            set.add(cur);
        }
    }

    return list;
}

From source file:Main.java

/**
 * Removed null elements in-place./*from w  w  w  .j av a2s.  c  o  m*/
 * 
 * @param <T>
 * @param col
 */
public static <T> void clearNulls(List<T> col) {
    ListIterator<T> iter = col.listIterator();
    while (iter.hasNext()) {
        if (iter.next() == null) {
            iter.remove();
        }
    }
}

From source file:Main.java

private static <T> T findNext(ListIterator<T> iter) {
    T next = null;/*from w  w w.j av  a  2 s.c  o m*/
    if (iter.hasNext()) {
        next = iter.next();
        iter.previous(); // come back
    }
    return next;
}

From source file:Main.java

public static <E> boolean addToSortedList(List<E> list, E item, Comparator<? super E> comparator,
        BiPredicate<? super E, ? super E> distincter) {
    int addIndex = list.size();
    boolean foundAddIndex = false;
    ListIterator<E> iter = list.listIterator();
    while (iter.hasNext()) {
        int currIndex = iter.nextIndex();
        E currItem = iter.next();/*w ww .j av a  2 s .co m*/
        if (distincter != null && distincter.test(currItem, item)) {
            return false;
        }
        if (!foundAddIndex) {
            int comparison = comparator.compare(currItem, item);
            if (comparison > 0) {
                addIndex = currIndex;
                // if we don't have to check the remaining items for distinct we can break the loop now
                if (distincter == null) {
                    break;
                }
                foundAddIndex = true;
            } else if (comparison == 0) {
                addIndex = currIndex + 1;
                // if we don't have to check the remaining items for distinct we can break the loop now
                if (distincter == null) {
                    break;
                }
                foundAddIndex = true;
            }
        }
    }
    list.add(addIndex, item);
    return true;
}

From source file:Main.java

/**
 *  Resizes the passed list to N entries. Will add the specified object if
 *  the list is smaller than the desired size, discard trailing entries if
 *  larger. This is primarily used to presize a list that will be accessed
 *  by index, but it may also be used to truncate a list for display.
 *
 *  @return The list, as a convenience for callers
 *
 *  @throws UnsupportedOperationException if the list does not implement
 *          <code>RandomAccess</code> and its list iterator does not
 *          support the <code>remove()</code> operation
 */// www  . ja v a2s.c om
public static <T> List<T> resize(List<T> list, int newSize, T obj) {
    if (list instanceof ArrayList)
        ((ArrayList<T>) list).ensureCapacity(newSize);

    if (list.size() < newSize) {
        for (int ii = list.size(); ii < newSize; ii++)
            list.add(obj);
    } else if (list.size() > newSize) {
        if (list instanceof RandomAccess) {
            for (int ii = list.size() - 1; ii >= newSize; ii--)
                list.remove(ii);
        } else {
            ListIterator<T> itx = list.listIterator(newSize);
            while (itx.hasNext()) {
                itx.next();
                itx.remove();
            }
        }
    }

    return list;
}