/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * An object that may (but need not) hold one or more references to * resources that will be released when closed. Such objects may be * used with try-with-resources or related {@code try...finally} * constructions that ensure they are closed as soon as they are no * longer needed. Interface {@code MayHoldCloseableResource} indicates that * only a minority of usages warrant resource control constructions: * those specialized to known resource-bearing instances, or those * that must operate in complete generality. * *

For example, most usages of the {@link java.util.stream.Stream} * classes operate on data sources such as an array, {@code * Collection}, or generator function that do not require or benefit * from explicit resource control. However, some uses of IO channels * as data sources do. * *

Annotation {@link HoldsResource} may be used to guide users or static * analysis tools to deciding whether resource-control constructions are * warranted when using particular instantiations of * {@code MayHoldCloseableResource}. * * @see AutoCloseable * @see HoldsResource */ public interface MayHoldCloseableResource extends AutoCloseable { /** * Closes this resource, relinquishing any underlying resources. * This method is invoked automatically on objects managed by the * {@code try}-with-resources statement. * * Implementers of this interface are strongly encouraged * to make their {@code close} methods idempotent. * * @see AutoCloseable#close() */ @Override void close(); /** * Indicates that a variable holding a {@code MayHoldCloseableResource} or * a method returning a {@code MayHoldCloseableResource} definitely does * hold a closeable resource. */ @Retention(RetentionPolicy.CLASS) @Documented @interface HoldsResource { } }