Created
March 13, 2013 04:54
-
-
Save bitterfox/5149477 to your computer and use it in GitHub Desktop.
Java8の言語仕様総まとめ
LambdaとTypeAnnotationで変更される言語仕様を網羅しようとしてみました。
これ忘れてるよというのがありましたら教えてください。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.lang.annotation.*; | |
public class Java8 | |
{ | |
@FunctionalInterface | |
private interface F // 関数型インターフェース | |
{ | |
static F concat(F f1, F f2) // staticインターフェースメソッド | |
{ | |
return () -> {f1.invoke(); f2.invoke();}; | |
} | |
void invoke(); | |
default F then(F f) // default実装 | |
{ | |
return () -> {this.invoke(); f.invoke();}; | |
} | |
} | |
@FunctionalInterface | |
private interface Supplier<T> | |
{ | |
T supply(); | |
} | |
@FunctionalInterface | |
private interface Factory<T, R> | |
{ | |
R create(T t); | |
} | |
private interface I1 extends F | |
{ | |
default void invoke() | |
{ | |
System.out.println("I1"); | |
} | |
} | |
private interface I2 extends F | |
{ | |
default void invoke() | |
{ | |
System.out.println("I2"); | |
} | |
} | |
private static class C1 implements I1, I2 | |
{ | |
public void invoke() // ダイアモンド継承になるので実装しなければならない | |
{ | |
I1.super.invoke(); // 親インターフェースのメソッド呼び出し | |
I2.super.invoke(); | |
} | |
} | |
public static <T> T nul() {return null;} | |
public static <T> T through(T t) {return t;} | |
public static void jsr335(/*実質的にfinal*/ int n) | |
{ | |
F f0 = (F & Serializable) () -> System.out.println("Lambda"); // 交差型キャスト & ラムダ式 | |
F f1 = System.out::println; // メソッド参照 | |
Supplier<Java8> java8 = Java8::new; // コンストラクタ参照 | |
Factory<Integer, int[]> intArray = int[]::new; // 配列コンストラクタ参照 | |
F f2 = () -> System.out.println("n = " + n); // 実質的にfinalな変数への参照(ラムダ式) | |
F f3 = new F() | |
{ | |
public void invoke() | |
{ | |
System.out.println("n = " + n); // 実質的にfinalな変数への参照(匿名クラス) | |
} | |
}; | |
String str = through(nul()); // グラフ型推論 | |
} | |
@Target(ElementType.TYPE_USE) | |
private @interface NonNull {} // 型アノテーション | |
@Target(ElementType.TYPE_USE) | |
private @interface Nullable {} | |
@Target(ElementType.TYPE_USE) | |
private @interface Readonly {} | |
private class C2 {} | |
public static void jsr308(@NonNull String @Readonly [] args) // 型アノテーション(NonNullなStringのReadonlyな配列) | |
{ | |
boolean isReadonlyArrayOfNonNullString = | |
args instanceof @NonNull String @Readonly []; | |
if (args.length == 0) | |
{ | |
args = new @NonNull String @Readonly []{""}; // インスタンス生成 | |
} | |
java.lang.@NonNull String arg0 = args[0]; // 完全修飾名の場合 | |
@NonNull Java8.@Nullable C2 c = null; // ネストクラスの場合(NonNullなJava8のNullableなC2) | |
@Nullable Supplier<@NonNull String> supplier = null; // ジェネリクス内での型アノテーション | |
// and more forms | |
class Local | |
{ | |
public @NonNull String toString(@NonNull Local this) // 明示的なメソッドレシーバ | |
{ | |
return ""; | |
} | |
} | |
} | |
public static void main(String[] args) | |
{ | |
jsr335(args.length); // ラムダ | |
jsr308((@NonNull String @Readonly [])args); // 型アノテーション & 型アノテーション付きキャスト | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment