Example usage for java.lang Iterable iterator

List of usage examples for java.lang Iterable iterator

Introduction

In this page you can find the example usage for java.lang Iterable iterator.

Prototype

Iterator<T> iterator();

Source Link

Document

Returns an iterator over elements of type T .

Usage

From source file:Main.java

/**
 * Determines the size of the given iterable. If it is null, zero is
 * returned. If it is a {@code Collection}, then the size method is used.
 * Otherwise, the iterable is iterated over to get the size.
 *
 * @param   iterable//from  w  ww  .j a v  a2s  . c om
 *      The iterable to determine the size of.
 * @return
 *      The size of the given iterable.
 */
public static int size(final Iterable<?> iterable) {
    if (iterable == null) {
        // The size is zero.
        return 0;
    } else if (iterable instanceof Collection) {
        // Get the size from the collection. This cast is justified by
        // not having to loop over all the elements.
        return ((Collection<?>) iterable).size();
    } else {
        // Cound up the elements in the iterable.
        int counter = 0;
        final Iterator<?> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            iterator.next();
            counter++;
        }
        return counter;
    }
}

From source file:org.aludratest.hpalm.infrastructure.HpAlmSession.java

private static String getSingleHeaderValue(Response response, String header) {
    Map<String, ? extends Iterable<String>> headers = response.getResponseHeaders();
    if (headers != null) {
        Iterable<String> values = headers.get(header);
        if (values != null) {
            Iterator<String> iter = values.iterator();
            if (iter.hasNext()) {
                return iter.next();
            }//w  w  w  .j  a  v a 2s .  c  o m
        }
    }

    return null;
}

From source file:fresto.datastore.titan.TitanEventWriter.java

public static void linkToTS(TitanGraph g, Vertex v, long timestamp) {
    long second = (timestamp / 1000) * 1000;
    long minute = (timestamp / 60000) * 60000;

    Iterable<Vertex> vertices = g.getVertices("second", second);
    //Iterable<Vertex> vertices = secondIndex.get("second", second);
    Vertex secondVertex = null;/*from   w  w w .j a  va2  s . c  o m*/
    if (vertices.iterator().hasNext()) {
        secondVertex = vertices.iterator().next();
    } else {
        secondVertex = g.addVertex(null);
        secondVertex.setProperty("second", second);
        //LOGGER.info("Second[" + second + "] created.");
    }
    secondVertex.addEdge("include", v).setProperty("event", v.getProperty("event"));
}

From source file:gov.nih.nci.caarray.application.translation.geosoft.GeoSoftFileWriterUtil.java

private static void writeProtocol(Iterable<ProtocolApplication> pas, String label,
        Predicate<ProtocolApplication> protocolFilter, PrintWriter out) {

    Iterable<ProtocolApplication> selected = pas;
    if (protocolFilter != null) {
        selected = Iterables.filter(pas, protocolFilter);
    }/*  w ww  .j  ava  2  s . c om*/
    if (selected.iterator().hasNext()) {
        out.print('!');
        out.print(label);
        out.print("=");
        String semi = "\"";
        final Set<String> uniqueEntries = new HashSet<String>();
        final StringBuilder sb = new StringBuilder();
        for (final ProtocolApplication pa : selected) {
            sb.setLength(0);
            sb.append(pa.getProtocol().getName());
            if (StringUtils.isNotBlank(pa.getProtocol().getDescription())) {
                sb.append(':').append(pa.getProtocol().getDescription());
            }
            final String entry = sb.toString();
            if (uniqueEntries.add(entry)) {
                out.print(semi);
                out.print(entry);
                semi = "\"; \"";
            }
        }
        out.println('"');
    }
}

From source file:RandomChooser.java

public static void notEmpty(String arg, Iterable<?> iter) {
    if (arg == null) {
        arg = "Iterable";
    }/* www.  j a va  2 s  .  c  om*/
    notNull(iter);
    if (iter.iterator().hasNext())
        return;
    throw new IllegalArgumentException(arg + " is empty");
}

From source file:org.paxml.util.ReflectUtils.java

public static int collect(Iterable it, Collection col) {
    return collect(it.iterator(), col);
}

From source file:com.github.rinde.rinsim.util.StochasticSuppliers.java

/**
 * Create a {@link StochasticSupplier} based on an {@link Iterable}. It will
 * return the values in the order as defined by the iterable. The resulting
 * supplier will throw an {@link IllegalArgumentException} when the iterable
 * is empty./*from  w  w  w.j a v a  2 s .c  o m*/
 * @param iter The iterable from which the values will be used.
 * @param <T> The type this supplier generates.
 * @return A supplier based on an iterable.
 */
public static <T> StochasticSupplier<T> fromIterable(Iterable<T> iter) {
    return new IteratorSS<>(iter.iterator());
}

From source file:com.metamx.druid.indexing.coordinator.TaskLifecycleTest.java

private static FirehoseFactory newMockFirehoseFactory(final Iterable<InputRow> inputRows) {
    return new FirehoseFactory() {
        @Override//ww w  . j  a v  a  2  s  . com
        public Firehose connect() throws IOException {
            final Iterator<InputRow> inputRowIterator = inputRows.iterator();

            return new Firehose() {
                @Override
                public boolean hasMore() {
                    return inputRowIterator.hasNext();
                }

                @Override
                public InputRow nextRow() {
                    return inputRowIterator.next();
                }

                @Override
                public Runnable commit() {
                    return new Runnable() {
                        @Override
                        public void run() {

                        }
                    };
                }

                @Override
                public void close() throws IOException {

                }
            };
        }
    };
}

From source file:com.haulmont.cuba.gui.ComponentsHelper.java

/**
 * @deprecated Use guava {@link Iterables#indexOf(Iterable, Predicate)}
 *//*from ww w  . j a  v a 2 s . c  o  m*/
@Deprecated
public static int indexOf(Iterable<Component> components, Component component) {
    Preconditions.checkNotNullArgument(components);

    Iterator<Component> iterator = components.iterator();
    for (int i = 0; iterator.hasNext(); i++) {
        Component current = iterator.next();
        if (current == component) {
            return i;
        }
    }
    return -1;
}

From source file:com.hazelcast.stabilizer.Utils.java

public static String join(Iterable<?> collection, String delimiter) {
    StringBuilder builder = new StringBuilder();
    Iterator<?> iterator = collection.iterator();
    while (iterator.hasNext()) {
        Object o = iterator.next();
        builder.append(o);//from  w  w  w .j a v  a2s . c o m
        if (iterator.hasNext()) {
            builder.append(delimiter);
        }
    }
    return builder.toString();
}