Example usage for java.util List iterator

List of usage examples for java.util List iterator

Introduction

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

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this list in proper sequence.

Usage

From source file:com.ibm.watson.catalyst.corpus.tfidf.Template.java

private static StringBuffer makeOr(List<String> aList) {
    StringBuffer result = new StringBuffer();
    result.append("(");
    Iterator<String> wordsIterator = aList.iterator();
    while (wordsIterator.hasNext()) {
        result.append(wordsIterator.next());
        if (wordsIterator.hasNext())
            result.append("|");
    }/* www. j  a v a  2 s .c  o  m*/
    result.append(")");
    return result;
}

From source file:com.intbit.FileUploadUtil.java

public static String uploadFile(String uploadPath, HttpServletRequest request)
        throws FileUploadException, Exception {
    logger.info("FileUploadUtil::Entering FileUploadUtil#uploadFile");

    String fileName = null;/*  w  w  w .java  2s  .  c  o  m*/
    logger.info("FileUploadUtil::Upload path without filename: " + uploadPath);
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // maximum size that will be stored in memory
    factory.setSizeThreshold(maxMemSize);
    // Location to save data that is larger than maxMemSize.
    factory.setRepository(new File(AppConstants.TMP_FOLDER));

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // maximum file size to be uploaded.
    upload.setSizeMax(maxFileSize);

    // Parse the request to get file items.
    List fileItems = upload.parseRequest(request);

    // Process the uploaded file items
    Iterator i = fileItems.iterator();

    while (i.hasNext()) {
        FileItem fi = (FileItem) i.next();
        if (!(fi.isFormField())) {
            // Get the uploaded file parameters
            fileName = fi.getName();
            if (!"".equals(fileName)) {
                File uploadDir = new File(uploadPath);
                boolean result = false;
                if (!uploadDir.exists()) {
                    result = uploadDir.mkdirs();
                }
                // Write the file
                String filePath = uploadPath + File.separator + fileName;
                logger.info("FileUploadUtil::Upload path with filename" + filePath);
                File storeFile = new File(filePath);
                fi.write(storeFile);
                logger.info("FileUploadUtil::File Uploaded successfully");

            } else {
                throw new IllegalArgumentException("Filename of uploded file cannot be empty");
            }
        }
    }
    return fileName;
}

From source file:net.sf.mcf2pdf.mcfelements.util.PdfUtil.java

/**
 * Converts an FO file to a PDF file using Apache FOP.
 *
 * @param fo the FO file//from  w  w w  .  ja va  2 s  .  c  o  m
 * @param pdf the target PDF file
 * @param dpi the DPI resolution to use for bitmaps in the PDF
 * @throws IOException In case of an I/O problem
 * @throws FOPException In case of a FOP problem
 * @throws TransformerException In case of XML transformer problem
 */
@SuppressWarnings("rawtypes")
public static void convertFO2PDF(InputStream fo, OutputStream pdf, int dpi)
        throws IOException, FOPException, TransformerException {

    FopFactory fopFactory = FopFactory.newInstance();

    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    // configure foUserAgent as desired
    foUserAgent.setTargetResolution(dpi);

    // Setup output stream. Note: Using BufferedOutputStream
    // for performance reasons (helpful with FileOutputStreams).
    OutputStream out = new BufferedOutputStream(pdf);

    // Construct fop with desired output format
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

    // Setup JAXP using identity transformer
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(); // identity
                                                        // transformer

    // Setup input stream
    Source src = new StreamSource(fo);

    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());

    // Start XSLT transformation and FOP processing
    transformer.transform(src, res);

    // Result processing
    FormattingResults foResults = fop.getResults();
    java.util.List pageSequences = foResults.getPageSequences();
    for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
        PageSequenceResults pageSequenceResults = (PageSequenceResults) it.next();
        log.debug("PageSequence "
                + (String.valueOf(pageSequenceResults.getID()).length() > 0 ? pageSequenceResults.getID()
                        : "<no id>")
                + " generated " + pageSequenceResults.getPageCount() + " pages.");
    }
    log.info("Generated " + foResults.getPageCount() + " PDF pages in total.");
    out.flush();
}

From source file:Main.java

/**
 * Sorts <code>source</code> and adds the first n entries to
 * <code>dest</code>. If <code>source</code> contains less than n entries,
 * all of them are added to <code>dest</code>.
 * /*from   w ww  . ja  v a 2 s .c o m*/
 * If adding an entry to <code>dest</code> does not increase the
 * collection's size, for example if <code>dest</code> is a set and already
 * contained the inserted contact, an additional entry of
 * <code>source</code> will be added, if available. This guarantees that
 * <code>n</code> new, distinct entries are added to collection
 * <code>dest</code> as long as this can be fulfilled with the contents of
 * <code>source</code>, and as <code>dest</code> does recognise duplicate
 * entries. Consequently, this guarantee does not hold for simple lists.
 * 
 * Both collections may not be <code>null</code>.
 * 
 * @param <T>
 *            the entry type of the collections.
 * @param source
 *            the source collection.
 * @param dest
 *            the destination collection.
 * @param order
 *            the order in which <code>source</code> is to be sorted.
 * @param n
 *            the number of new entries that are to be added to
 *            <code>dest</code>.
 */
