Example usage for java.util List listIterator

List of usage examples for java.util List listIterator

Introduction

In this page you can find the example usage for java.util List listIterator.

Prototype

ListIterator<E> listIterator(int index);

Source Link

Document

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Usage

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
 *//*  w  w w.j  a  v a2 s. co  m*/
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;
}

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// w w  w. j  a  v a  2 s. co 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:org.apache.xml.security.keys.keyresolver.implementations.RetrievalMethodResolver.java

private static Element getDocumentElement(Set<Node> set) {
    Iterator<Node> it = set.iterator();
    Element e = null;/* w w  w . ja v a  2s  .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:org.apache.cocoon.forms.util.DomHelper.java

public static Map getInheritedNSDeclarations(Element elm) {
    List ancestorsAndSelf = new LinkedList();
    Element current = elm;/*w ww. j av a2  s . c om*/
    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:jenkins.scm.impl.subversion.SubversionSCMSource.java

/**
 * Returns the index of the next wildcard segment on or after the specified start index.
 *
 * @param pathSegment the path segments.
 * @param startIndex  the first index to check.
 * @return the index with a wildcard or {@code -1} if none exist.
 *///from www.j  a  v  a2 s.com
static int indexOfNextWildcard(@NonNull List<String> pathSegment, int startIndex) {
    int index = startIndex;
    ListIterator<String> i = pathSegment.listIterator(index);
    while (i.hasNext()) {
        String segment = i.next();
        if (segment.indexOf('*') != -1 || segment.indexOf('?') != -1) {
            break;
        }
        index++;
    }
    return index;
}

From source file:com.ecyrd.jspwiki.util.ProviderConverter.java

protected void convert() throws WikiException, IOException {
    Properties props = new Properties();

    props.setProperty(WikiEngine.PROP_APPNAME, "JSPWikiConvert");
    props.setProperty(AbstractFileProvider.PROP_PAGEDIR, m_rcsSourceDir);
    props.setProperty(PageManager.PROP_PAGEPROVIDER, "RCSFileProvider");
    props.setProperty(PageManager.PROP_USECACHE, "false");
    props.setProperty("log4j.appender.outlog", "org.apache.log4j.ConsoleAppender");
    props.setProperty("log4j.appender.outlog.layout", "org.apache.log4j.PatternLayout");
    props.setProperty("jspwiki.useLucene", "false");
    props.setProperty("log4j.rootCategory", "INFO,outlog");
    WikiEngine engine = new WikiEngine(props);

    WikiPageProvider sourceProvider = engine.getPageManager().getProvider();

    File tmpDir = new File(SystemUtils.JAVA_IO_TMPDIR, "converter-tmp");

    props.setProperty(AbstractFileProvider.PROP_PAGEDIR, tmpDir.getAbsolutePath());
    WikiPageProvider destProvider = new VersioningFileProvider();

    destProvider.initialize(engine, props);

    Collection allPages = sourceProvider.getAllPages();

    int idx = 1;//  w  ww.  j av a 2s  .co  m

    for (Iterator i = allPages.iterator(); i.hasNext();) {
        WikiPage p = (WikiPage) i.next();

        System.out.println("Converting page: " + p.getName() + " (" + idx + "/" + allPages.size() + ")");
        List pageHistory = engine.getVersionHistory(p.getName());

        for (ListIterator v = pageHistory.listIterator(pageHistory.size()); v.hasPrevious();) {
            WikiPage pv = (WikiPage) v.previous();

            String text = engine.getPureText(pv.getName(), pv.getVersion());

            destProvider.putPageText(pv, text);
        }

        //
        //  Do manual setting now
        //

        for (Iterator v = pageHistory.iterator(); v.hasNext();) {
            WikiPage pv = (WikiPage) v.next();

            File f = new File(tmpDir, "OLD");
            f = new File(f, mangleName(pv.getName()));
            f = new File(f, pv.getVersion() + ".txt");

            System.out
                    .println("   Setting old version " + pv.getVersion() + " to date " + pv.getLastModified());
            f.setLastModified(pv.getLastModified().getTime());
        }

        idx++;
    }

    tmpDir.delete();
}

From source file:org.craftercms.engine.targeting.impl.merge.TargetedContentDescriptorMergeStrategy.java

@Override
public List<MergeableDescriptor> getDescriptors(Context context, CachingOptions cachingOptions,
        String mainDescriptorUrl, Document mainDescriptorDom, boolean mainDescriptorOptional)
        throws XmlMergeException {
    Set<MergeableDescriptor> results = new LinkedHashSet<>();
    List<String> candidateUrls = candidateTargetedUrlsResolver.getUrls(mainDescriptorUrl);

    for (ListIterator<String> iter = candidateUrls.listIterator(candidateUrls.size()); iter.hasPrevious();) {
        String candidateUrl = iter.previous();
        if (!candidateUrl.equals(mainDescriptorUrl)) {
            Document descriptorDom = getDescriptorDom(context, cachingOptions, candidateUrl);

            if (descriptorDom != null) {
                DescriptorMergeStrategy mergeStrategy = mergeStrategyResolver.getStrategy(candidateUrl,
                        descriptorDom);/*w w w. j av  a 2 s . com*/
                List<MergeableDescriptor> descriptors = mergeStrategy.getDescriptors(context, cachingOptions,
                        candidateUrl, descriptorDom, true);

                results.addAll(descriptors);
            }
        }
    }

    List<MergeableDescriptor> descriptors = super.getDescriptors(context, cachingOptions, mainDescriptorUrl,
            mainDescriptorDom, mainDescriptorOptional);
    results.addAll(descriptors);

    return new ArrayList<>(results);
}

From source file:com.diffplug.gradle.GradleIntegrationTest.java

protected String getContents(Predicate<String> subpathsToInclude) throws IOException {
    TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow());
    List<File> files = TreeStream.depthFirst(treeDef, folder.getRoot()).filter(file -> file.isFile())
            .collect(Collectors.toList());

    ListIterator<File> iterator = files.listIterator(files.size());
    int rootLength = folder.getRoot().getAbsolutePath().length() + 1;
    return StringPrinter.buildString(printer -> {
        Errors.rethrow().run(() -> {/*from w ww  .  j  a v  a2  s .  co m*/
            while (iterator.hasPrevious()) {
                File file = iterator.previous();
                String subPath = file.getAbsolutePath().substring(rootLength);
                if (subpathsToInclude.test(subPath)) {
                    printer.println("### " + subPath + " ###");
                    printer.println(read(subPath));
                }
            }
        });
    });
}

