jdk/src/share/classes/java/lang/AutoCloseable.java
author jrose
Tue, 14 Jun 2011 22:47:09 -0700
changeset 9859 47e26ad535c4
parent 8786 50520f3f8ada
child 10347 1c9efe1ec7d3
permissions -rw-r--r--
7052202: JSR 292: Crash in sun.invoke.util.ValueConversions.fillArray Summary: Fix corner cases involving MethodHandles.permuteArguments with long or double argument lists. Reviewed-by: twisti, never
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
/*
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
     2
 * Copyright (c) 2009, 2011, 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
 */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    34
public interface AutoCloseable {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    35
    /**
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
    36
     * 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
    37
     * 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
    38
     * {@code try}-with-resources statement.
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    39
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    40
     * <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
    41
     * 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
    42
     * 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
    43
     * 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
    44
     * 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
    45
     *
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    46
     * <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
    47
     * 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
    48
     * InterruptedException}.</em>
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    49
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    50
     * 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
    51
     * 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
    52
     * InterruptedException} is {@linkplain Throwable#addSuppressed
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    53
     * suppressed}.
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    54
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    55
     * 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
    56
     * 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
    57
     * method should not throw it.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    58
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    59
     * <p>Note that unlike the {@link java.io.Closeable#close close}
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    60
     * method of {@link java.io.Closeable}, this {@code close} method
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    61
     * is <em>not</em> required to be idempotent.  In other words,
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    62
     * calling this {@code close} method more than once may have some
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    63
     * visible side effect, unlike {@code Closeable.close} which is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    64
     * required to have no effect if called more than once.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    65
     *
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    66
     * 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
    67
     * to make their {@code close} methods idempotent.
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
    68
     *
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    69
     * @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
    70
     */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    71
    void close() throws Exception;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    72
}