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

org.hibernate.search.util.impl.Closeables Maven / Gradle / Ivy

There is a newer version: 5.11.12.Final
Show newest version
/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.search.util.impl;

import java.io.Closeable;
import java.io.IOException;

import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
import java.lang.invoke.MethodHandles;

/**
 * @author Yoann Rodiere
 */
public final class Closeables {

	private static final Log log = LoggerFactory.make( MethodHandles.lookup() );

	private Closeables() {
		// Private constructor
	}

	/**
	 * Close multiple resources, ensuring that all resources get closed
	 * even if one of them throws a Throwable.
	 * 

* The first caught throwable will be re-thrown, and any additional * throwable will be {@link Throwable#addSuppressed(Throwable) suppressed}. * * @param closeables The resources to close * @throws E if a closeable throws such an exception */ public static void close(Iterable> closeables) throws E { try ( Closer closer = new Closer<>() ) { closer.pushAll( GenericCloseable::close, closeables ); } } /** * Close a resource without throwing an exception. * * @param resource the resource to close */ public static void closeQuietly(Closeable resource) { if ( resource != null ) { try { resource.close(); } catch (IOException | RuntimeException e) { //we don't really care if we can't close log.couldNotCloseResource( e ); } } } /** * Close resources without throwing an exception. * * @param resourceIterables the iterables containing the resources to close */ @SafeVarargs public static void closeQuietly(Iterable ... resourceIterables) { for ( Iterable resourceIterable : resourceIterables ) { for ( Closeable resource : resourceIterable ) { closeQuietly( resource ); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy