public static int totalLengthWithMvccVersion(final Iterable<? extends KeyValue> kvs, final boolean includeMvccVersion) { int length = 0; for (KeyValue kv : IterableUtils.emptyIfNull(kvs)) { length += lengthWithMvccVersion(kv, includeMvccVersion); } return length; }
/** * Counts the number of elements in the input iterable that match the predicate. * <p> * A <code>null</code> iterable matches no elements. * * @param <E> the type of object the {@link Iterable} contains * @param input the {@link Iterable} to get the input from, may be null * @param predicate the predicate to use, may not be null * @return the number of matches for the predicate in the collection * @throws NullPointerException if predicate is null */ public static <E> long countMatches(final Iterable<E> input, final Predicate<? super E> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null."); } return size(filteredIterable(emptyIfNull(input), predicate)); }
/** * Returns a unique view of the given iterable. * <p> * The returned iterable's iterator supports {@code remove()} when the * corresponding input iterator supports it. Calling {@code remove()} * will only remove a single element from the underlying iterator. * * @param <E> the element type * @param iterable the iterable to use, may not be null * @return a unique view of the specified iterable * @throws NullPointerException if iterable is null */ public static <E> Iterable<E> uniqueIterable(final Iterable<E> iterable) { checkNotNull(iterable); return new FluentIterable<E>() { @Override public Iterator<E> iterator() { return new UniqueFilterIterator<E>(iterable.iterator()); } }; }
/** * Combines two iterables into a single iterable. * <p> * The returned iterable has an iterator that traverses the elements in {@code a}, * followed by the elements in {@code b}. The source iterators are not polled until * necessary. * <p> * The returned iterable's iterator supports {@code remove()} when the corresponding * input iterator supports it. * * @param <E> the element type * @param a the first iterable, may not be null * @param b the second iterable, may not be null * @return a new iterable, combining the provided iterables * @throws NullPointerException if either a or b is null */ @SuppressWarnings("unchecked") public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a, final Iterable<? extends E> b) { return chainedIterable(new Iterable[] {a, b}); }
/** * Returns a new FluentIterable whose iterator will traverse the * elements of the current and provided iterable in natural order. * <p> * Example: natural ordering * <ul> * <li>this contains elements [1, 3, 5, 7] * <li>other contains elements [2, 4, 6, 8] * </ul> * <p> * The returned iterable will traverse the elements in the following * order: [1, 2, 3, 4, 5, 6, 7, 8] * * @param other the other iterable to collate, may not be null * @return a new iterable, collating this iterable with the other in natural order * @throws NullPointerException if other is null * @see {@link org.apache.hbase.thirdparty.org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator} */ public FluentIterable<E> collate(final Iterable<? extends E> other) { return of(IterableUtils.collatedIterable(iterable, other)); }
/** * Checks if the object is contained in this iterable. * * @param object the object to check * @return true if the object is contained in this iterable, false otherwise */ public boolean contains(final Object object) { return IterableUtils.contains(iterable, object); }
/** * Counts the number of elements in the input collection that match the * predicate. * <p> * A <code>null</code> collection or predicate matches no elements. * * @param <C> the type of object the {@link Iterable} contains * @param input the {@link Iterable} to get the input from, may be null * @param predicate the predicate to use, may be null * @return the number of matches for the predicate in the collection * @deprecated since 4.1, use {@link IterableUtils#countMatches(Iterable, Predicate)} instead */ @Deprecated public static <C> int countMatches(final Iterable<C> input, final Predicate<? super C> predicate) { return predicate == null ? 0 : (int) IterableUtils.countMatches(input, predicate); }
/** * Returns a new FluentIterable whose iterator will return at most * the provided maximum number of elements from this iterable. * * @param maxSize the maximum number of elements * @return a new iterable, providing a bounded view of this iterable * @throws IllegalArgumentException if maxSize is negative */ public FluentIterable<E> limit(final long maxSize) { return of(IterableUtils.boundedIterable(iterable, maxSize)); }
/** * Returns the number of occurrences of the provided object in the iterable. * * @param <E> the element type that the {@link Iterable} may contain * @param <T> the element type of the object to find * @param iterable the {@link Iterable} to search * @param obj the object to find the cardinality of * @return the the number of occurrences of obj in iterable */ public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }
/** * Combines the two provided iterables into an ordered iterable using * natural ordering. * <p> * The returned iterable's iterator supports {@code remove()} when the * corresponding input iterator supports it. * * @param <E> the element type * @param a the first iterable, may not be null * @param b the second iterable, may not be null * @return a filtered view on the specified iterable * @throws NullPointerException if either of the provided iterables is null */ public static <E> Iterable<E> collatedIterable(final Iterable<? extends E> a, final Iterable<? extends E> b) { checkNotNull(a, b); return new FluentIterable<E>() { @Override public Iterator<E> iterator() { return IteratorUtils.collatedIterator(null, a.iterator(), b.iterator()); } }; }
/** * Combines three iterables into a single iterable. * <p> * The returned iterable has an iterator that traverses the elements in {@code a}, * followed by the elements in {@code b} and {@code c}. The source iterators are * not polled until necessary. * <p> * The returned iterable's iterator supports {@code remove()} when the corresponding * input iterator supports it. * * @param <E> the element type * @param a the first iterable, may not be null * @param b the second iterable, may not be null * @param c the third iterable, may not be null * @return a new iterable, combining the provided iterables * @throws NullPointerException if either of the provided iterables is null */ @SuppressWarnings("unchecked") public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a, final Iterable<? extends E> b, final Iterable<? extends E> c) { return chainedIterable(new Iterable[] {a, b, c}); }
/** * Returns a new FluentIterable whose iterator will traverse the * elements of the current and provided iterable according to the * ordering defined by an comparator. * <p> * Example: descending order * <ul> * <li>this contains elements [7, 5, 3, 1] * <li>other contains elements [8, 6, 4, 2] * </ul> * <p> * The returned iterable will traverse the elements in the following * order: [8, 7, 6, 5, 4, 3, 2, 1] * * @param comparator the comparator to define an ordering, may be null, * in which case natural ordering will be used * @param other the other iterable to collate, may not be null * @return a new iterable, collating this iterable with the other in natural order * @throws NullPointerException if other is null * @see {@link org.apache.hbase.thirdparty.org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator} */ public FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator) { return of(IterableUtils.collatedIterable(comparator, iterable, other)); }
public static ByteBuffer toByteBufferAndRewind(final Iterable<? extends KeyValue> kvs, boolean includeMemstoreTS) { int totalBytes = KeyValueUtil.totalLengthWithMvccVersion(kvs, includeMemstoreTS); ByteBuffer bb = ByteBuffer.allocate(totalBytes); for (KeyValue kv : IterableUtils.emptyIfNull(kvs)) { KeyValueUtil.appendToByteBuffer(bb, kv, includeMemstoreTS); } bb.rewind(); return bb; }
/** * Returns a reversed view of the given iterable. * <p> * In case the provided iterable is a {@link List} instance, a * {@link ReverseListIterator} will be used to reverse the traversal * order, otherwise an intermediate {@link List} needs to be created. * <p> * The returned iterable's iterator supports {@code remove()} if the * provided iterable is a {@link List} instance. * * @param <E> the element type * @param iterable the iterable to use, may not be null * @return a reversed view of the specified iterable * @throws NullPointerException if iterable is null * @see ReverseListIterator */ public static <E> Iterable<E> reversedIterable(final Iterable<E> iterable) { checkNotNull(iterable); return new FluentIterable<E>() { @Override public Iterator<E> iterator() { final List<E> list = (iterable instanceof List<?>) ? (List<E>) iterable : IteratorUtils.toList(iterable.iterator()); return new ReverseListIterator<E>(list); } }; }
/** * Combines four iterables into a single iterable. * <p> * The returned iterable has an iterator that traverses the elements in {@code a}, * followed by the elements in {@code b}, {@code c} and {@code d}. The source * iterators are not polled until necessary. * <p> * The returned iterable's iterator supports {@code remove()} when the corresponding * input iterator supports it. * * @param <E> the element type * @param a the first iterable, may not be null * @param b the second iterable, may not be null * @param c the third iterable, may not be null * @param d the fourth iterable, may not be null * @return a new iterable, combining the provided iterables * @throws NullPointerException if either of the provided iterables is null */ @SuppressWarnings("unchecked") public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a, final Iterable<? extends E> b, final Iterable<? extends E> c, final Iterable<? extends E> d) { return chainedIterable(new Iterable[] {a, b, c, d}); }
for (HStoreFile file : IterableUtils.emptyIfNull(compactedFiles)) { name2File.put(file.getFileInfo().getActiveFileName(), file);
/** * Combines the two provided iterables into an ordered iterable using the * provided comparator. If the comparator is null, natural ordering will be * used. * <p> * The returned iterable's iterator supports {@code remove()} when the * corresponding input iterator supports it. * * @param <E> the element type * @param comparator the comparator defining an ordering over the elements, * may be null, in which case natural ordering will be used * @param a the first iterable, may not be null * @param b the second iterable, may not be null * @return a filtered view on the specified iterable * @throws NullPointerException if either of the provided iterables is null */ public static <E> Iterable<E> collatedIterable(final Comparator<? super E> comparator, final Iterable<? extends E> a, final Iterable<? extends E> b) { checkNotNull(a, b); return new FluentIterable<E>() { @Override public Iterator<E> iterator() { return IteratorUtils.collatedIterator(comparator, a.iterator(), b.iterator()); } }; }
/** * Returns a new FluentIterable whose iterator will first traverse * the elements of the current iterable, followed by the elements * of the provided iterable. * * @param other the other iterable to combine, may not be null * @return a new iterable, combining this iterable with other * @throws NullPointerException if other is null */ public FluentIterable<E> append(final Iterable<? extends E> other) { return of(IterableUtils.chainedIterable(iterable, other)); }
public static int totalLengthWithMvccVersion(final Iterable<? extends KeyValue> kvs, final boolean includeMvccVersion) { int length = 0; for (KeyValue kv : IterableUtils.emptyIfNull(kvs)) { length += lengthWithMvccVersion(kv, includeMvccVersion); } return length; }
/** * Fail-fast check for null arguments. * * @param iterable the iterable to check * @throws NullPointerException if the argument or any of its contents is null */ static void checkNotNull(final Iterable<?>... iterables) { if (iterables == null) { throw new NullPointerException("Iterables must not be null."); } for (final Iterable<?> iterable : iterables) { checkNotNull(iterable); } }