2019/04/04

Java SE 12: Not Mentioned in JEP

このエントリーをはてなブックマークに追加

Java SE 12 was released in 19th March!

So, I'll show updates in Java SE 12. But, I don't explain about the new features mentioned in JEPs.

In general, the updates in Java SE 12 are fewer than other Java SE releases. In the point of syntax, switch statement changes to switch expression (JEP 325). It's big change! But, the API updates are not big.

 

Removed APIs

There are 5 removed methods in Java SE 12.

All are the methods that forRemoval of @Deprecated set true in Java SE 10.

  • java.io.FileInputStream.finalize()
  • java.io.FileOutputStream.finalize()
  • java.util.zip.Deflater.finalize()
  • java.util.zip.Inflater.finalize()
  • java.util.zip.ZipFile.finalize()

Because these methods are only finalize methods, so you feel no difficulty for coding. If you have difficulty, the design may be wrong.

 

In addition, 4 methods were removed.

All of them are override getCause methods, and these classes use getCause method of super class after removing.

  • java.lang.FileNotFoundException.getCause()
  • java.lang.ExceptionInInitializerError.getCause()
  • java.lang.reflext.UndeclaredThrowableException.getCause()
  • java.security.PrivilegedActionException.getCause()

 

APIs proposed for Removal

There are no APIs that are added for removal in Java SE 12.

 

Added APIs

java.lang.constant package

A package was added to java.base module!

But, most of developer seem never to use the package. Because, the pacakge was introduced for "Condy." Cody is for dynamic constant loading as an equivalent to Indy for dynamic method invocation.

APIs included in new package are based on JEP 334: JVM Constants API.

In addition, some interfaces/class and methods are introduced in java.lang.invoke package.

Added interfaces/class are shown below:

  • TypeDescriptor interface
  • TypeDescriptor.OfField interface
  • TypeDescriptor.OfMethod interface
  • VarHandle.VarHandleDesc class

There are 3 methods added:

  • Optional<MethodHandleDesc> MethodHandle.describeConstable()
  • String MethodType.describeString()
  • Optional<MethodTypeDesc> describeConstable()

Moreover, there are 4 methods added in java.lang.Class class:

  • Class<?> arrayType()
  • Class<?> componentType()
  • Optional<ClassDesc> describeConstable()
  • String descriptorString()

 

Besides these, Enum class implements Constable interface, and Enum.EnumDesc inner class is added. Integer/Long/Float/Double/String classes are updated to implement ConstantDesc insterface. These classes are added describeConstant method and resolveConstantDesc method.

However, most of developers seems not to use these new condy APIs for usual software development.

 

java.io package

InputStream class

InputStream class has skip method, and a related method is added:

  • void skipNBytes(long n)

skip method javadoc said "The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes." For this reason, skip methods return actual number of skipping over.

Comparatively, skipNBytes method skip over and discards exactly n bytes. Because of exactly skipping over, type of skipNBytes return value is void.

 

java.lang package

I listed condy related updates of java.lang package, so I'll show other updates.

 

Character.UnicodeBloch/UnicodeScript class

Java SE 12 starts to support Unicode 11.0, but this update is not described in JEP. Java SE 11 supports Unicode 10.0.

For this update, some Unicode blocks and scripts are added. For example, CHESS_SYMBOLS that means chess pawns is added.

 

String class

As previously described, String class added two condy related methods. In addition, a method is added:

  • String indent(int n)

indent method inserts specified number of white spaces into head of line, and inserts new line (\n) into end of string.

jshell> var text = "Hello\nWorld!"
text ==> "Hello\nWorld!"

jshell> System.out.println(text)
Hello
World!

jshell> text = text.indent(4)
text ==> "    Hello\n    World!\n"

jshell> System.out.println(text)
    Hello
    World!


jshell>

If argument is minus value, indent method removes specified number of white spaces. In this case, indent method only remove white spaces.

jshell> text = text.indent(-2)
text ==> "  Hello\n  World!\n"

jshell> text = text.indent(-3)
text ==> "Hello\nWorld!\n"

jshell>

Originally, Java SE 12 planed to introduce JEP 326 Raw String Literals, but gave it up.

JEP 326 includes some String class methods definition. However, these methods were also abandoned.

Only indent method was added to String class. I don't know why.

 

java.net package

SecureCacheResponse class

A method to get SSL/TLS session is added to SevureCacheResponse class.

  • Optional<SSLSession> getSSLSession()

 

ServerSocket class

A constructe is added to ServerSocket class!

  • protected ServerSocket(SocketImpl impl)

However, this constructer is proteced method, so we don't use it in normal software development.

 

java.nio.file package

Files class

A method for finding mismatch points of two fiels is introduced:

  • static long mismatch(Path path, Path path2)

 

mismatch method finds the first difference between path and path2, and returns the position of the first difference of two.

If two files are same, mismatch method returns -1.

C:\demo>cat hello1.txt
Hello, World!

C:\demo>cat hello2.txt
Hello, Java!

C:\demo>cat hello3.txt
Hello, World!

C:\demo>jshell
|  Welcome to JShell -- Version 12
|  For an introduction type: /help intro

