@@ -5500,7 +5500,7 @@ public static List<Number> findIndexValues(Object self, Closure condition) {
55005500 * @since 1.5.2
55015501 */
55025502 public static List<Number> findIndexValues(Object self, Number startIndex, Closure condition) {
5503- return findIndexValues(InvokerHelper.asIterator(self), startIndex, condition);
5503+ return toList( findIndexValues(InvokerHelper.asIterator(self), startIndex, condition) );
55045504 }
55055505
55065506 /**
@@ -5510,10 +5510,12 @@ public static List<Number> findIndexValues(Object self, Number startIndex, Closu
55105510 * @param self an Iterator
55115511 * @param condition the matching condition
55125512 * @return a list of numbers corresponding to the index values of all matched objects
5513+ * @deprecated
55135514 * @since 2.5.0
55145515 */
5515- public static <T> List<Number> findIndexValues(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
5516- return findIndexValues(self, 0, condition);
5516+ @Deprecated
5517+ public static <T> List<Number> findIndexValues$$bridge(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
5518+ return toList(findIndexValues(self, 0, condition));
55175519 }
55185520
55195521 /**
@@ -5525,23 +5527,12 @@ public static <T> List<Number> findIndexValues(Iterator<T> self, @ClosureParams(
55255527 * @param startIndex start matching from this index
55265528 * @param condition the matching condition
55275529 * @return a list of numbers corresponding to the index values of all matched objects
5530+ * @deprecated
55285531 * @since 2.5.0
55295532 */
5530- public static <T> List<Number> findIndexValues(Iterator<T> self, Number startIndex, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
5531- List<Number> result = new ArrayList<>();
5532- long count = 0;
5533- long startCount = startIndex.longValue();
5534- BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
5535- while (self.hasNext()) {
5536- Object value = self.next();
5537- if (count++ < startCount) {
5538- continue;
5539- }
5540- if (bcw.call(value)) {
5541- result.add(count - 1);
5542- }
5543- }
5544- return result;
5533+ @Deprecated
5534+ public static <T> List<Number> findIndexValues$$bridge(Iterator<T> self, Number startIndex, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
5535+ return toList(findIndexValues(self, startIndex, condition));
55455536 }
55465537
55475538 /**
@@ -5569,7 +5560,104 @@ public static <T> List<Number> findIndexValues(Iterable<T> self, @ClosureParams(
55695560 * @since 2.5.0
55705561 */
55715562 public static <T> List<Number> findIndexValues(Iterable<T> self, Number startIndex, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
5572- return findIndexValues(self.iterator(), startIndex, condition);
5563+ return toList(findIndexValues(self.iterator(), startIndex, condition));
5564+ }
5565+
5566+ /**
5567+ * Returns an iterator of transformed values from the source iterator using the
5568+ * <code>transform</code> closure.
5569+ * <pre class="groovyTestCase">
5570+ * def letters = ('a'..'z').iterator()
5571+ * def vowels = 'aeiou'.toSet()
5572+ * assert letters.findIndexValues{ vowels.contains(it) }.toList() == [0, 4, 8, 14, 20]
5573+ * </pre>
5574+ *
5575+ * @param self an Iterator
5576+ * @param transform the closure used to transform each element
5577+ * @return an Iterator for the transformed values
5578+ * @since 5.0.0
5579+ */
5580+ public static <T> Iterator<Number> findIndexValues(
5581+ @DelegatesTo.Target Iterator<T> self,
5582+ @DelegatesTo(genericTypeIndex = 0)
5583+ @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> transform) {
5584+ return findIndexValues(self, 0, transform);
5585+ }
5586+
5587+ /**
5588+ * Returns an iterator of transformed values from the source iterator using the
5589+ * <code>transform</code> closure and starting with index <code>startIndex</code>.
5590+ *
5591+ * <pre class="groovyTestCase">
5592+ * def letters = ('a'..'z')
5593+ * def vowels = 'aeiou'.toSet()
5594+ * assert letters.iterator().findIndexValues(0){ vowels.contains(it) }.toList() == [0, 4, 8, 14, 20]
5595+ * assert letters.iterator().findIndexValues(1){ vowels.contains(it) }.toList() == [4, 8, 14, 20]
5596+ * </pre>
5597+ *
5598+ * @param self an Iterator
5599+ * @param startIndex start matching from this index
5600+ * @param transform the closure used to transform each element
5601+ * @return an Iterator for the transformed values
5602+ * @since 5.0.0
5603+ */
5604+ public static <T> Iterator<Number> findIndexValues(
5605+ @DelegatesTo.Target Iterator<T> self,
5606+ Number startIndex,
5607+ @DelegatesTo(genericTypeIndex = 0)
5608+ @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> transform) {
5609+ return new FindIndexValuesIterator<>(self, startIndex, transform);
5610+ }
5611+
5612+ private static final class FindIndexValuesIterator<T> implements Iterator<Number> {
5613+ private final Iterator<T> source;
5614+ private final BooleanClosureWrapper test;
5615+ private final long startCount;
5616+ private long count = 0;
5617+ private Number current;
5618+ private boolean exhausted;
5619+
5620+ private FindIndexValuesIterator(Iterator<T> source, Number startIndex, Closure<?> transform) {
5621+ this.source = source;
5622+ this.test = new BooleanClosureWrapper(transform);
5623+ this.startCount = startIndex.longValue();
5624+ this.exhausted = false;
5625+ advance();
5626+ }
5627+
5628+ @Override
5629+ public boolean hasNext() {
5630+ return !exhausted;
5631+ }
5632+
5633+ private void advance() {
5634+ while (source.hasNext()) {
5635+ Object value = source.next();
5636+ if (count++ < startCount) {
5637+ continue;
5638+ }
5639+ if (test.call(value)) {
5640+ current = count - 1;
5641+ return;
5642+ }
5643+ }
5644+ exhausted = true;
5645+ }
5646+
5647+ @Override
5648+ public Number next() {
5649+ if (!hasNext()) {
5650+ throw new NoSuchElementException("FindIndexValuesIterator has been exhausted and contains no more elements");
5651+ }
5652+ Number result = current;
5653+ advance();
5654+ return result;
5655+ }
5656+
5657+ @Override
5658+ public void remove() {
5659+ throw new UnsupportedOperationException();
5660+ }
55735661 }
55745662
55755663 //--------------------------------------------------------------------------
0 commit comments