Sep 11, 2008
[Dolphin] J2SE 7.0 の closure で遊んでみた
先日構築した J2SE 7.0 EA で closure の動作環境で早速遊んでみた。
サンプルコード(0)
まずは引数を受け取って戻り値を返す closure。
public class Test2 {
public Test2(){
super();
}
public static void main(final String[] args) {
int result = {int arg => arg * arg}.invoke(3);
System.out.println(result);
}
}
>java.bat Test2 9
サンプルコード(1)
次に、参照型の引数を受け取って、参照型の戻り値を返す closure。
public class Test3 {
public Test3(){
super();
}
public static void main(final String[] args) {
Comparee c0 = new Comparee(0);
Comparee c1 = new Comparee(1);
Comparee result = {Comparee c0, Comparee c1 =>
c0.getValue() >= c1.getValue() ? c0 : c1}.invoke(c0, c1);
System.out.println("" + result.getValue());
}
public static class Comparee {
private final int value;
public Comparee(final int value){
super();
this.value = value;
}
public int getValue(){
return this.value;
}
}
}
>java.bat Test3 1
サンプルコード(2)
既存のインタフェース(Comparable)を暗黙で実装したとみなされる closure。 簡単な Comparable の実装は楽になるな。
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test4 {
public Test4(){
super();
}
public static void main(final String[] args) {
List<Comparee> list = new ArrayList<Comparee>();
list.add(new Comparee(1));
list.add(new Comparee(3));
list.add(new Comparee(0));
list.add(new Comparee(2));
Comparator<Comparee> comparator =
{Comparee c0, Comparee c1 => c0.getValue() - c1.getValue()};
System.out.println("" + list);
Collections.sort(list, comparator);
System.out.println("" + list);
}
public static class Comparee {
private final int value;
public Comparee(final int value){
super();
this.value = value;
}
public int getValue(){
return this.value;
}
public String toString(){
return "" + this.value;
}
}
}
>java.bat Test4 [1, 3, 0, 2] [0, 1, 2, 3]
サンプルコード(3)
closure を引数で受け取るメソッド。
public class Test5 {
public Test5(){
super();
}
public static void main(final String[] args) {
invoker({String message, int count =>
for(int i = 0; i < count; i++){
System.out.print("" + message);
}
System.out.println();
});
invoker({String message, int count =>
for(int i = 0; i < count; i++){
System.out.print("-" + message);
}
System.out.println();
});
}
public static void invoker({String, int => void} invokee){
invokee.invoke("*", 5);
}
}
>java.bat Test5 ***** -*-*-*-*-*
サンプルコード(4)
closure を実装するクラス。
public class Test6 {
public Test6(){
super();
}
public static void main(final String[] args) {
check(new MyChecker(), 5);
check(new MyChecker(), -1);
}
public static void check(final {int => boolean} f, final int i) {
System.out.println("" + f.invoke(i));
}
public static class MyChecker implements {int => boolean} {
public MyChecker(){
super();
}
public boolean invoke(final int i) {
return i < 0;
}
}
}
>java.bat Test6 false true
Sep 09, 2008
[Dolphin] J2SE 7.0 EA で closure 環境を用意する。
そろそろ closure を実際に使用しておこうかと思い、JDK 7.0 EA をダウンロードして closure 環境を用意してみた。 現時点(2008/09/09)では JDK 単体では closure は利用できず、別途 closure ライブラリを用意する必要がある様だ。
- Closures for the Java Programming Language (aka BGGA)
- http://javac.info/
- JSR Proposal: Closures for Java
- http://www.javac.info/consensus-closures-jsr.html
- JDK 7 Project
- https://jdk7.dev.java.net/
- Java Platform, Standard Edition 7 Binary Snapshot Releases
- http://download.java.net/jdk7/binaries/
closure 環境の準備
まずは JDK 7.0 EA をダウンロードする。 今回使用したのは現時点で最新と思われる jdk-7-ea-bin-b34-windows-i586-28_aug_2008.exe。 closure ライブラリは javac.info から closures.tar.gz をダウンロードする。
JDK 7.0 EA 単体では closure は利用できないらしいが、とりあえず試してみる。>set JAVA_HOME=C:\_java\jdk\1.7.0-ea-28_aug_2008 >set PATH=%PATH%;%JAVA_HOME%\bin >java -version java version "1.7.0-ea" Java(TM) SE Runtime Environment (build 1.7.0-ea-b34) Java HotSpot(TM) Client VM (build 14.0-b03, mixed mode, sharing) >javac -version javac 1.7.0-ea
>md test
>cd test
>javac Test.java
Test.java:7: 式の開始が不正です。
int value = { => 1 + 2 }.invoke();
^
Test.java:7: 式の開始が不正です。
int value = { => 1 + 2 }.invoke();
^
Test.java:7: ';' がありません。
int value = { => 1 + 2 }.invoke();
^
エラー 3 個
>javac -source 7 -target 7 Test.java
Test.java:7: 式の開始が不正です。
int value = { => 1 + 2 }.invoke();
^
Test.java:7: 式の開始が不正です。
int value = { => 1 + 2 }.invoke();
^
Test.java:7: ';' がありません。
int value = { => 1 + 2 }.invoke();
^
エラー 3 個
やはり無理だった。で、closure ライブラリを導入してみる。
先程と同じテストコードを実行してみる。>xcopy /S \closures-2008-08-11\* \_java\jdk\1.7.0-ea-28_aug_2008 \closures-2008-08-11\CHANGES \closures-2008-08-11\DISTRIBUTION C:\_java\jdk\1.7.0-ea-28_aug_2008\LICENSE を上書きしますか (Yes/No/All)? y \closures-2008-08-11\LICENSE \closures-2008-08-11\bin\java \closures-2008-08-11\bin\java.bat \closures-2008-08-11\bin\javac \closures-2008-08-11\bin\javac.bat \closures-2008-08-11\bin\javadoc \closures-2008-08-11\bin\javadoc.bat \closures-2008-08-11\lib\closures.jar 10 個のファイルをコピーしました
動いた。今回はここまで。>javac.bat Test.java >java.bat Test 3 >
使用したテストコード
上で使用したテストコード(Test.java)はこれ。
public class Test{
public Test(){
super();
}
public static void main(final String[] args){
int value = { => 1 + 2 }.invoke();
System.out.println(value);
}
}