jshell> import java.nio.file.*

jshell> var hello1 = Paths.get("hello1.txt")
hello1 ==> hello1.txt

jshell> var hello2 = Paths.get("hello2.txt")
hello2 ==> hello2.txt

jshell> var hello3 = Paths.get("hello3.txt")
hello3 ==> hello3.txt

jshell> Files.mismatch(hello1, hello2)
$5 ==> 7

jshell> Files.mismatch(hello1, hello3)
$6 ==> -1

jshel>

 

java.text package

CompactNumberFormat class

We occasionally want to show numbers using K or M like 10K (10,000). But, Java didn't have such number formatter API before.

Then, Java SE 12 introduced CompactNumberFormat class.

CompaceNumberFormat is a subclass of NumberFormat class, so usage is same. There is a constructor, but it is convenient for us to use factory methods of NumberFormat class.

jshell> import java.text.*

jshell> var formatter = NumberFormat.getCompactNumberInstance()
formatter ==> java.text.CompactNumberFormat@73cf7357

jshell> formatter.format(10000)
$3 ==> "10K"

jshell> formatter.format(12345)
$4 ==> "12K"

jshell> formatter.format(15678)
$5 ==> "16K"

jshell>

The method to get CompactNumberFormat object is NumberFormat.getCompactNumberInstance method. If you use it without argument, you get CompactNumberFormat object of default locale.

In above example, the default locale is English, so the result of formatting 10,000 was 10K.

It's to be noted that default rounding is RoundingMode.HALF_UP. So, CompactNumberInstance method returns 16K when formatting 15,678.

If you'd like to use RoundingMode.FLOOR, you should use setRoundingMode method.

When formatting the number more than 1,000,000, CompactNumberFormat object uses M.

jshell> formatter.format(12_345_678)
$3 ==> "12M"

jshell>

It's also to be noted that the return type of getCompactNumberInstance method is not CompactNumberFormat class but NumberFormat class.

We, Japanese count number by 104not 103. Number unit of 104 is "万" and 108 is "億".

If you'd like to use "万" or "億", you chage locale.

jshell> var formatter = NumberFormat.getCompactNumberInstance(new Locale("jp"), NumberFormat.Style.SHORT)
formatter ==> java.text.CompactNumberFormat@952071d5

jshell> formatter.format(100_000)
$5 ==> "10万"

jshell> formatter.format(123_456)
$6 ==> "12万"

jshell>

Second argument type of getCompactNumberInstance method is Number.Style enum. It is introduced in Java SE 12.

There are two constants defined in Number.Style: LONG and SHORT. No argument getCompactNumberInstance method uses SHORT by default.

When locale is jp (Japanese), 100,000 is formatted to "10万" and 123,456 is formatted to "12万".

 

In case of LONG;

jshell> var formatter = NumberFormat.getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG)
formatter ==> java.text.CompactNumberFormat@952071d5

jshell> formatter.format(10000)
$7 ==> "10 thousand"

jshell> formatter.format(12345)
$8 ==> "12 thousand"

jshell>

"10 thousand" is compact, isn't it 🤔.

 

NumberFormat class

As previously explained, two static factory methods are introduced:

  • static NumberFormat getCompactNumberInstance()
  • static NumberFormat getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle)

java.text.spi.NumberFormatProvider were also added getCompactNumberInstance method.

 

NumberFormat.Field class

Two constants are added:

  • static final NumberFormat.Field PREFIX
  • static final NumberFormat.Field SUFFIX

 

NumberFormat.Style enum

Style enum is also explained previously. It is for constructing CompactNumberFormat object.

There are two constants: LONG and SHORT.

 

java.util.concurrent package

CompletionStage class

5 similar methods to exceptionally method are introduced.

  • default CompletionStage<T> exceptionallyAsync(Function<Throwable, ? extends T> fn)
  • default CompletionStage<T> exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor executor)
  • default CompletionStage<T> exceptionallyCompose (Function<Throwable, ? extends CompletionStage<T>> fn)
  • default CompletionStage<T> exceptionallyComposeAsync(Function<Throwable, ? extends CompletionStage<T>> fn)
  • default CompletionStage<T> exceptionallyComposeAsync(Function<Throwable, ? extends CompletionStage<T>> fn, Executor executor)

exceptionally method is called when an exception is thrown in the previsou stage. If no exception in the previous stage, exceptionally method are skipped, and then next stage are executed.

exceptionallyAsync methods are called asynchronously as can be seen by method name.

jshell> import java.util.concurrent.*;
  
jshell> CompletableFuture.runAsync(() -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     throw new RuntimeException();
   ...> }).
   ...> exceptionally(ex -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     return null;
   ...> }).get();
Thread[ForkJoinPool.commonPool-worker-3,5,main]
Thread[main,5,main]
$2 ==> null

jshell> CompletableFuture.runAsync(() -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     throw new RuntimeException();
   ...> }).
   ...> exceptionallyAsync(ex -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     return null;
   ...> }).get();
Thread[ForkJoinPool.commonPool-worker-3,5,main]
Thread[ForkJoinPool.commonPool-worker-3,5,main]
$3 ==> null

jshell>

