jdk/src/share/classes/java/lang/AutoCloseable.java
author mduigou
Wed, 21 Aug 2013 12:03:19 -0700
changeset 19572 e43d5c2e79ca
parent 15647 314007859004
child 19798 b627a06c30ed
permissions -rw-r--r--
8023306: Add replace() implementations to TreeMap Reviewed-by: psandoz, alanb, chegar, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     1
/*
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 10347
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     4
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    10
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    15
 * accompanied this code).
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    16
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    20
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    23
 * questions.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    24
 */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    25
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    26
package java.lang;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    27
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    28
/**
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    29
 * A resource that must be closed when it is no longer needed.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    30
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    31
 * @author Josh Bloch
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    32
 * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    33
 */
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 10347
diff changeset
    34
@FunctionalInterface
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    35
public interface AutoCloseable {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    36
    /**
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
    37
     * Closes this resource, relinquishing any underlying resources.
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    38
     * This method is invoked automatically on objects managed by the
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    39
     * {@code try}-with-resources statement.
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    40
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    41
     * <p>While this interface method is declared to throw {@code
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    42
     * Exception}, implementers are <em>strongly</em> encouraged to
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    43
     * declare concrete implementations of the {@code close} method to
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    44
     * throw more specific exceptions, or to throw no exception at all
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    45
     * if the close operation cannot fail.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    46
     *
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    47
     * <p> Cases where the close operation may fail require careful
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    48
     * attention by implementers. It is strongly advised to relinquish
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    49
     * the underlying resources and to internally <em>mark</em> the
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    50
     * resource as closed, prior to throwing the exception. The {@code
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    51
     * close} method is unlikely to be invoked more than once and so
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    52
     * this ensures that the resources are released in a timely manner.
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    53
     * Furthermore it reduces problems that could arise when the resource
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    54
     * wraps, or is wrapped, by another resource.
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    55
     *
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    56
     * <p><em>Implementers of this interface are also strongly advised
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    57
     * to not have the {@code close} method throw {@link
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    58
     * InterruptedException}.</em>
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    59
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    60
     * This exception interacts with a thread's interrupted status,
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    61
     * and runtime misbehavior is likely to occur if an {@code
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    62
     * InterruptedException} is {@linkplain Throwable#addSuppressed
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    63
     * suppressed}.
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    64
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    65
     * More generally, if it would cause problems for an
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    66
     * exception to be suppressed, the {@code AutoCloseable.close}
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    67
     * method should not throw it.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    68
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    69
     * <p>Note that unlike the {@link java.io.Closeable#close close}
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    70
     * method of {@link java.io.Closeable}, this {@code close} method
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    71
     * is <em>not</em> required to be idempotent.  In other words,
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    72
     * calling this {@code close} method more than once may have some
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    73
     * visible side effect, unlike {@code Closeable.close} which is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    74
     * required to have no effect if called more than once.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    75
     *
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    76
     * However, implementers of this interface are strongly encouraged
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    77
     * to make their {@code close} methods idempotent.
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
    78
     *
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    79
     * @throws Exception if this resource cannot be closed
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    80
     */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    81
    void close() throws Exception;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    82
}