From source file:me.tfeng.play.plugins.StartablePlugin.java

@Override
public void onStop() {
    List<Startable> startables = getStartables();

    for (ListIterator<Startable> iterator = startables.listIterator(startables.size()); iterator
            .hasPrevious();) {//from   w  w w .  j a  va  2s  .  c o  m
        Startable startable = iterator.previous();
        if (startable instanceof ExtendedStartable) {
            try {
                ((ExtendedStartable) startable).beforeStop();
            } catch (Throwable t) {
                onStartFailure(startable, t);
            }
        }
    }

    for (ListIterator<Startable> iterator = startables.listIterator(startables.size()); iterator
            .hasPrevious();) {
        Startable startable = iterator.previous();
        try {
            startable.onStop();
        } catch (Throwable t) {
            onStopFailure(startable, t);
        }
    }

    for (ListIterator<Startable> iterator = startables.listIterator(startables.size()); iterator
            .hasPrevious();) {
        Startable startable = iterator.previous();
        if (startable instanceof ExtendedStartable) {
            try {
                ((ExtendedStartable) startable).afterStop();
            } catch (Throwable t) {
                onStartFailure(startable, t);
            }
        }
    }

    super.onStop();
}

From source file:hu.ppke.itk.nlpg.purepos.model.internal.NGramModel.java

@Override
public void addWord(List<Integer> context, W word) {
    ListIterator<Integer> iterator = context.listIterator(context.size());
    IntTrieNode<W> act = root;//from   www .  j  a  v  a  2 s.  c o m
    int i = 0;
    int size = n - 1;
    act.addWord(word);
    while (iterator.hasPrevious() && i < size) {
        act = (IntTrieNode<W>) act.addChild(iterator.previous());
        act.addWord(word);
        i++;
    }

}