Executing by JShell, you can see exceptionally method is executed in main thread, but exceptionallyAsync method is executed in worker thread managed by ForkJoinPool.

 

The Argument of exceptionally method is a lambda expression that gets Throwable object as argument and return something value. On the other hand, the return value tyep of exceptionallyCompose method is CompletionStage class.

thenCompose method returns also CompletionStage object, so exceptionallyCompose is similar to thenCompose. As you may know, exceptionallyComposeAsync method is asynchronous version of exceptionallyCompose.

 

java.util.stream package

Collectors class

A method that execute two terminal operations and gathers these return values is introduced:

  • static <T,R1,R2,R> Collector<T,?,R> teeing(Collector<? super T,?,R1> downstream1, Collector<? super T,?,R2> downstream2, BiFunction<? super R1,? super R2,R> merger)

First and second argument is objects implicated Collect interface (usually we use static methods of Collectors class.) Third argument is a lambda expression for combining first and second Collect objects return values.

The following shows string concatination and couting number of characters, and then combining these two operations return values.

jshell> var text = List.of("a", "b", "c", "d", "e")
text ==> [a, b, c, d, e]

jshell> text.stream().
   ...> collect(Collectors.teeing(Collectors.joining(), 
   ...> Collectors.counting(), 
   ...> (x, y) -> x + " : " + y))
$1 ==> "abcde : 5"

jshell>

It seems very useful!

 

javax.lang.model package

SouceVersion class

Every version up, SouceVersion add a constant:

  • static final SourceVersion RELEASE_12

On the other hand, SourceVersion class is now implementing Constable interface.

 

Others

javax.naming.ldap.spi package is introduced for LDAP service provider. The package includes two class: LdapDnsProvider and LdapDnsProviderResult.

javax.net.ssl.HttpsURLConnection class is added getSSLSession method.

 

Swing is also added a method.

getChooserShortcutPanelFiles method is introduced in javax.swing.filechooser.FileSystemView class. But, usually we don't use the method. It is for internal use.

 

That's all.

2019/03/16

JEPでは語れないJava SE 12

このエントリーをはてなブックマークに追加

アメリカ西海岸時間の3月19日にJava SE 12がリリースされます。

恒例となっているJEPで提案されている以外のJava SEの変更をまとめておきましょう。

全般的にいえるのが、Java SE 12の変更がかなり小規模だということ。言語仕様的にはswitchが文から式になったことが大きいですが、APIの変更はほんとにちょっとしかありません。

今回もABC順にならんでいます。同じように、セキュリティ系のAPIはちゃんと理解していないので、省略してます。

 

廃止になったAPI

Java SE 12では5つのメソッドが廃止になっています。

いずれも、Java SE 10で@DeprecatedのforRemovalがtrueになったメソッドです。つまり、forRemovalがtrueになってから1年で廃止ということです。

  • java.io.FileInputStream.finalize()
  • java.io.FileOutputStream.finalize()
  • java.util.zip.Deflater.finalize()
  • java.util.zip.Inflater.finalize()
  • java.util.zip.ZipFile.finalize()

finalizeメソッドですから、なくても何ら問題はないはず。というか、逆にfinalizeメソッドを使っていたとしたら、その設計の方が問題です。

 

これら以外に、4つのクラスの4メソッドが廃止されました。

いずれもスーパークラスをオーバーライドしていたメソッドで、スーパークラスのメソッドを使うようになったというだけです。

  • java.lang.FileNotFoundException.getCause()
  • java.lang.ExceptionInInitializerError.getCause()
  • java.lang.reflext.UndeclaredThrowableException.getCause()
  • java.security.PrivilegedActionException.getCause()

 

廃止予定のAPI

Java SE 12での廃止予定APIの追加はありませんでした。

 

追加されたAPI

java.lang.constantパッケージ

なんと、java.baseモジュールにパッケージが追加されました!

とはいうものの、普通の開発者がjava.lang.constantパッケージを使うことはまずないと思います。というのも、Java SE 11で導入されたindyの定数版であるcondyで使われるためのクラス群だからです。

このパッケージはJEP 334: JVM Constants APIに基づくパッケージです。

また、このパッケージの追加にともない、java.lang.invokeパッケージのクラス群にもクラスの追加やメソッドの追加があります。

追加されたクラス群は

  • TypeDescriptorインタフェース
  • TypeDescriptor.OfFieldインタフェース
  • TypeDescriptor.OfMethodインタフェース
  • VarHandle.VarHandleDescクラス

の4つです。

メソッドの追加は3つ。

  • Optional<MethodHandleDesc> MethodHandle.describeConstable()
  • String MethodType.describeString()
  • Optional<MethodTypeDesc> describeConstable()
さらに、java.lang.Classクラスにも以下のメソッドが追加されています。
  • Class<?> arrayType()
  • Class<?> componentType()
  • Optional<ClassDesc> describeConstable()
  • String descriptorString()

他にも、EnumクラスがConstableインタフェースを実装するようになり、サブクラスとしてEnum.EnumDescクラスが追加されています。

また、Integerクラス、Longクラス、Floatクラス、Doubleクラス、StringクラスがConstableインタフェースとConstatntDescインタフェースを実装しています。

これらのクラスはdescribeConstantメソッドとresolveConstantDescメソッドが追加されています。

いずれも、普通の開発では使わないと思います。

java.ioパッケージ

InputStreamクラス

InputStreamクラスにはskipメソッドがありましたが、それに関連したメソッドが追加されました。

  • void skipNBytes(long n)

skipメソッドは "さまざまな理由から、skipメソッドは指定よりも少ないバイト数しかスキップしないことがあります" とJavadocに書いてあります。このため、返り値として実際にスキップしたバイト数が返ります。

これに対し、skipNBytesメソッドは必ず指定したバイト数をスキップします。このため、返り値はありません。

 

java.langパッケージ

java.lang.Classクラスのメソッド追加に関しては上述したので、それ以外の追加分です。

Character.UnicodeBloch/UnicodeScriptクラス

JEPにはなっていないのですが、Java SE 12はUnicode 11.0をサポートしています。Java SE 11はUnicode 10.0でした。

この変更に伴って、ブロックとスクリプトの定数が追加されています。たとえば、チェスの駒の絵文字を表すブロックとしてCHESS_SYMBOLSなどがあります。

 

Stringクラス

Stringクラスは前述したようにcondy関連のメソッドが2つ追加されていますが、それ以外にもう1つメソッドが追加されました。

  • String indent(int n)

indentメソッドは指定した数だけ行頭に空白を埋め込む処理を行います。さらに、文字列の最後に\nが挿入されます。

jshell> var text = "Hello\nWorld!"
text ==> "Hello\nWorld!"

jshell> System.out.println(text);
Hello
World!

jshell> text = text.indent(4);
text ==> "    Hello\n    World!\n"

jshell> System.out.println(text)
    Hello
    World!


jshell>

なお、負の数を指定すると、行頭のホワイトスペースが指定した数だけ削除されます。削除されるのはホワイトスペースだけです。

jshell> text = text.indent(-2)
text ==> "  Hello\n  World!\n"

jshell> text = text.indent(-3)
text ==> "Hello\nWorld!\n"

jshell>

もともとJava SE 12はJEP 326 Raw String Literalsが導入される予定だったのですが、結局スリップしてしまいました。

このJEP 326にともなって、Stringクラスにはalignメソッドなどのメソッドが追加予定だったのです。しかし、JEP 326がスリップしてしまったことにより、これらのメソッドも追加されないことになっていました。

ところが、indentメソッドだけは生き残ったというわけです。

 

java.netパッケージ

SecureCacheResponseクラス

SecureCacheResponseクラスにはSSLセッションを取得するメソッドが追加されました。

  • Optional<SSLSession> getSSLSession()

 

ServerSocketクラス

ちょっと珍しいのですが、コンストラクタが追加されました。

  • protected ServerSocket(SocketImpl impl)

追加されたといっても、protectedメソッドなので普通は使わないでしょう。

 

java.nio.fileパッケージ

Filesクラス

2つのファイルの相違点を見つけるメソッドが追加されました。

  • static long mismatch(Path path, Path path2)

引数のpathとpath2のファイルの相違点を見つけて、はじめの相違点までのバイト数を返します。

相違点がない場合、-1が返ります。

C:\demo>cat hello1.txt
Hello, World!

C:\demo>cat hello2.txt
Hello, Java!

C:\demo>cat hello3.txt
Hello, World!

C:\demo>jshell
|  JShellへようこそ -- バージョン12
|  概要については、次を入力してください: /help intro

jshell> import java.nio.file.*

jshell> var hello1 = Paths.get("hello1.txt")
hello1 ==> hello1.txt

jshell> var hello2 = Paths.get("hello2.txt")
hello2 ==> hello2.txt

jshell> var hello3 = Paths.get("hello3.txt")
hello3 ==> hello3.txt

jshell> Files.mismatch(hello1, hello2)
$5 ==> 7

jshell> Files.mismatch(hello1, hello3)
$6 ==> -1

jshel>

 

java.textパッケージ

CompactNumberFormatクラス

数字の10,000を10Kとか1万とか記述することがありますね。ところが、今までJavaでは数字をこのようにフォーマットする標準のAPIがありませんでした。

そこで、Java SE 12で導入されたのがCompactNumberFormatクラスです。

CompactNumberFormatクラスはNumberFormatクラスのサブクラスなので、使い方は一緒です。コンストラクタも用意されていますが、手っ取り早いのはNumberFrmatクラスのファクトリメソッドを使用する方法です。

jshell> import java.text.*

jshell> var formatter = NumberFormat.getCompactNumberInstance()
formatter ==> java.text.CompactNumberFormat@73cf7357

jshell> formatter.format(10000)
$3 ==> "1万"

jshell> formatter.format(12345)
$4 ==> "1万"

jshell> formatter.format(15678)
$5 ==> "2万"

jshell>

CompactNumberFormatオブジェクトを取得するためのメソッドがgetCompactNumberInstanceメソッドです。引数なしでコールすると、デフォルトロケールのCompactNumberFormatオブジェクトを返します。

そのため、10,000をフォーマットすると"1万"となるわけです。

