List of usage examples for java.lang Iterable iterator
Iterator<T> iterator();
From source file:io.druid.indexing.overlord.TaskLifecycleTest.java
private static FirehoseFactory newMockFirehoseFactory(final Iterable<InputRow> inputRows) { return new FirehoseFactory() { @Override//from w w w. ja va2 s . co m public Firehose connect(InputRowParser parser) 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.splicemachine.orc.OrcTester.java
private static <T> Iterable<T> insertNullEvery(int n, Iterable<T> iterable) { return () -> new AbstractIterator<T>() { private final Iterator<T> delegate = iterable.iterator(); private int position; @Override/*from ww w .j a va 2s . c o m*/ protected T computeNext() { position++; if (position > n) { position = 0; return null; } if (!delegate.hasNext()) { return endOfData(); } return delegate.next(); } }; }
From source file:fr.landel.utils.commons.CollectionUtils2.java
/** * To array an iterable (take the type of the first element, otherwise use * the default 'toArray(new T[0])'). Returns {@code null} if the iterable is * empty.//from w w w .j ava 2 s. co m * * @param iterable * The input iterable (required) * @param type * the type of objects (optional, if null take the type of the * first element) * @param <T> * The type of object in collection * @return The array */ @SuppressWarnings("unchecked") public static <T> T[] toArray(final Iterable<T> iterable, final Class<T> type) { if (!IterableUtils.isEmpty(iterable)) { final Iterator<T> iterator = iterable.iterator(); final List<T> list = new ArrayList<>(); final Set<Class<T>> classes = new HashSet<>(); while (iterator.hasNext()) { final T obj = iterator.next(); list.add(obj); if (obj != null) { classes.add(ClassUtils.getClass(obj)); } } final Class<?> typeClass; if (type != null) { typeClass = type; } else if (classes.size() == 1) { typeClass = classes.iterator().next(); } else { typeClass = Object.class; } return list.toArray((T[]) Array.newInstance(typeClass, list.size())); } else if (iterable != null && type != null) { return (T[]) Array.newInstance(type, 0); } return null; }
From source file:com.google.cloud.genomics.dataflow.pipelines.CountReads.java
private static PCollection<Read> getReadsFromBAMFile() throws IOException { LOG.info("getReadsFromBAMFile"); final Iterable<Contig> contigs = Contig.parseContigsFromCommandLine(options.getReferences()); if (options.getShardBAMReading()) { LOG.info("Sharded reading of " + options.getBAMFilePath()); return ReadBAMTransform.getReadsFromBAMFilesSharded(p, auth, contigs, Collections.singletonList(options.getBAMFilePath())); } else { // For testing and comparing sharded vs. not sharded only LOG.info("Unsharded reading of " + options.getBAMFilePath()); return p.apply(Create .of(Reader.readSequentiallyForTesting(GCSOptions.Methods.createStorageClient(options, auth), options.getBAMFilePath(), contigs.iterator().next()))); }/*from ww w. j av a2 s .c o m*/ }
From source file:fr.landel.utils.commons.CollectionUtils2.java
/** * List all classes in an iterable ({@code null} values are excluded in the * output set)/*w ww . j a va 2 s.c o m*/ * * @param iterable * The iterable to check * @param <T> * The generic type of iterable * @return a list of classes, even if iterable is null or empty */ public static <T> Set<Class<T>> getClasses(final Iterable<T> iterable) { final Set<Class<T>> classes = new HashSet<>(); if (!IterableUtils.isEmpty(iterable)) { final Iterator<T> iterator = iterable.iterator(); while (iterator.hasNext()) { final T obj = iterator.next(); if (obj != null) { classes.add(ClassUtils.getClass(obj)); } } } return classes; }
From source file:com.hcc.cms.util.PageUtils.java
public static int doesNodeExist(Node content, String nodeName) { try {//from w ww .j a va2 s.c o m int childRecs = 0; java.lang.Iterable<Node> requiredRoot = JcrUtils.getChildNodes(content, nodeName); Iterator<Node> rootitr = requiredRoot.iterator(); // only going to be 1 content/nodeName node if it exists if (rootitr.hasNext()) { // Count the number of child nodes in content/customer Node requiredRootNode = content.getNode(nodeName); Iterable<Node> itCust = JcrUtils.getChildNodes(requiredRootNode); Iterator<Node> childNodeIt = itCust.iterator(); // Count the number of nodeName child nodes while (childNodeIt.hasNext()) { childRecs++; childNodeIt.next(); } return childRecs; } else return -1; // required node does not exist } catch (Exception e) { log.error("Exception occured while checking not into the JCR: " + e); } return 0; }
From source file:backtype.storm.utils.Utils.java
public static <T> String join(Iterable<T> coll, String sep) { Iterator<T> it = coll.iterator(); String ret = ""; while (it.hasNext()) { ret = ret + it.next();/*w ww .ja v a 2 s. com*/ if (it.hasNext()) { ret = ret + sep; } } return ret; }
From source file:com.joyent.manta.util.MantaUtils.java
/** * Naively converts a collection of objects to a single CSV string. * Warning: this doesn't escape.//from www.j a va 2s.c o m * @param stringable collection of objects with implemented toString methods * @return CSV string or empty string */ public static String csv(final Iterable<?> stringable) { if (stringable == null) { return ""; } final StringBuilder builder = new StringBuilder(); Iterator<?> itr = stringable.iterator(); while (itr.hasNext()) { final Object o = itr.next(); if (o == null) { continue; } final String value; if (o instanceof InetAddress) { value = ((InetAddress) o).getHostAddress(); } else if (o instanceof Map) { StringBuilder sb = new StringBuilder(); @SuppressWarnings({ "unchecked", "rawtypes" }) final Map map = (Map) o; @SuppressWarnings("unchecked") final Iterator<Map.Entry<?, ?>> mapItr = (Iterator<Map.Entry<?, ?>>) map.entrySet().iterator(); while (mapItr.hasNext()) { Map.Entry<?, ?> entry = mapItr.next(); sb.append("[").append(String.valueOf(entry.getKey())).append("=").append(entry.getValue()) .append("]"); if (mapItr.hasNext()) { sb.append(" "); } } value = sb.toString(); } else { value = o.toString(); } // Strip any commas out of the string builder.append(StringUtils.replaceChars(value, ',', ' ')); if (itr.hasNext()) { builder.append(", "); } } return builder.toString(); }
From source file:br.msf.commons.util.CollectionUtils.java
public static <T> T get(final Iterable<T> iterable, final int index) { ArgumentUtils.rejectIfLessThan(index, 0); if (iterable instanceof List<?>) { return ((List<T>) iterable).get(index); }/*from ww w . j a va2s . c om*/ return get(iterable.iterator(), index); }
From source file:com.bitranger.parknshop.common.recommend.collections.CollectionUtils.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <E> Iterable<E> fast(final Iterable<E> iter) { if (iter instanceof FastIterable) { return new Iterable<E>() { @Override/*from w w w . j a va 2 s. c om*/ public Iterator<E> iterator() { return ((FastIterable) iter).fastIterator(); } }; } else if (iter instanceof Cursor) { throw new IllegalArgumentException(); } else { Optional<Method> fastMethod = fastIteratorMethods.getUnchecked(iter.getClass()); if (fastMethod.isPresent()) { final Method method = fastMethod.get(); return new Iterable<E>() { @Override public Iterator<E> iterator() { try { return (Iterator<E>) method.invoke(iter); } catch (IllegalAccessException e) { return iter.iterator(); } catch (InvocationTargetException e) { throw Throwables.propagate(e.getCause()); } } }; } else { return iter; } } }