public static <T> void copyNSorted(final Collection<? extends T> source, final Collection<? super T> dest,
        final Comparator<? super T> order, final int n) {
    final List<? extends T> src = Collections.list(Collections.enumeration(source));
    Collections.sort(src, order);
    final Iterator<? extends T> it = src.iterator();
    final int maxEntries = dest.size() + n;
    while (it.hasNext() && dest.size() < maxEntries) {
        dest.add(it.next());
    }
}

From source file:Main.java

/**
 * <p>Given a <code>List</code> of class names, this method converts them into classes.</p>
 *
 * <p>A new <code>List</code> is returned. If the class name cannot be found, <code>null</code>
 * is stored in the <code>List</code>. If the class name in the <code>List</code> is
 * <code>null</code>, <code>null</code> is stored in the output <code>List</code>.</p>
 *
 * @param classNames  the classNames to change
 * @return a <code>List</code> of Class objects corresponding to the class names,
 *  <code>null</code> if null input
 * @throws ClassCastException if classNames contains a non String entry
 */// www  .j ava2s.  co  m
public static List convertClassNamesToClasses(List classNames) {
    if (classNames == null) {
        return null;
    }
    List classes = new ArrayList(classNames.size());
    for (Iterator it = classNames.iterator(); it.hasNext();) {
        String className = (String) it.next();
        try {
            classes.add(Class.forName(className));
        } catch (Exception ex) {
            classes.add(null);
        }
    }
    return classes;
}

From source file:Main.java

/**
 * @return/*from ww  w .j  a v  a  2s .com*/
 */
public static XPath getCruxPagesXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath findPath = factory.newXPath();
    findPath.setNamespaceContext(new NamespaceContext() {
        public String getNamespaceURI(String prefix) {
            if (prefix.equals("c")) {
                return "http://www.cruxframework.org/crux";
            } else if (prefix.equals("v")) {
                return "http://www.cruxframework.org/view";
            }

            return "";
        }

        public String getPrefix(String namespaceURI) {
            if (namespaceURI.equals("http://www.cruxframework.org/crux")) {
                return "c";
            } else if (namespaceURI.equals("http://www.cruxframework.org/view")) {
                return "v";
            }
            return "";
        }

        public Iterator<?> getPrefixes(String namespaceURI) {
            List<String> prefixes = new ArrayList<String>();
            prefixes.add("c");
            prefixes.add("v");

            return prefixes.iterator();
        }
    });

    return findPath;
}

From source file:fll.TournamentTeam.java

/**
 * Filter the specified list to just the teams in the specified event
 * division.//w  w  w .  ja  v  a2  s.c  om
 * 
 * @param teams list that is modified
 * @param divisionStr the division to keep
 * @throws RuntimeException
 * @throws SQLException
 */
public static void filterTeamsToEventDivision(final List<TournamentTeam> teams, final String divisionStr)
        throws SQLException, RuntimeException {
    final Iterator<TournamentTeam> iter = teams.iterator();
    while (iter.hasNext()) {
        final TournamentTeam t = iter.next();
        final String eventDivision = t.getEventDivision();
        if (!eventDivision.equals(divisionStr)) {
            iter.remove();
        }
    }
}

From source file:ch.systemsx.cisd.openbis.generic.server.business.bo.common.EntityListingTestUtils.java

public static long getAnyEntityId(List<? extends BaseEntityPropertyRecord> properties) {
    return properties.iterator().next().entity_id;
}

From source file:Main.java

public static <T> T choice(List<T> list) {
    int stuff = random.nextInt(list.size());
    for (T t : list) {
        if (stuff == 0)
            return t;
        else/*from w ww  .  j  a va2 s .  c  o  m*/
            stuff -= 1;
    }

    return list.iterator().next();
}

From source file:Main.java

public static Map split(List list, String separator) {
    if (list == null)
        return null;
    Map map = new HashMap();
    if (list == null || list.size() == 0)
        return map;
    for (Iterator i$ = list.iterator(); i$.hasNext();) {
        String item = (String) i$.next();
        int index = item.indexOf(separator);
        if (index == -1)
            map.put(item, "");
        else/*from  w  w w  .  ja v  a  2s  .  c  om*/
            map.put(item.substring(0, index), item.substring(index + 1));
    }

    return map;
}