yohhoyの日記

技術的メモをしていきたい日記

AutoClosable#closeメソッドのべき等要件

プログラミング言語Javaのtry-with-resources構文で用いるAutoClosableインタフェース仕様では、closeメソッドのべき等(idempotent)性は要求されない。(実装時には"べき等"が強く推奨される)

void close()
    throws Exception

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.
(snip)
Note that unlike the close method of Closeable, this close method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike Closeable.close which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.

https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html#close--
void close()
    throws IOException

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

https://docs.oracle.com/javase/8/docs/api/java/io/Closeable.html#close--

関連URL