001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.util;
028
029import edu.umd.cs.findbugs.annotations.NonNull;
030import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
031
032/**
033 * Wraps a resource to make it {@link AutoCloseable}.
034 *
035 * @param <T>
036 *          the resource type
037 * @param <E>
038 *          the exception type that may be thrown if an error occurs when
039 *          closing the resource
040 */
041public class AutoCloser<T, E extends Exception> implements AutoCloseable {
042  @NonNull
043  private final T resource;
044  @NonNull
045  private final Closer<T, E> closeLambda;
046
047  /**
048   * Adapt the the provided {@code resource} to be {@link AutoCloseable}, using a
049   * provided closer {@code lambda}.
050   *
051   * @param <T>
052   *          the resource's type
053   * @param <E>
054   *          the exception type that can be thrown when closing
055   * @param resource
056   *          the object to adapt
057   * @param lambda
058   *          the lambda to use as a callback on close
059   * @return the resource wrapped in an {@link AutoCloseable}
060   */
061  @NonNull
062  public static <T, E extends Exception> AutoCloser<T, E> autoClose(
063      @NonNull T resource,
064      @NonNull Closer<T, E> lambda) {
065    return new AutoCloser<>(resource, lambda);
066  }
067
068  /**
069   * Adapt the provided {@code resource} to be {@link AutoCloseable}, using a
070   * provided closer {@code lambda}.
071   *
072   * @param resource
073   *          the object to adapt
074   * @param lambda
075   *          the lambda to use as a callback on close
076   */
077  public AutoCloser(@NonNull T resource, @NonNull Closer<T, E> lambda) {
078    this.resource = resource;
079    this.closeLambda = lambda;
080  }
081
082  /**
083   * Get the wrapped resource.
084   *
085   * @return the resource object
086   */
087  @NonNull
088  public T getResource() {
089    return resource;
090  }
091
092  @Override
093  @SuppressFBWarnings("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION")
094  public void close() throws E {
095    closeLambda.close(getResource());
096  }
097
098  @FunctionalInterface
099  public interface Closer<T, E extends Exception> {
100    /**
101     * This method is called to auto-close the resource.
102     *
103     * @param object
104     *          the resource to auto-close
105     * @throws E
106     *           the exception type that can be thrown when closing
107     */
108    @SuppressFBWarnings("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION")
109    void close(@NonNull T object) throws E;
110  }
111}