/** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default IndexedConsumer<T> andThen( IndexedConsumer<? super T> after ) { Objects.requireNonNull( after ); return ( int index, T t ) -> { accept( index, t ); after.accept( index, t ); }; }
/** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default IndexedConsumer<T> andThen( IndexedConsumer<? super T> after ) { Objects.requireNonNull( after ); return ( int index, T t ) -> { accept( index, t ); after.accept( index, t ); }; }
/** * Performs the given {@code action} on each element, providing sequential index with the element. * * @param action function that takes the index of an element and the element itself * and performs the desired action on the element. */ public static <T> void forEachIndexed( @This Iterable<T> thiz, IndexedConsumer<T> action ) { int index = 0; for( T item : thiz ) { action.accept( index++, item ); } }