それにしても、万の単位より小さいところが四捨五入といいうのは...

四捨五入にするか切り捨てにするかは、NumberFormatクラスのsetRoundingModeメソッドで変更できますが、デフォルトが四捨五入というのはどうなんですかね。

万の次は億なので、100,000,000より大きい数になると億より小さいところも四捨五入です。1億2345万とかにはならないです。

setMaximumFractionDigitsメソッドで四捨五入された部分を小数点で出せますけど、あまり意味ないかも。

ついでに、気をつけなくてはいけないのが、getCompactNumberInstanceメソッドの返り値の型がCompactNumberFormatクラスではなく、NumberFormatクラスだということです。

 

"1万"ではなくて"10K"と表示したい場合は、ロケールを指定します。

jshell> var formatter = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT)
formatter ==> java.text.CompactNumberFormat@952071d5

jshell> formatter.format(10000)
$6 ==> "10K"

jshell> formatter.format(12345)
$7 ==> "12K"

jshell>

getCompactNumberInstanceメソッドの第2引数は、新しく追加された列挙型のNumber.Style列挙型です。

Number.Style列挙型の定数としてLONGとSHORTがあります。もちろん、CompactNumberFormatオブジェクトを取得するにはSHORTを指定します。

ロケールがen_USだと、10,000が"10K"とフォーマットされます。こちらもKより小さい桁は切り捨てです。

ところで、SHORTでなくて、LONGにしてみたらどうなるでしょう。

jshell> var formatter = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.LONG)
formatter ==> java.text.CompactNumberFormat@952071d5

jshell> formatter.format(10000)
$7 ==> "10 thousand"

jshell> formatter.format(12345)
$8 ==> "12 thousand"

jshell>

なんとLONGだとthousandですよw

ちなみに、ロケールをja_JPにして、LONGにしても表示は変わりません。

 

NumberFormatクラス

前述したように、CompactNumberFormatオブジェクトを取得するためのstaticメソッドが追加されました。

  • static NumberFormat getCompactNumberInstance()
  • static NumberFormat getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle)

これに伴い、java.text.spi.NumberFormatProviderクラスにもgetCompactNumberInstanceメソッドが追加されています。

 

NumberFormat.Fieldクラス

プレフィックスとサフィックスを表す定数が追加されています。

  • static final NumberFormat.Field PREFIX
  • static final NumberFormat.Field SUFFIX

 

NumberFormat.Style列挙型

こちらも前述したようにCompactNumberFormatクラスの生成時に指定するための列挙型です。

LONGとSHORTの定数が定義されています。

 

java.util.concurrentパッケージ

CompletionStageクラス

exceptionallyメソッドの亜種が5種類増えました。

  • default CompletionStage<T> exceptionallyAsync(Function<Throwable, ? extends T> fn)
  • default CompletionStage<T> exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor executor)
  • default CompletionStage<T> exceptionallyCompose (Function<Throwable, ? extends CompletionStage<T>> fn)
  • default CompletionStage<T> exceptionallyComposeAsync(Function<Throwable, ? extends CompletionStage<T>> fn)
  • default CompletionStage<T> exceptionallyComposeAsync(Function<Throwable, ? extends CompletionStage<T>> fn, Executor executor)

exceptionallyメソッドは前段のステージで例外がスローされた時にコールされるメソッドです。前段で例外がスローされていなければ、exceptionallyメソッドはスキップされて、次段のステージが実行されます。

exceptionallyAsyncメソッドは、その名の通りexceptionallyメソッドと同様の処理を非同期に行うメソッドです。

jshell> import java.util.concurrent.*;
  
jshell> CompletableFuture.runAsync(() -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     throw new RuntimeException();
   ...> }).
   ...> exceptionally(ex -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     return null;
   ...> }).get();
Thread[ForkJoinPool.commonPool-worker-3,5,main]
Thread[main,5,main]
$2 ==> null

jshell> CompletableFuture.runAsync(() -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     throw new RuntimeException();
   ...> }).
   ...> exceptionallyAsync(ex -> {
   ...>     System.out.println(Thread.currentThread());
   ...>     return null;
   ...> }).get();
Thread[ForkJoinPool.commonPool-worker-3,5,main]
Thread[ForkJoinPool.commonPool-worker-3,5,main]
$3 ==> null

jshell>

JShellで実行してみるとexceptionallyメソッドはメインスレッドで実行されていますが、exceptionallyAsyncメソッドはForkJoinPoolが管理しているワーカースレッドで実行されていることが分かります。

exceptionallyメソッドの引数はThrowableオブジェクトを受け取って、何らかの値を返すラムダ式です。これに対してexceptionallyComposeメソッドの場合、返り値の型がCompletionStageクラスになっています。

同じようなメソッドにthenComposeメソッドがありますが、それの例外版だと考えればいいと思います。

で、exceptionallyComposeAsyncメソッドはそれの非同期版です。

 

java.util.streamパッケージ

Collectorsクラス

2種類の終端処理を行って、それをまとめるためのメソッドが追加されました。

  • static <T,R1,R2,R> Collector<T,?,R> teeing(Collector<? super T,?,R1> downstream1, Collector<? super T,?,R2> downstream2, BiFunction<? super R1,? super R2,R> merger)

