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:org.fcrepo.apix.routing.impl.RoutingImpl.java

private static <T> T exactlyOne(final Collection<T> of, final String errMsg) {
    if (of.size() != 1) {
        throw new ResourceNotFoundException(errMsg);
    }// w w  w.j  a  v  a  2  s  .c  om

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

From source file:com.aw.core.db.support.WhereBuilder2.java

public static String buildIn(Collection idList, boolean removeNulls) {
    if (removeNulls) {
        List idListTrim = new ArrayList(idList.size());
        for (Object id : idList) {
            if (id != null)
                idListTrim.add(id);/*from   w  ww  .  jav  a  2s. c  o m*/
        }
        idList = idListTrim;
    }
    if (idList.size() == 0)
        return null;

    boolean isNumber = idList.iterator().next() instanceof Number;
    StringBuffer result = isNumber ? ListUtils.concatenarSepPorComa(idList)
            : ListUtils.concatenarSepPorComaYApostrofe(idList);
    return result.toString();
}

From source file:eu.edisonproject.classification.main.BatchMain.java

private static void profile(String csvFile1, String csvFile2, String output) throws IOException, Exception {
    Map<String, Collection<Double>> cv = CSVFileReader.csvCompetanceToMap(csvFile1, ",", Boolean.TRUE);
    Map<String, Collection<Double>> jobVec = CSVFileReader.csvCompetanceToMap(csvFile2, ",", Boolean.TRUE);
    CosineSimilarityMatrix cosineFunction = new CosineSimilarityMatrix();

    String k1 = cv.keySet().iterator().next();
    Map<String, Double> winners = new HashMap<>();
    for (String k : jobVec.keySet()) {
        Collection<Double> j = jobVec.get(k);
        double d = cosineFunction.computeDistance(cv.get(k1), j);
        //      if (!Double.isNaN(d)) {
        winners.put(k, d);/*from w w w  .j  a v  a 2 s  .  c  om*/
        //      }
    }
    StringBuilder lines = new StringBuilder();
    ReaderFile rf = new ReaderFile(csvFile1);
    String fileHeader = rf.readFileWithN().split("\n")[0];
    String[] header = fileHeader.split(",");
    lines.append("rank").append(",");
    lines.append(fileHeader);
    lines.append("\n");

    int rank = 0;

    JSONObject cvJson = new JSONObject();
    Collection<Double> vector = cv.get(k1);
    String val = vector.toString().replaceAll("\\[", "").replaceAll("\\]", "");
    lines.append(rank).append(",").append(k1).append(",").append(val).append("\n");
    Iterator<Double> iter = vector.iterator();
    int i = 0;
    cvJson.put(header[i++], k1);
    while (iter.hasNext()) {
        String key = header[i++];
        Double value = iter.next();
        cvJson.put(key, value);
    }
    cvJson.put("rank", rank);
    JSONArray profileJson = new JSONArray();
    profileJson.add(cvJson);
    ValueComparator bvc = new ValueComparator(winners);
    Map<String, Double> sorted_map = new TreeMap(bvc);
    sorted_map.putAll(winners);

    for (String k : sorted_map.keySet()) {
        JSONObject jobJason = new JSONObject();
        rank++;
        vector = jobVec.get(k);
        val = vector.toString().replaceAll("\\[", "").replaceAll("\\]", "");
        lines.append(rank).append(",").append(k).append(",").append(val).append("\n");

        i = 0;
        jobJason.put(header[i++], k);
        iter = vector.iterator();
        while (iter.hasNext()) {
            String key = header[i++];
            Double value = iter.next();
            jobJason.put(key, value);
        }
        jobJason.put("rank", rank);
        profileJson.add(jobJason);
    }
    WriterFile wf = new WriterFile(output + File.separator + "result.csv");
    wf.writeFile(lines.toString());

    wf = new WriterFile(output + File.separator + "result.json");
    wf.writeFile(profileJson.toJSONString());
}

From source file:com.atlassian.theplugin.idea.bamboo.tree.BuildTreeNode.java

private static String getCommiters(BambooBuildAdapter build) {
    StringBuilder commiters = new StringBuilder();

    Collection<String> c = build.getCommiters();
    for (Iterator<String> iterator = c.iterator(); iterator.hasNext();) {
        commiters.append(iterator.next());
        if (iterator.hasNext()) {
            commiters.append(", ");
        }//from  w  w w.  j a  v a  2 s. c om
    }
    return commiters.toString();
}

From source file:com.jalios.ejpt.sync.utils.IOUtil.java

public static File findPluginXMLFile(File directory) throws FileNotFoundException {
    Collection<File> files = FileUtils.listFiles(directory,
            FileFilterUtils.nameFileFilter(ParseConstants.PLUGIN_XML), TrueFileFilter.INSTANCE);

    if (!files.isEmpty() && files.size() == 1) {
        return files.iterator().next();
    }/* w w w  . j a va2 s  .  c  o  m*/

    throw new FileNotFoundException("'plugin.xml' not found in " + directory);

}

From source file:it.geosolutions.geobatch.services.jmx.JMXServiceManager.java

private static <T extends JMXProgressListener> List<T> get(Collection<IProgressListener> coll) {
    final List<T> al = new ArrayList<T>();
    if (coll == null) {
        return al;
    }/*from w  w w.j a  v a 2s . c  o m*/
    final Iterator<IProgressListener> cit = coll.iterator();
    while (cit.hasNext()) {
        IProgressListener pl = cit.next();
        if (pl instanceof JMXCumulatorListener) {
            al.add((T) JMXCumulatorListener.class.cast(pl));
        }
    }
    return al;
}

From source file:cz.muni.fi.nbs.utils.Helpers.java

public static void processResults(Collection<RunResult> runResults, String resultsDirectory) {

    resultsDir = resultsDirectory;/*from  w  ww  .  j av a2s  . c  om*/
    String benchmarkName = null;
    Map<String, Collection<Result>> results = new HashMap<>();

    Iterator<RunResult> it = runResults.iterator();

    //process the runs
    while (it.hasNext()) {
        RunResult runResult = it.next();
        Collection<BenchmarkResult> benchmarkResults = runResult.getBenchmarkResults();

        //Extract the benchmark class name.
        Collection<String> benchNames = new ArrayList<>();
        benchNames.add(runResult.getParams().getBenchmark());
        Map<String, String> denseClassNames = ClassUtils.denseClassNames(benchNames);
        //TODO: Modify for multiple results!
        String denseClassName = denseClassNames.get(denseClassNames.keySet().iterator().next());
        String[] splitName = denseClassName.split("\\.");
        StringBuilder benchmarkClass = new StringBuilder(splitName[0]);
        benchmarkName = benchmarkClass.toString();

        Iterator<BenchmarkResult> brit = benchmarkResults.iterator();

        //process the results
        while (brit.hasNext()) {

            BenchmarkResult benchmarkResult = brit.next();

            // get params
            Iterator<String> iterator = benchmarkResult.getParams().getParamsKeys().iterator();
            while (iterator.hasNext()) {
                String param = iterator.next();
                String suffix = "_" + param + "_" + benchmarkResult.getParams().getParam(param);
                benchmarkClass.append(suffix);
            }

            if (!results.containsKey(benchmarkClass.toString()))
                results.put(benchmarkClass.toString(), new ArrayList<Result>());

            Result r = benchmarkResult.getPrimaryResult();
            results.get(benchmarkClass.toString()).add(r);
        }
    }

    //output the results
    exportToHTML(results, benchmarkName, true);
    exportToPNG(results);
}

From source file:Main.java

/**
 * Creates an array of the given Collection's elements, but with the given
 * <code>Class</code> as element type. Useful for arrays of objects that
 * implement multiple interfaces and a "typed view" onto these objects is
 * required.//from  w w w.  j a  v a 2 s.c om
 * 
 * @param objects a Collection of objects
 * @param clazz the desired service type of the new array
 * @return <code>null</code> when objects is <code>null</code>, or a new
 *         array containing the elements of the source array which is typed to
 *         the given <code>clazz</code> parameter.
 * @throws IllegalArgumentException if the <code>clazz</code> argument is
 *             <code>null</code>.
 * @throws ArrayStoreException if the elements in <code>objects</code> cannot
 *             be cast to <code>clazz</code>.
 */
public static <T> T[] toArrayOfComponentType(Collection objects, Class<T> clazz) {
    if (objects == null) {
        return null;
    }

    if (clazz == null) {
        throw new IllegalArgumentException("Array target class must not be null");
    }

    if (objects.isEmpty()) {
        return (T[]) Array.newInstance(clazz, 0);
    }

    int i = 0, size = objects.size();
    T[] result = (T[]) Array.newInstance(clazz, size);
    Iterator iter = objects.iterator();

    while (i < size && iter.hasNext()) {
        result[i++] = (T) iter.next();
    }

    return result;
}

From source file:edu.stanford.mobisocial.dungbeetle.Helpers.java

private static String buildAddresses(Collection<Contact> contacts) {
    String to = "";
    Iterator<Contact> it = contacts.iterator();
    while (it.hasNext()) {
        Contact c = it.next();/*from   w  ww .  j a  v a 2 s.c  om*/
        if (it.hasNext()) {
            to += c.id + ",";
        } else {
            to += c.id;
        }
    }
    return to;
}

From source file:com.ksmpartners.ernie.util.TestUtil.java

/**
 * Checks equality of two objects based on the equality of their fields, items, or .equals() method.
 * @param obj1//  w  ww.  ja  v a  2  s  . c  om
 * @param obj2
 * @return
 */
public static <T> boolean equal(T obj1, T obj2) {
    if (obj1 == null || obj2 == null) {
        // If they're both null, we call this equal
        if (obj1 == null && obj2 == null)
            return true;
        else
            return false;
    }

    if (!obj1.getClass().equals(obj2.getClass()))
        return false;

    if (obj1.equals(obj2))
        return true;

    List<Pair> vals = new ArrayList<Pair>();

    // If obj1 and obj2 are Collections, get the objects in them
    if (Collection.class.isAssignableFrom(obj1.getClass())) {

        Collection c1 = (Collection) obj1;
        Collection c2 = (Collection) obj2;

        if (c1.size() != c2.size())
            return false;

        Iterator itr1 = c1.iterator();
        Iterator itr2 = c2.iterator();

        while (itr1.hasNext() && itr2.hasNext()) {
            vals.add(new Pair(itr1.next(), itr2.next()));
        }

    }

    // Get field values from obj1 and obj2
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(obj1);
    for (PropertyDescriptor property : properties) {

        // ignore getClass() and isEmpty()
        if (property.getName().equals("class") || property.getName().equals("empty"))
            continue;

        Object val1 = invokeMethod(obj1, property.getReadMethod(), null, property.getName());
        Object val2 = invokeMethod(obj2, property.getReadMethod(), null, property.getName());

        vals.add(new Pair(val1, val2));
    }

    if (vals.isEmpty())
        return false;

    for (Pair pair : vals) {
        if (!equal(pair.left, pair.right))
            return false;
    }

    return true;
}