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:com.indoqa.lang.util.StringUtils.java

public static void append(StringBuilder stringBuilder, Iterable<?> values, String separator) {
    for (Iterator<?> i = values.iterator(); i.hasNext();) {
        stringBuilder.append(String.valueOf(i.next()));

        if (i.hasNext()) {
            stringBuilder.append(separator);
        }//w ww .  ja  va2  s  .  c o m
    }
}

From source file:com.ocs.dynamo.ui.composite.table.TableUtils.java

/**
 * Formats a collection of entities (turns it into a comma-separated string based on the value
 * of the "displayProperty")// ww w .  jav  a2  s .c om
 * 
 * @param entityModelFactory
 * @param collection
 * @return
 */
public static String formatEntityCollection(EntityModelFactory entityModelFactory, Object collection) {
    StringBuilder builder = new StringBuilder();
    Iterable<?> col = (Iterable<?>) collection;
    Iterator<?> it = col.iterator();
    while (it.hasNext()) {
        if (builder.length() > 0) {
            builder.append(", ");
        }

        Object next = it.next();
        if (next instanceof AbstractEntity) {
            EntityModel<?> entityModel = entityModelFactory.getModel(next.getClass());
            String displayProperty = entityModel.getDisplayProperty();
            if (displayProperty != null) {
                builder.append(ClassUtils.getFieldValueAsString(next, displayProperty));
            } else {
                builder.append(next.toString());
            }
        } else {
            builder.append(next);
        }
    }
    return builder.toString();
}

From source file:Main.java

/**
 * Returns a {@link Map} mapping each unique element in the given
 * {@link Iterable} to an {@link Integer} representing the number
 * of occurrences of that element in the {@link Iterable}.
 * <p/>/* w  ww  . java  2 s .  c  o  m*/
 * Only those elements present in the Iterable will appear as
 * keys in the map.
 *
 * @param iterable the collection to get the cardinality map for, must not be null
 * @return the populated cardinality map
 */
public static <E> Map<E, java.lang.Integer> getCardinalityMap(final Iterable<E> iterable) {
    Map<E, Integer> count = new HashMap<E, Integer>();
    for (Iterator<E> it = iterable.iterator(); it.hasNext();) {
        E obj = it.next();
        Integer c = count.get(obj);
        if (c == null) {
            count.put(obj, INTEGER_ONE);
        } else {
            count.put(obj, new Integer(c.intValue() + 1));
        }
    }
    return count;
}

From source file:com.thoughtworks.studios.journey.utils.IterableUtils.java

public static <T> Iterable<T> uniqueBy(final Function<T, Object> function, final Iterable<T> iterable) {
    return new Iterable<T>() {
        @Override//  ww w.j ava 2s.c o  m
        public Iterator<T> iterator() {
            final Iterator<T> iterator = iterable.iterator();

            return new Iterator<T>() {
                Set<Object> keys = new HashSet<>();
                T nextItem;

                @Override
                public boolean hasNext() {
                    while (iterator.hasNext()) {
                        nextItem = iterator.next();
                        if (keys.add(function.apply(nextItem))) {
                            return true;
                        }
                    }

                    return false;
                }

                @Override
                public T next() {
                    if (nextItem == null && !hasNext()) {
                        throw new NoSuchElementException();
                    }

                    return nextItem;
                }

                @Override
                public void remove() {
                }
            };
        }
    };
}

From source file:com.wrmsr.kleist.util.Itertools.java

public static <T extends AutoCloseable, R> R withCloseables(Iterable<? extends T> iterable,
        Function<List<T>, R> function) throws Exception {
    return withCloseables(iterable.iterator(), ImmutableList.builder(), function);
}

From source file:org.arrow.test.WorkflowTestUtils.java

public static void validSignalWorkflow(String signal, ApplicationContext ctx) throws Exception {

    final RuntimeService runtimeService = ctx.getBean(RuntimeService.class);
    final ExecutionRepository executionRepository = ctx.getBean(ExecutionRepository.class);

    // when a process is started by a process id
    Future<Iterable<ProcessInstance>> future;
    future = runtimeService.startProcessBySignal(signal);

    Iterable<ProcessInstance> instances = Await.result(future, Duration.Inf());

    junit.framework.Assert.assertNotNull(instances);
    junit.framework.Assert.assertTrue(instances.iterator().hasNext());

    // then the process should complete successfully
    for (ProcessInstance pi : instances) {
        pi.waitOnCompletion();/*w w w  .ja v a  2  s  .  c  o  m*/
        junit.framework.Assert.assertTrue(pi.isFinished());

        WorkflowTestUtils.assertSuccessState(executionRepository, pi);
    }
}

From source file:com.yahoo.pulsar.common.naming.NamespaceBundleFactory.java

public static String getNamespaceFromPoliciesPath(String path) {
    if (path.isEmpty()) {
        return path;
    }/*from  w w w  . ja v a2 s  .c o  m*/
    // String before / is considered empty string by splitter
    Iterable<String> splitter = Splitter.on("/").limit(6).split(path);
    Iterator<String> i = splitter.iterator();
    // skip first three - "","admin", "policies"
    i.next();
    i.next();
    i.next();
    // prop, cluster, namespace
    return Joiner.on("/").join(i.next(), i.next(), i.next());
}

From source file:com.github.rvesse.airline.utils.AirlineUtils.java

public static <T> List<T> listCopy(Iterable<T> iterable) {
    if (iterable == null)
        return new ArrayList<T>();
    return IteratorUtils.toList(iterable.iterator());
}

From source file:ivorius.ivtoolkit.tools.Pairs.java

public static <L, R> Iterable<Pair<L, R>> of(final Iterable<L> left, final Iterable<R> right) {
    return new FluentIterable<Pair<L, R>>() {
        @Override/*from  w  w w.  ja v a  2s. c om*/
        public Iterator<Pair<L, R>> iterator() {
            return new PairIterator<>(left.iterator(), right.iterator());
        }
    };
}

From source file:Main.java

/**
 * Returns the first element of the collection.
 * //  ww  w.  j a  v a 2  s . co m
 * @param <T>
 *            the element type
 * @param iterable
 *            the collection
 * @return the first element or null if the list is empty
 */
public static <T> T first(Iterable<T> iterable) {
    if (iterable instanceof Deque<?>)
        return ((Deque<T>) iterable).getFirst();
    if (iterable instanceof List<?>) {
        List<T> list = (List<T>) iterable;
        return list.isEmpty() ? null : list.get(0);
    }
    Iterator<T> iterator = iterable.iterator();
    return iterator.hasNext() ? iterator.next() : null;
}