Do blog de Neal Gafter:
I’m pleased to announce that the Java Closures prototype now supports all of the features of its specification!
The complete source code, released under GPLv2, is in the project’s openjdk repository. A binary build, suitable for use with an existing JDK6, is at http://www.javac.info/closures.tar.gz. Other related documents are on the website http://www.javac.info/.
Mais informações em: http://gafter.blogspot.com/2008/08/java-closures-prototype-feature.html
E para quem quiser conhecer um pouco mais da proposta de closures: http://www.parleys.com/display/PARLEYS/Home#talk=2097237;;slide=1
Baixei o protótipo e brinquei um pouquinho…
[code]
public class HelloClosures1 {
public static void main(String args[]) {
{ int, int => int } soma = { int a, int b => a + b };
System.out.println(soma.invoke(1,2));
}
}
[/code]Saída: 3
[code]
import java.util.*;
interface ListClosure extends List {
public ListClosure extract({ E => boolean } block);
}
class ArrayListClosure extends ArrayList implements ListClosure {
public ListClosure<E> extract({ E => boolean } block) {
ListClosure<E> lista = new ArrayListClosure<E>();
for (E elem : this) {
if (block.invoke(elem)) {
lista.add(elem);
}
}
return lista;
}
}
public class HelloClosure2 {
public static void main(String args[]) {
Integer[] numeros = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ListClosure<Integer> lista = new ArrayListClosure<Integer>();
lista.addAll(Arrays.asList(numeros));
ListClosure<Integer> filtrada = lista.extract({ Integer a => a % 2 == 0 });
System.out.println(filtrada);
}
}
[/code]Saída: [0, 2, 4, 6, 8, 10]