List of usage examples for java.lang Iterable iterator
Iterator<T> iterator();
From source file:Main.java
/** * Returns true if the given iterable is null or empty. * * @param iterable/*from ww w . j a v a 2 s. c om*/ * The iterable to determine if it is null or empty. * @return * True if the given iterable is null or empty. */ public static boolean isEmpty(final Iterable<?> iterable) { if (iterable == null) { // It is null, so it is empty. return true; } else if (iterable instanceof Collection) { return ((Collection<?>) iterable).isEmpty(); } else { return !iterable.iterator().hasNext(); } }
From source file:edu.byu.nlp.util.Iterables2.java
public static <E> Iterable<Enumeration<E>> enumerate(final Iterable<E> iterable) { return new Iterable<Enumeration<E>>() { @Override//from w ww .j a v a 2s. co m public Iterator<Enumeration<E>> iterator() { return Iterators2.enumerate(iterable.iterator()); } }; }
From source file:Main.java
/** * Returns the <code>index</code>-th value in the <code>iterable</code>'s {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * <p>// w w w .jav a 2 s . c om * If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}. * * @param iterable the {@link Iterable} to get a value from * @param index the index to get * @param <T> the type of object in the {@link Iterable}. * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid */ public static <T> T get(final Iterable<T> iterable, final int index) { checkIndexBounds(index); if (iterable instanceof List<?>) { return ((List<T>) iterable).get(index); } return get(iterable.iterator(), index); }
From source file:Main.java
/** * Returns the last element of the collection. * // w w w . jav a2 s . co m * @param <T> * the element type * @param iterable * the collection * @return the last element or null if the list is empty */ public static <T> T last(Iterable<T> iterable) { if (iterable instanceof Deque<?>) return ((Deque<T>) iterable).getLast(); if (iterable instanceof List<?>) { List<T> list = (List<T>) iterable; return list.isEmpty() ? null : list.get(list.size() - 1); } Iterator<T> iterator = iterable.iterator(); T last = null; while (iterator.hasNext()) last = iterator.next(); return last; }
From source file:com.github.rvesse.airline.utils.AirlineUtils.java
public static <T> List<T> unmodifiableListCopy(Iterable<T> iterable) { if (iterable == null) return Collections.emptyList(); return ListUtils.unmodifiableList(IteratorUtils.toList(iterable.iterator())); }
From source file:Main.java
/** * Returns an iterator over the values referenced by the elements of {@code * iterable}.// w w w. j a va 2 s .c o m * * @param trim true to remove reference objects from the iterable after * their referenced values have been cleared. */ public static <T> Iterable<T> dereferenceIterable(final Iterable<? extends Reference<T>> iterable, final boolean trim) { return new Iterable<T>() { public Iterator<T> iterator() { return new Iterator<T>() { private final Iterator<? extends Reference<T>> delegate = iterable.iterator(); private boolean removeIsOkay; private T next; private void computeNext() { removeIsOkay = false; while (next == null && delegate.hasNext()) { next = delegate.next().get(); if (trim && next == null) { delegate.remove(); } } } @Override public boolean hasNext() { computeNext(); return next != null; } @Override public T next() { if (!hasNext()) { throw new IllegalStateException(); } T result = next; removeIsOkay = true; next = null; return result; } public void remove() { if (!removeIsOkay) { throw new IllegalStateException(); } delegate.remove(); } }; } }; }
From source file:com.networknt.light.server.DbService.java
public static long getCount(String className, Map<String, Object> criteria) { long count = 0; StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM ").append(className); String whereClause = DbService.getWhereClause(criteria); if (whereClause != null && whereClause.length() > 0) { sql.append(whereClause);//from w w w . j av a 2 s . c o m } logger.debug("sql={}", sql); OrientGraph graph = ServiceLocator.getInstance().getGraph(); try { Iterable<Vertex> it = graph.command(new OCommandSQL(sql.toString())).execute(); if (it != null) { Vertex v = it.iterator().next(); count = v.getProperty("count"); } } catch (Exception e) { logger.error("Exception:", e); throw e; } finally { graph.shutdown(); } return count; }
From source file:forge.deck.CardPool.java
public static CardPool fromCardList(final Iterable<String> lines) { CardPool pool = new CardPool(); if (lines == null) { return pool; }/*ww w . ja v a2s . c o m*/ final Iterator<String> lineIterator = lines.iterator(); while (lineIterator.hasNext()) { final String line = lineIterator.next(); if (line.startsWith(";") || line.startsWith("#")) { continue; } // that is a comment or not-yet-supported card final Matcher m = p.matcher(line.trim()); m.matches(); final String sCnt = m.group(2); final String cardName = m.group(3); if (StringUtils.isBlank(cardName)) { continue; } final int count = sCnt == null ? 1 : Integer.parseInt(sCnt); pool.add(cardName, count); } return pool; }
From source file:com.thoughtworks.studios.journey.utils.IterableUtils.java
public static <FROM, TO> Iterable<TO> flatMap(final Function<FROM, Iterable<TO>> function, Iterable<FROM> from) { Iterator<TO> iterator = Iterables.flatMap(new Function<FROM, Iterator<TO>>() { @Override//ww w . j a v a2 s .c o m public Iterator<TO> apply(FROM o) { return function.apply(o).iterator(); } }, from.iterator()); return toIterable(iterator); }
From source file:com.github.rvesse.airline.utils.AirlineUtils.java
public static <T> Set<T> unmodifiableSetCopy(Iterable<T> iterable) { if (iterable == null) return Collections.emptySet(); LinkedHashSet<T> set = new LinkedHashSet<T>(); Iterator<T> iter = iterable.iterator(); while (iter.hasNext()) { set.add(iter.next());/*from w ww . j a v a 2 s. c o m*/ } return Collections.unmodifiableSet(set); }