/** * Tests whether every element of this iterable relates to the corresponding element of another iterable by * satisfying a test predicate. * * @param <U> Component type of that iterable * @param that the other iterable * @param predicate the test predicate, which relates elements from both iterables * @return {@code true} if both iterables have the same length and {@code predicate(x, y)} * is {@code true} for all corresponding elements {@code x} of this iterable and {@code y} of {@code that}, * otherwise {@code false}. */ default <U> boolean corresponds(Iterable<U> that, BiPredicate<? super T, ? super U> predicate) { final java.util.Iterator<T> it1 = iterator(); final java.util.Iterator<U> it2 = that.iterator(); while (it1.hasNext() && it2.hasNext()) { if (!predicate.test(it1.next(), it2.next())) { return false; } } return !it1.hasNext() && !it2.hasNext(); }
@Override default Spliterator<T> spliterator() { return Spliterators.spliterator(iterator(), isEmpty() ? 0 : 1, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED); }
/** * Converts this to a {@link CharSeq}. * * @return A new {@link CharSeq}. */ default CharSeq toCharSeq() { if (this instanceof CharSeq) { return (CharSeq) this; } else if (isEmpty()) { return CharSeq.empty(); } else { return CharSeq.of(iterator().mkString()); } }
/** * Converts this to a Java array with component type {@code Object} * * <pre>{@code * // = [] of type Object[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray() * * // = [ok] of type Object[] * Try.of(() -> "ok") * .toJavaArray() * * // = [1, 2, 3] of type Object[] * List.of(1, 2, 3) * .toJavaArray() * }</pre> * * @return A new Java array. */ default Object[] toJavaArray() { if ((this instanceof Traversable<?>) && ((Traversable<?>) this).isTraversableAgain()) { final Object[] results = new Object[((Traversable<T>) this).size()]; final Iterator<T> iter = iterator(); Arrays.setAll(results, i -> iter.next()); return results; } else { return toJavaList().toArray(); } }
/** * Tests whether every element of this iterable relates to the corresponding element of another iterable by * satisfying a test predicate. * * @param <U> Component type of that iterable * @param that the other iterable * @param predicate the test predicate, which relates elements from both iterables * @return {@code true} if both iterables have the same length and {@code predicate(x, y)} * is {@code true} for all corresponding elements {@code x} of this iterable and {@code y} of {@code that}, * otherwise {@code false}. */ default <U> boolean corresponds(Iterable<U> that, BiPredicate<? super T, ? super U> predicate) { final java.util.Iterator<T> it1 = iterator(); final java.util.Iterator<U> it2 = that.iterator(); while (it1.hasNext() && it2.hasNext()) { if (!predicate.test(it1.next(), it2.next())) { return false; } } return !it1.hasNext() && !it2.hasNext(); }
@Override default Spliterator<T> spliterator() { return Spliterators.spliterator(iterator(), isEmpty() ? 0 : 1, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED); }
/** * Converts this to a {@link CharSeq}. * * @return A new {@link CharSeq}. */ default CharSeq toCharSeq() { if (this instanceof CharSeq) { return (CharSeq) this; } else if (isEmpty()) { return CharSeq.empty(); } else { return CharSeq.of(iterator().mkString()); } }
/** * Converts this to a Java array with component type {@code Object} * * <pre>{@code * // = [] of type Object[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray() * * // = [ok] of type Object[] * Try.of(() -> "ok") * .toJavaArray() * * // = [1, 2, 3] of type Object[] * List.of(1, 2, 3) * .toJavaArray() * }</pre> * * @return A new Java array. */ default Object[] toJavaArray() { if ((this instanceof Traversable<?>) && ((Traversable<?>) this).isTraversableAgain()) { final Object[] results = new Object[((Traversable<T>) this).size()]; final Iterator<T> iter = iterator(); Arrays.setAll(results, i -> iter.next()); return results; } else { return toJavaList().toArray(); } }