Hi Uwe,
> Am 11.05.2016 um 21:35 schrieb Uwe Schindler <[email protected]>:
> With Java 9 this gets a bit worse: There is no "easy way" with the
> MethodHanldes class to generate a MethodHandles.countedLoop() on all elements
> of an array:
>
> public static MethodHandle countedLoop(MethodHandle iterations, MethodHandle
> init, MethodHandle body) [new in Java 9]
this isn't a remedy when you need the index in each iteration, but you can
generate a loop over all elements of an array using iteratedLoop(), as
illustrated by this test from LoopCombinatorTest:
@Test
public static void testIterateSum() throws Throwable {
// Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) {
sum += e; } return sum; => 21
MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator,
Iterate.MH_sumInit, Iterate.MH_sumStep);
assertEquals(Iterate.MT_sum, loop.type());
assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6}));
}
... where MH_sumIterator is a handle to this method:
static Iterator<Integer> sumIterator(Integer[] a) {
return Arrays.asList(a).iterator();
}
Best,
Michael
--
<http://www.oracle.com/>
Dr. Michael Haupt | Principal Member of Technical Staff
Phone: +49 331 200 7277 | Fax: +49 331 200 7561
Oracle Java Platform Group | LangTools Team | Nashorn
Oracle Deutschland B.V. & Co. KG | Schiffbauergasse 14 | 14467 Potsdam, Germany
ORACLE Deutschland B.V. & Co. KG | Hauptverwaltung: Riesstraße 25, D-80992
München
Registergericht: Amtsgericht München, HRA 95603
Komplementärin: ORACLE Deutschland Verwaltung B.V. | Hertogswetering 163/167,
3543 AS Utrecht, Niederlande
Handelsregister der Handelskammer Midden-Nederland, Nr. 30143697
Geschäftsführer: Alexander van der Ven, Jan Schultheiss, Val Maher
<http://www.oracle.com/commitment> Oracle is committed to developing
practices and products that help protect the environment