第1引数と第2引数がそれぞれCollectインタフェースのオブジェクト(実際にはCollectorsのstaticメソッドを使用します)、第3引数が2つの終端処理の結果を引数にとるBiFunctionインタフェースのラムダ式です。

たとえば、文字列を連結する処理と、要素数を数える処理を行い、最後に結果をコロンでつなげた文字列にするには、次のように記述します。

jshell> var text = List.of("a", "b", "c", "d", "e")
text ==> [a, b, c, d, e]

jshell> text.stream().
   ...> collect(Collectors.teeing(Collectors.joining(), 
   ...> Collectors.counting(), 
   ...> (x, y) -> x + " : " + y))
$1 ==> "abcde : 5"

jshell>

これはけっこう便利かもしれません。

 

javax.lang.modelパッケージ

SouceVersionクラス

バージョンアップのたびに定数が増えるのはお約束。

  • static final SourceVersion RELEASE_12

また、このクラスもcondy対応でConstableインタフェースを実装するように変更されています。

 

その他

LDAPのサービスプロバイダー用のパッケージjavax.naming.ldap.spiが追加されました。このパッケージにはLdapDnsProviderクラスと、LdapDnsProviderResultクラスが含まれています。

また、javax.net.sslパッケージのHttpsURLConnectionクラスに、getSSLSessionメソッドが追加されています。

最後にSwing。

ファイルチューザー用のjavax.swing.filechooser.FileSystemViewクラスにgetChooserShortcutPanelFilesメソッドが追加されました。とはいうものの、このクラスを直接使うことはまずないはずです。

 

ということで、標準で提供されているモジュールをすべて合わせても、変更は少ないですね。

 

2018/10/13

Java SE 11 not mentioned in JEP

このエントリーをはてなブックマークに追加

Java SE 11 was release on 25th Sep.

Features of Java SE are proposed in JEP (JDK enhancement proposal), and Java SE 11 includes 17 JEPs.

However, there are many small updates not mentioned in JEP. So, I summarized these small updates, especially java.base module.

I excepted security features, because I don't have enough knowledge about security.

 

Removed API

Java is the language that give weight to compatibility, however Java compatibility policy has been changed since Java SE 9.

In fact, there are many APIs removed in Java SE 11.

Module

The biggest incompatibility of Java SE 11 is to remove modules about Java EE. This was proposed in JEP 320.

These modules were deprecated in Java SE 9, but we can still use them in Java SE 9 and 10.

However, the modules were removed finally in Java SE 11!

The removed modules are following:

  • java.se.ee
  • java.activation (JAF)
  • java.corba
  • java.transaction (JTA)
  • java.xml.bind (JAXB)
  • java.xml.ws (JAX-WS)
  • java.xml.ws.annotation (JAX-WS)

You can use JAF, JTA, JAXB and JAX-WS of Java EE (Jakarta EE). These modules are also registered in Maven central reposity. I wrote the article how to use JAXB and JAF in Maven project.

No alternative about CORBA, but I think no one uses CORBA any more.

