Skip to content

Commit 5916bc5

Browse files
committed
Add withIndex/indexed methods for ArrayGroovyMethods (add missing variants for T[])
1 parent 14fcb08 commit 5916bc5

1 file changed

Lines changed: 76 additions & 2 deletions

File tree

src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4664,7 +4664,7 @@ public static Map<Integer, Double> indexed(double[] self) {
46644664
* assert ["1: 10", "2: 20", "3: 30"] == nums.indexed(1).collect { idx, str {@code ->} "$idx: $str" }
46654665
* </pre>
46664666
*
4667-
* @param self an Iterable
4667+
* @param self an int array
46684668
* @param offset an index to start from
46694669
* @return a Map (since the keys/indices are unique) containing the elements from the iterable zipped with indices
46704670
* @see DefaultGroovyMethods#indexed(Iterable, int)
@@ -4683,7 +4683,7 @@ public static Map<Integer, Integer> indexed(int[] self, int offset) {
46834683
* assert [5: 10L, 6: 20L, 7: 30L] == nums.indexed(5)
46844684
* </pre>
46854685
*
4686-
* @param self a long[]
4686+
* @param self a long array
46874687
* @param offset an index to start from
46884688
* @return a Map (since the keys/indices are unique) containing the elements from the iterable zipped with indices
46894689
* @see DefaultGroovyMethods#indexed(Iterable, int)
@@ -4731,6 +4731,42 @@ public static Map<Integer, Double> indexed(double[] self, int offset) {
47314731
return DefaultGroovyMethods.indexed(new DoubleArrayIterable(self), offset);
47324732
}
47334733

4734+
/**
4735+
* Zips an object array with indices in (index, value) order starting from index 0.
4736+
* <p>
4737+
* Example usage:
4738+
* <pre class="groovyTestCase">
4739+
* String[] letters = 'A'..'C'
4740+
* assert [0: 'A', 1: 'B', 2: 'C'] == letters.indexed()
4741+
* </pre>
4742+
*
4743+
* @see #indexed(Object[], int)
4744+
* @since 5.0.0
4745+
*/
4746+
public static <T> Map<Integer, T> indexed(T[] self) {
4747+
return indexed(self, 0);
4748+
}
4749+
4750+
/**
4751+
* Zips an object array with indices in (index, value) order starting from a given index.
4752+
* <p>
4753+
* Example usage:
4754+
* <pre class="groovyTestCase">
4755+
* String[] letters = 'A'..'C'
4756+
* assert [5: 'A', 6: 'B', 7: 'C'] == letters.indexed(5)
4757+
* assert ["1: A", "2: B", "3: C"] == letters.indexed(1).collect { idx, str {@code ->} "$idx: $str" }
4758+
* </pre>
4759+
*
4760+
* @param self an Object array
4761+
* @param offset an index to start from
4762+
* @return a Map (since the keys/indices are unique) containing the elements from the iterable zipped with indices
4763+
* @see DefaultGroovyMethods#indexed(Iterable, int)
4764+
* @since 5.0.0
4765+
*/
4766+
public static <T> Map<Integer, T> indexed(T[] self, int offset) {
4767+
return DefaultGroovyMethods.indexed(new ArrayIterable<>(self), offset);
4768+
}
4769+
47344770
//--------------------------------------------------------------------------
47354771
// init
47364772

@@ -10685,6 +10721,44 @@ public static List<Tuple2<Double, Integer>> withIndex(double[] self, int offset)
1068510721
return DefaultGroovyMethods.withIndex(new DoubleArrayIterable(self), offset);
1068610722
}
1068710723

10724+
/**
10725+
* Zips an object array with indices in (value, index) order.
10726+
* <p/>
10727+
* Example usage:
10728+
* <pre class="groovyTestCase">
10729+
* String[] letters = ['a', 'z']
10730+
* assert [['a', 0], ['z', 1]] == letters.withIndex()
10731+
* </pre>
10732+
*
10733+
* @param self an object array
10734+
* @return a zipped list with indices
10735+
* @see DefaultGroovyMethods#withIndex(Iterable)
10736+
* @since 5.0.0
10737+
*/
10738+
public static <T> List<Tuple2<T, Integer>> withIndex(T[] self) {
10739+
return withIndex(self, 0);
10740+
}
10741+
10742+
/**
10743+
* Zips an object array with indices in (value, index) order starting from a given index.
10744+
* <p/>
10745+
* Example usage:
10746+
* <pre class="groovyTestCase">
10747+
* String[] letters = ['a', 'z']
10748+
* assert [['a', 5], ['z', 6]] == letters.withIndex(5)
10749+
* assert ["5: a", "6: z"] == letters.withIndex(5).collect { n, idx {@code ->} "$idx: $n" }
10750+
* </pre>
10751+
*
10752+
* @param self an object array
10753+
* @param offset an index to start from
10754+
* @return a zipped list with indices
10755+
* @see DefaultGroovyMethods#withIndex(Iterable, int)
10756+
* @since 5.0.0
10757+
*/
10758+
public static <T> List<Tuple2<T, Integer>> withIndex(T[] self, int offset) {
10759+
return DefaultGroovyMethods.withIndex(new ArrayIterable<>(self), offset);
10760+
}
10761+
1068810762
//--------------------------------------------------------------------------
1068910763
// zip
1069010764

0 commit comments

Comments
 (0)