Example usage for java.util Collection iterator

List of usage examples for java.util Collection iterator

Introduction

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

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this collection.

Usage

From source file:Main.java

/**
 * Returns the first element of the collection that satisfies the given condition, or <code>null</code> if no such
 * element exists. If the condition is <code>null</code>, the first element of the collection is returned.
 *
 * @param collection The collection to be searched
 * @param condition  The condition to be applied
 * @param <T>        The type of the collection elements
 * @return The first element of the collection matching the condition, or <code>null</code>
 *///from  ww  w. j a v  a 2  s.  c om
public static <T> T firstOrDefault(Collection<T> collection, Predicate<T> condition) {
    T result = null;
    if (collection != null) {
        if (condition != null) {
            List<T> subset = where(collection, condition);
            if (subset != null && subset.size() > 0)
                result = subset.get(0);
        } else {
            result = collection.iterator().next();
        }
    }
    return result;
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.administrativeOffice.gradeSubmission.EditMarkSheet.java

private static void editRectificationMarkSheet(MarkSheetManagementEditBean markSheetManagementEditBean) {

    Collection<MarkSheetEnrolmentEvaluationBean> filteredEnrolmentEvaluationBeansToEditList = getEnrolmentEvaluationsWithValidGrades(
            markSheetManagementEditBean.getEnrolmentEvaluationBeansToEdit());

    /*// ww w .j a  v a 2  s .  c o  m
     * Rectification MarkSheet MUST have ONLY ONE EnrolmentEvaluation
     */
    Iterator<MarkSheetEnrolmentEvaluationBean> iterator = filteredEnrolmentEvaluationBeansToEditList.iterator();
    markSheetManagementEditBean.getMarkSheet().editRectification(iterator.hasNext() ? iterator.next() : null);
}

From source file:ar.com.qbe.siniestros.model.utils.MimeMagic.Magic.java

/**
 * print a magic match/*from w  ww.  j av  a 2s.c  om*/
 *
 * @param stream DOCUMENT ME!
 * @param match DOCUMENT ME!
 * @param spacing DOCUMENT ME!
 */
public static void printMagicMatch(PrintStream stream, MagicMatch match, String spacing) {
    stream.println(spacing + "=============================");
    stream.println(spacing + "mime type: " + match.getMimeType());
    stream.println(spacing + "description: " + match.getDescription());
    stream.println(spacing + "extension: " + match.getExtension());
    stream.println(spacing + "test: " + new String(match.getTest().array()));
    stream.println(spacing + "bitmask: " + match.getBitmask());
    stream.println(spacing + "offset: " + match.getOffset());
    stream.println(spacing + "length: " + match.getLength());
    stream.println(spacing + "type: " + match.getType());
    stream.println(spacing + "comparator: " + match.getComparator());
    stream.println(spacing + "=============================");

    Collection submatches = match.getSubMatches();
    Iterator i = submatches.iterator();

    while (i.hasNext()) {
        printMagicMatch(stream, (MagicMatch) i.next(), spacing + "    ");
    }
}

From source file:grails.plugin.searchable.internal.SearchableUtils.java

/**
 * Returns a collection of user classes for the given GrailsDomainClass instances
 * @param grailsDomainClasses a collection of GrailsDomainClass instances
 * @return a collection of user classes//  w w  w. j  a  v  a 2s. com
 */
public static Collection getClasses(Collection grailsDomainClasses) {
    Assert.notNull(grailsDomainClasses, "grailsDomainClasses cannot be null");
    Set classes = new HashSet();
    for (Iterator iter = grailsDomainClasses.iterator(); iter.hasNext();) {
        classes.add(((GrailsDomainClass) iter.next()).getClazz());
    }
    return classes;
}

From source file:com.bluexml.side.integration.standalone.GenerateModelHelper.java

/**
 * Retrieve the model files in a directory, given a file-extension name
 * /*from   www.  j ava2s. co  m*/
 * @param directoryPath the base directory path
 * @param subDirectoryName the sub-directory name to restrict to
 * @param extensionName the extension name
 * @return a list of corresponding {@link File}s
 * @throws FileNotFoundException if the input directory path cannot be found
 */
public static List<File> retrieveModelFiles(String directoryPath, String extensionName)
        throws FileNotFoundException {
    File projectDirectory = new File(directoryPath);

    if (!projectDirectory.exists()) {
        final String message = String.format(
                "The directory '%s' does not exist. A valid project path has to be provided.",
                projectDirectory);
        LOGGER.severe(message);
        throw new FileNotFoundException(message);
    }

    if (!projectDirectory.isDirectory()) {
        return Collections.emptyList();
    }

    List<File> modelFiles = new ArrayList<File>();

    Collection<?> files = FileUtils.listFiles(projectDirectory, FileFilterUtils.suffixFileFilter(extensionName),
            FileFilterUtils.trueFileFilter());
    for (Iterator<?> iterator = files.iterator(); iterator.hasNext();) {
        modelFiles.add((File) iterator.next());
    }

    return modelFiles;
}

From source file:com.projity.graphic.configuration.SpreadSheetFieldArray.java

public static Collection toIdArray(Collection fieldArray) {
    ArrayList result = new ArrayList(fieldArray.size());
    Iterator i = fieldArray.iterator();
    while (i.hasNext()) {
        result.add(TimeDistributedHelper.getIdForObject(i.next()));
    }//from  ww w  .  j a  v a  2  s. c o m
    return result;
}

From source file:com.projity.graphic.configuration.SpreadSheetFieldArray.java

public static Collection fromIdArray(Collection fieldArray) {
    ArrayList result = new ArrayList(fieldArray.size());
    Iterator i = fieldArray.iterator();
    while (i.hasNext()) {
        result.add(TimeDistributedHelper.getObjectFromId((String) i.next()));
    }/*from  w w w. j  ava  2 s .  c  o m*/
    return result;
}

From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java

/**
 * Fetches the fields on a class that are annotated with SpringBean. The first time it
 * is called for a particular class it will introspect the class and cache the results.
 * All non-overridden fields are examined, including protected and private fields.
 * If a field is not public an attempt it made to make it accessible - if it fails
 * it is removed from the collection and an error is logged.
 *
 * @param clazz the class on which to look for SpringBean annotated fields
 * @return the collection of methods with the annotation
 *//*from  w  w w  .j av  a  2  s . co m*/
protected static Collection<Field> getFields(Class<?> clazz) {
    Collection<Field> fields = fieldMap.get(clazz);
    if (fields == null) {
        fields = ReflectUtil.getFields(clazz);
        Iterator<Field> iterator = fields.iterator();

        while (iterator.hasNext()) {
            Field field = iterator.next();
            if (!field.isAnnotationPresent(SpringBean.class)) {
                iterator.remove();
            } else if (!field.isAccessible()) {
                // If the field isn't public, try to make it accessible
                try {
                    field.setAccessible(true);
                } catch (SecurityException se) {
                    throw new StripesRuntimeException("Field " + clazz.getName() + "." + field.getName()
                            + "is marked " + "with @SpringBean and is not public. An attempt to call "
                            + "setAccessible(true) resulted in a SecurityException. Please "
                            + "either make the field public, annotate a public setter instead "
                            + "or modify your JVM security policy to allow Stripes to "
                            + "setAccessible(true).", se);
                }
            }
        }

        fieldMap.put(clazz, fields);
    }

    return fields;
}

From source file:com.doculibre.constellio.plugins.PluginFactory.java

private static void initPluginManager() {
    if (pm == null) {
        pm = PluginManagerFactory.createPluginManager();
        File classesDir = ClasspathUtils.getClassesDir();

        pm.addPluginsFrom(classesDir.toURI());

        File pluginsDir = getPluginsDir();
        File[] pluginDirs = pluginsDir.listFiles(new FileFilter() {
            @Override/* w w  w  .  ja  v  a 2 s.  c o  m*/
            public boolean accept(File pathname) {
                boolean accept;
                if (pathname.isFile()) {
                    accept = false;
                } else if (DefaultConstellioPlugin.NAME.equals(pathname)) {
                    accept = true;
                } else {
                    List<String> availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();
                    accept = availablePluginNames.contains(pathname.getName());
                }
                return accept;
            }
        });
        if (pluginDirs == null) {
            return;
        }
        for (File pluginDir : pluginDirs) {
            // Plugin root dir jars
            Collection<File> pluginJarFiles = FileUtils.listFiles(pluginDir, new String[] { "jar" }, false);
            // Accept only one root dir jar
            File pluginJarFile = pluginJarFiles.isEmpty() ? null : pluginJarFiles.iterator().next();
            if (pluginJarFile != null) {
                URI pluginJarFileURI = pluginJarFile.toURI();
                pm.addPluginsFrom(pluginJarFileURI);

                PluginManagerImpl pmImpl = (PluginManagerImpl) pm;
                ClassPathManager classPathManager = pmImpl.getClassPathManager();
                ClassLoader classLoader = ClassPathManagerUtils.getClassLoader(classPathManager, pluginJarFile);
                classLoaders.add(classLoader);

                File pluginLibDir = new File(pluginDir, "lib");
                if (pluginLibDir.exists() && pluginLibDir.isDirectory()) {
                    Collection<File> pluginDependencies = FileUtils.listFiles(pluginLibDir,
                            new String[] { "jar" }, false);
                    ClassPathManagerUtils.addJarDependencies(classPathManager, pluginJarFile,
                            pluginDependencies);
                }
            }
        }

        File webInfDir = ClasspathUtils.getWebinfDir();
        File libDir = new File(webInfDir, "lib");
        File[] contellioJarFiles = libDir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                boolean accept;
                if (pathname.isDirectory()) {
                    accept = false;
                } else {
                    List<String> availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();
                    String jarNameWoutExtension = FilenameUtils.removeExtension(pathname.getName());
                    accept = availablePluginNames.contains(jarNameWoutExtension);
                }
                return accept;
            }
        });
        for (File constellioJarFile : contellioJarFiles) {
            URI constellioJarFileURI = constellioJarFile.toURI();
            pm.addPluginsFrom(constellioJarFileURI);
        }
    }
}

From source file:Main.java

/**
 * <p>/*from  w w w  .  j a v  a 2  s.  c o m*/
 * Joins the elements of the provided <code>Collection</code> into a single
 * String containing the provided elements.
 * </p>
 * <p>
 * No delimiter is added before or after the list. Null objects or empty
 * strings within the iteration are represented by empty strings.
 * </p>
 * <p>
 * See the examples here: {@link #join(Object[],char)}.
 * </p>
 * 
 * @param collection the <code>Collection</code> of values to join together,
 *            may be null
 * @param separator the separator character to use
 * @return the joined String, <code>null</code> if null iterator input
 * @since 2.3
 */
public static String join(Collection<?> collection, char separator) {
    if (collection == null) {
        return null;
    }
    return join(collection.iterator(), separator);
}