In addition, JavaFX modules in Oracle JDK were removed (OpenJDK doesn't include JavaFX modules.)

  • javafx.base
  • javafx.graphics
  • javafx.controls
  • javafx.fxml
  • javafx.media
  • javafx.web
  • javafx.swing

JavaFX is developed by OpenJFX project in OpenJDK. JavaFX 11 is also released before Java SE 11.

New JavaFX Site was opened at the same time as JavaFX 11 release. You can download JavaFX 11 and refere Javadoc of JavaFX 11 at the site.

Class

There were many changes about security API. Following class was removed:

  • java.security.Policy

Method

forRemovel atribute of @Deprecated annotation was introduced in Java SE 9. Type of forRemoval is boolean, and true means that target API will be removed.

In Java SE 11, following methods that forRemoval value were true were removed:

  • java.lang.Runtime.runFinalizersOnExit(boolean)
  • java.lang.SecurityManager.checkAwtEventQueueAccess()
  • java.lang.SecurityManager.checkMemberAccess(java.lang.Class, int)
  • java.lang.SecurityManager.checkSystemClipboardAccess()
  • java.lang.SecurityManager.checkTopLevelWindow(java.lang.Object)
  • java.lang.System.runFinalizersOnExit(boolean)
  • java.lang.Thread.destroy()
  • java.lang.Thread.stop(java.lang.Throwable)

All of removed methods were that forRemoval value were true in Java SE 9.

APIs proposed for Removal

Follwoing APIs were proposed for removal in Java SE 11.

Class

  • java.util.jar.Pack200

Interface

  • java.util.jar.Pack200.Packer
  • java.util.jar.Pack200.Unpacker

Pack200 class was proposed by JEP 336.

All of deprecated API are listed in Deprecated in Javadoc.

 

Added APIs

java.io package

ByteArrayOutputStream class

  • void writeBytes(byte[] b)

This method is equivalent to write(b, 0, b.lenght).

FileReader class

Two constructors are introduced.

  • FileReader(java.io.File file, java.nio.charset.Charset charset)
  • FileReader(java.lang.String fileName, java.nio.charset.Charset charset)

Finally, we can specify charset to create FileReader object!

FileWriter class

Four constructers with charset are also introduced as FileReader.

  • FileWriter(java.io.File file, java.nio.charset.Charset charset)
  • FileWriter(java.io.File file, java.nio.charset.Charset charset, boolean append)
  • FileWriter(java.lang.String fileName, java.nio.charset.Charset charset)
  • FileWriter(java.lang.String fileName, java.nio.charset.Charset charset, boolean append)

InputStream class

  • static InputStream nullInputStream()
  • byte[] readNBytes(int len)

nullInputStream method is a factory method to create InputStream object that reads no bytes.

readNBytes method was introduced in Java SE 9. In Java SE 11, overload method is also introduced.

You need to specify byte array for readNBytes in Java SE 9, but no need byte array argument in Java SE 11. You only specigy lenght of read bytes.

However, I think the implementation is not so efficient at the present. Therefore, if reading data repeatedly in a loop, you should use a buffer byte array instead of readNBytes in Java SE 11. If reading all data at once, readAllbytes method is suitable.

OutputStream class

  • static OutputStream nullOutputStream()

nullOutputStream method is a factory method to create OutputStream object that writes no bytes as InputStream.

Reader class

  • static Reader nullReader()

Writer class

  • static Writer nullWriter()

java.lang package

CharSequence interface

  • static int compare(java.lang.CharSequence cs1, java.lang.CharSequence cs2)

compare method is a static method to compare two arguments.

I think it is a simple delegation method, but the implementation of compare methods compares two arguments character one by one.

Character class

  • static String toString(int codePoint)

toString method to create String object from char argument is existed, but no int argument until Java SE 11. It means we can use a codepoint of Unicode to create String object.

Character.UnicodeBlock class

Java SE 11 enable to use Unicode 10. With this update, 18 constans of Unicode block are added.

Character.UnicodeScript enum

With introducing Unicode 10, 10 script name of Unicode are added.

Class class

  • Class<?> getNestHost()

getNestHost method returns host class object of nested class. When class is non-nest, array, or primitive, it returns self class object.

  • Class<?>[] getNestMembers()

getNestMembers returns nested members.

  • boolean isNestmateOf(Class<?> c)

Comparing to the host class of argument Class object. When argument is array or primitive type, it returns false.

These three methods were porposed by JEP 181 Nest-Based Access Control.

String class

  • boolean isBlank()

isBlank return true when string is empty or only white space.

  • Stream<String> lines()

lines method divides string by line break, and makes Stream object.

  • String repeat(int count)

repeat method makes a concatenated string repeatedly by count argument value. For example, "a".repeat(3) returs "aaa".

  • String strip()
  • String stripLeading()
  • String stripTailing()

These three methods remove all white spaces of leading and trailing.

strip method removes leading and trailing white spaces, stripLeading method removes only leading, and stripTailing method removes only tailiing.

strip and stripLeading methods are similar to trim method. But, trim method removes only Latin-1 white space.

To remove non-Latin-1 white space such as U+3000, you should use strip/stripLeading/stripTailing methods.

StringBuffer class

StringBuilder class

These two classes now implement Comparable interface. Therefore, we can use compareTo method of these classes.

java.lang.invoke package

A class was added to java.lang.invoke package.

  • ConstantBootstraps class

boostrap of this class name means bootstrap method for InvokeDynamic bytecode. InvokeDynamic define how to process dynamically, and bootstrap is used for this purpose. InvokeDynamic is used for dynamic type JVM language such as JRuby and Lambda expression.

Java SE 11 introduced new bytecode, that is CONSTANT_Dynamic. CONSTANT_Dynamic is used for constant initialization, on the other hand InvokeDynamic is for method invoking.

CONSTANT_Dynamic also uses bootstrap method for defining constant initialization.

ConstantBootstraps is a utility class for bootstrap of CONSTANT_Dynamic.

By the way, javac of Java SE 11 doesn't use CONSTANT_Dynamic. In the future, javac will make class file including CONSTANT_Dynamic.

java.lang.ref package

Reference class

  • Object clone()

The reason clone method is overridden is to indicate Reference class can't clone. Therefore, the clone method always throws CloneNotSupportedException.

But, I think root cause is that clone method is defined in Object class. Although the root cause is clear, the cause can't be fixed anymore. So, clone method in Reference class is work-around.

java.nio package

ByteBuffer class

  • int mismatch(ByteBuffer that)

CharBuffer class

  • int mismatch(CharBuffer that)

DoubleBuffer class

  • int mismatch(DoubleBuffer that)

FloatBuffer class

  • int mismatch(FloatBuffer that)

IntBuffer class

  • int mismatch(IntBuffer that)

LongBuffer class

  • int mismatch(LongBuffer that)

ShortBuffer class

  • int mismatch(ShortBuffer that)

mismatch method compares this buffer and argument buffer, and returns the relative index that two buffers are diifferent. If the buffers are equal, it returns -1.

The relative means that mismatch method scans from the position to the limit of buffers.

Even if the first absolute mismatch index is before the position, mismatch method doesn't check it.

java.nio.chanels package

SelectionKey class

  • int interestOpsAnd(int ops)
  • int interestOpsOr(int ops)

SelectionKey class is used for non-blocking socket communication, and defines some constans of operations.

These constants are expressed a bit, and can do bit operations such as AND or OR. For example, OP_READ is 0b0001, and OP_WRITE is 0b0100.

We can get/set the present operation value by interestOps method.

Above two methods are combination methods of interestOps and AND or OR operation.

key.interestOpsAnd(ops) is equivalent to key.interestOps(key.interestOps() & ops). At the same way, key.interestOpsOr(ops) is same as key.interestOps(key.interestOps() | ops).

Selector class

Selector class is also used for non-blocking communitation. Now, we can describe the non-blocking process by lambda expression.

  • int select(java.util.function.Consumer<SelectionKey> action)
  • int select(java.util.function.Consumer<SelectionKey> action, long timeout)
  • int selectNow(java.util.function.Consumer<SelectionKey> action)

Until Java SE 11, we get SelectionKey object by selectedKeys method, and handle it according to the operation of SelectionKey object.

Now, we can describe like following:

    selector.select(key -> {
        try {
            if (key.isAcceptable()) {
                // OP_ACCEPT case
                ServerSocketChannel serverSocket = (ServerSocketChannel) key.channel();
                // change to Non-Blocking mode
                serverSocket.configureBlocking(false);
                // register to Selector
                serverSocket.register(selector, SelectionKey.OP_READ);
            } else if (key.isReadable()) {
                // OP_READ case
                SocketChannel socket = (SocketChannel) key.channel();
                // read contents...
            }
        } catch (IOException ex) {
            // exception handling
        }
    });

I don't like exception handling in lambda expression, but description is much easier than boefore.

java.nio.file package

Path interface

static Path of(java.lang.String first, java.lang.String... more)

static Path of(java.net.URI uri)

Two factory methods were added to Path interface.

The former factory method is equivalent to FileSystems.getDefault().getPath(first, more). It means the former factory method deal with phisical file system.

The latter factory method returns Path object of default phisical file system if schema of URI is "file". If not, it checks whether schema of URI is possible to deal with or not. If possible, the factory method calls getPath(uri) method of FileSystemProvider class.

Files class

  • static String readString(java.nio.file.Path path)
  • static String readString(java.nio.file.Path path, java.nio.charset.Charset cs)
  • static Path writeString(java.nio.file.Path path, java.lang.CharSequence csq, java.nio.file.OpenOption... options)
  • static Path writeString(java.nio.file.Path path, java.lang.CharSequence csq, java.nio.charset.Charset cs, java.nio.file.OpenOption... options)

Files class defines readAllLines method to read the contents at one time, but type of return value is List<String>. On the other hand, readString methods returns a String object of file contes including line break code.

In the same way, writeString writes a CharSequence object to path at one time.

java.util package

Collection interface

  • <T> T[] toArray(IntFunction<T[]> generator)

Collection intterface added an overload of toArray method. Argument of new toArray method is a generator function to allocate the returned array.

Optional class

OptionalDouble class

OptionalInt class

OptionalLong class

  • boolean isEmpty()

isEmpty method is reverse of isPresent method.

java.tuil.concurrent package

TimeUnit enum

long convert(java.time.Duration duration)

convert method converts duration to time unit. For example, TimeUnit.MILLISECONDS.convert(Duration.ofMinutes(1L)) returns 60000.

java.util.function package

Predicate interface

  • <T> Predicate<T> not(Predicate<? super T> target)

not method creates Predicate object that indicates negative condition of target.

java.util.regex package

Pattern class

  • Predicate<String> asMatchPredicate()

asPredicate method was introduced in Java SE 8. The asPredicate method creates Predicate object to use for an argument of fiter method, mid-operation of Stream pipeline.

However, Predicate object created by asPredicate is equivalent to s -> s.matcher().find(). It means partial check.

asMatchPredicate method also creates Predicate object, but the object checks whole string. It is equivalent to s -> s.matcher().matches().

    Pattern pattern = Pattern.compile("bc");
    List<String> list = List.of("abc", "bc", "bcd");

    list.stream()
        .filter(pattern.asPredicate())
        .forEach(System.out::println);

    list.stream()
        .filter(pattern.asMatchPredicate())
        .forEach(System.out::println);

The former result of above code is "abc", "bc", and "bcd", but the latter result is only "bc".

java.util.zip package

Deflater class

  • int deflate(java.nio.ByteBuffer output)
  • int deflate(java.nio.ByteBuffer output, int flush)
  • void setDictionary(java.nio.ByteBuffer dictionary)
  • void setInput(java.nio.ByteBuffer input)

Deflater class is used for ZLIB compress.

New four methods are all overloads. Argument of exsited methods are byte array, however one of new methods are ByteBuffer.

Inflater class

  • int inflate(java.nio.ByteBuffer output)
  • void setDictionary(java.nio.ByteBuffer dictionary)
  • void setInput(java.nio.
  • ByteBuffer input)

Inflater class is used for uncompressing ZLIB compressed contents.

As same as Deflater class, new methods enable to use ByteBuffer for argument.