All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.logcontext.MultiCloseableLogContext Maven / Gradle / Ivy

There is a newer version: 0.0.3
Show newest version
package io.github.logcontext;

import static java.util.Collections.unmodifiableList;
import static java.util.Objects.isNull;
import static java.util.Objects.requireNonNull;

import java.util.List;

/**
 * The MultiCloseableLogContext class provides a {@link LogContext} that will close a group of
 * {@link AutoCloseable} instances as a single close operation. The {@link AutoCloseable#close()}
 * method willed be called on every non-null instance provided. If any exceptions are thrown, the
 * last exception will be thrown after all contexts in the group have been closed.
 */
public class MultiCloseableLogContext implements LogContext {

  /** The list of auto-closeable contexts that should be closed as a group. */
  private final List closeables;

  /**
   * Constructs a new instance of MultiCloseableLogContext.
   *
   * @param closeables the list of auto-closeable contexts to close as a group (must not be 
   *     null).
   * @throws NullPointerException if closeables is null.
   */
  public MultiCloseableLogContext(final List closeables) {
    this.closeables = unmodifiableList(requireNonNull(closeables, "closeables must not be null"));
  }

  @Override
  public void close() throws Exception {
    Exception caught = null;
    for (AutoCloseable closeable : closeables) {
      try {
        if (!isNull(closeable)) {
          closeable.close();
        }
      } catch (final Exception e) {
        caught = e;
      }
    }

    if (!isNull(caught)) {
      throw caught;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy