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

au.csiro.sparkle.common.LoanUtils Maven / Gradle / Ivy

The newest version!
package au.csiro.sparkle.common;

import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.io.IOUtils;

public final class LoanUtils {

	public static  void withCloseable(final C in, ThrowingConsumer func) {
		try {
			func.accept(in);
		} catch (Exception ex) {
			throw new RuntimeException(ex);		
		} finally {
			IOUtils.closeQuietly(in);
		}		
	}

	public static  T withCloseableFunc(final C in, ThrowingFunction func) {
		try {
			return func.apply(in);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			IOUtils.closeQuietly(in);
		}		
	}
	
	public static void withPrintWriter(final File in, ThrowingConsumer func) {
		try {
	        LoanUtils.withCloseable(new PrintWriter(new FileWriter(in)), func);
        } catch (IOException ex) {
	        throw new RuntimeException(ex);
        }		
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy