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:com.liangc.hq.base.utils.MonitorUtils.java

public static Integer formatMetrics(Map metrics, Locale userLocale) {
    Integer resourceCount = null;
    try {/*from   w  w  w  .  j av a2s  .c  om*/
        for (Iterator iter = metrics.values().iterator(); iter.hasNext();) {
            Collection metricList = (Collection) iter.next();
            for (Iterator m = metricList.iterator(); m.hasNext();) {
                MetricDisplaySummary mds = (MetricDisplaySummary) m.next();
                if (resourceCount == null)
                    resourceCount = mds.getAvailUp();
                // the formatting subsystem doesn't interpret
                // units set to empty strings as "no units" so
                // we'll explicity set it so
                if (mds.getUnits().length() < 1) {
                    mds.setUnits(MeasurementConstants.UNITS_NONE);
                }
                FormattedNumber[] fs = new FormattedNumber[0];
                if (msgs.containsKey(Constants.UNIT_FORMAT_PREFIX_KEY + mds.getUnits())) {
                    // this means that there's a whole song and dance for formatting this type of thing
                    // a certain way (hopefully in a way that some decendent of java.test.Format will
                    // help with)
                    String fmtString = msgs.get(Constants.UNIT_FORMAT_PREFIX_KEY + mds.getUnits());
                    if (mds.getUnits().equals(MeasurementConstants.UNITS_EPOCH_MILLIS)) {
                        DateSpecifics specs = new DateSpecifics();

                        specs.setDateFormat(new SimpleDateFormat(fmtString, userLocale));
                        fs = UnitsConvert.convertSame(mds.getMetricValueDoubles(), mds.getUnits(), userLocale,
                                specs);
                    } else {
                        fs = UnitsConvert.convertSame(mds.getMetricValueDoubles(), mds.getUnits(), userLocale);
                    }
                } else {
                    fs = UnitsConvert.convertSame(mds.getMetricValueDoubles(), mds.getUnits(), userLocale);
                }
                String[] keys = mds.getMetricKeys();
                if (keys.length != fs.length)
                    throw new IllegalStateException("Formatting metrics failed");
                for (int i = 0; i < keys.length; i++) {
                    mds.getMetric(keys[i]).setValueFmt(fs[i]);
                }
            }
        }
    } catch (IllegalArgumentException e) { // catch and rethrow for debug/logging only
        if (log.isDebugEnabled())
            log.debug("formatting metrics failed due to IllegalArgumentException: ", e);
        throw e;
    }
    return resourceCount;
}

From source file:org.cloudfoundry.maven.common.CommonUtils.java

/**
 * Formats the supported frameworks as a command separated list.
 *
 * @param offerings List of services/*from  w  w  w  . jav a2  s . c  o  m*/
 * @return a String but never null
 */
public static String serviceOfferingsToCommaDelimitedString(final Collection<CloudServiceOffering> offerings) {

    if (offerings == null || offerings.isEmpty()) {
        return "";
    }

    StringBuilder sb = new StringBuilder();
    final Iterator<CloudServiceOffering> it = offerings.iterator();

    while (it.hasNext()) {
        CloudServiceOffering offering = it.next();
        sb.append(offering.getLabel());

        if (it.hasNext()) {
            sb.append(", ");
        }
    }
    return sb.toString();
}

From source file:net.sf.jvifm.util.AutoCompleteUtil.java

@SuppressWarnings("all")
public static String getCommandOptionTip(String cmd) {
    Options options = CommandRegister.getInstance().getCommandOptions(cmd);
    if (options == null)
        return null;
    Collection ops = options.getOptions();
    StringBuffer sb = new StringBuffer();
    for (Iterator it = ops.iterator(); it.hasNext();) {
        String opt = ((Option) it.next()).getOpt();
        sb.append("-").append(opt).append("  ");
    }/*from   w  w w .  j a  va2  s  .  c om*/
    return sb.toString();
}

From source file:Main.java

public static long[] getLongArray(Collection<Long> a_collection)
/*     */ {/*from  w  ww. j av a 2s. co  m*/
    /* 109 */long[] alResult = new long[0];
    /*     */
    /* 111 */if (a_collection != null)
    /*     */ {
        /* 113 */alResult = new long[a_collection.size()];
        /* 114 */int i = 0;
        /*     */
        /* 116 */if (a_collection != null)
        /*     */ {
            /* 118 */Iterator it = a_collection.iterator();
            /* 119 */while (it.hasNext())
            /*     */ {
                /* 121 */alResult[(i++)] = ((Long) it.next()).longValue();
                /*     */}
            /*     */}
        /*     */}
    /* 125 */return alResult;
    /*     */}

From source file:IORoutines.java

public static void saveStrings(File file, java.util.Collection list) throws IOException {
    BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file));
    try {/*  w  ww . j a v  a  2  s . co m*/
        PrintWriter writer = new PrintWriter(bout);
        java.util.Iterator i = list.iterator();
        while (i.hasNext()) {
            String text = (String) i.next();
            writer.println(text);
        }
        writer.flush();
    } finally {
        bout.close();
    }
}

From source file:org.synyx.hades.dao.query.QueryUtils.java

/**
 * Creates a where-clause referencing the given entities and appends it to
 * the given query string. Binds the given entities to the query.
 * // w  ww.  jav a2 s.co m
 * @param <T>
 * @param queryString
 * @param entities
 * @param entityManager
 * @return
 */
public static <T> Query applyAndBind(String queryString, Collection<T> entities, EntityManager entityManager) {

    Assert.notNull(queryString);
    Assert.notNull(entities);
    Assert.notNull(entityManager);

    Iterator<T> iterator = entities.iterator();

    if (!iterator.hasNext()) {
        return entityManager.createQuery(queryString);
    }

    String alias = detectAlias(queryString);
    StringBuilder builder = new StringBuilder(queryString);
    builder.append(" where");

    for (int i = 0; i < entities.size(); i++) {

        builder.append(String.format(" %s = ?%d", alias, i + 1));

        if (i < entities.size() - 1) {
            builder.append(" or");
        }
    }

    Query query = entityManager.createQuery(builder.toString());

    for (int i = 0; i < entities.size(); i++) {
        query.setParameter(i + 1, iterator.next());
    }

    return query;
}

From source file:net.kamhon.ieagle.util.CollectionUtil.java

public static <T> T getFirst(Collection<T> collection) {
    if (isNotEmpty(collection))
        return collection.iterator().next();
    else/*from   ww  w.  j  a  v a2s  . c om*/
        return null;
}

From source file:CollectionUtils.java

/**
 * Find a value of the given type in the given Collection.
 * @param collection the Collection to search
 * @param type the type to look for//from  ww  w . j  av  a2  s. co m
 * @return a value of the given type found, or <code>null</code> if none
 * @throws IllegalArgumentException if more than one value of the given type found
 */
public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException {
    if (isEmpty(collection)) {
        return null;
    }
    Class typeToUse = (type != null ? type : Object.class);
    Object value = null;
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object obj = it.next();
        if (typeToUse.isInstance(obj)) {
            if (value != null) {
                throw new IllegalArgumentException(
                        "More than one value of type [" + typeToUse.getName() + "] found");
            }
            value = obj;
        }
    }
    return value;
}

From source file:Main.java

/**
 * //from w w w. j av a 2 s  .c  o  m
 * @param element
 * @param identifyingProperties
 * @return Map
 */
public static Map<String, String> getResult(final Element element,
        final Collection<String> identifyingProperties) {
    // for each identifying property get that property value for this
    // document
    final String SEPARATOR = "; "; //$NON-NLS-1$
    final Map<String, String> result = new TreeMap<>();

    for (Iterator<String> identifyingPropertiesIterator = identifyingProperties
            .iterator(); identifyingPropertiesIterator.hasNext();) {
        final String identifyingProperty = identifyingPropertiesIterator.next();
        final List<String> newValues = getDOMElementTextByTagName(element, identifyingProperty, ALL_VALUES);
        final String existingValue = result.get(identifyingProperty);

        if (existingValue != null) {
            newValues.add(existingValue);
        }

        final StringBuffer buffer = new StringBuffer();

        boolean needsSeperator = false;

        for (Iterator<String> newValuesIterator = newValues.iterator(); newValuesIterator.hasNext();) {
            if (needsSeperator) {
                buffer.append(SEPARATOR);
            }

            final String value = newValuesIterator.next();

            if (value != null) {
                buffer.append(value);
            }

            needsSeperator = true;
        }

        result.put(identifyingProperty, buffer.toString());
    }

    return result;
}

From source file:Main.java

public static <T> T single(Collection<T> cl, boolean force, Object msg) {
    String amsg = msg == null ? "" : "," + msg.toString();
    if (cl.isEmpty()) {
        if (force) {
            throw new RuntimeException("force:" + cl + amsg);
        } else {//from www .  j a  v  a2s .c  o  m
            return null;
        }
    } else if (cl.size() > 1) {
        throw new RuntimeException("tomuch:" + cl + amsg);
    } else {
        return cl.iterator().next();
    }
}