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

org.cryptomator.cryptofs.common.FinallyUtil Maven / Gradle / Ivy

There is a newer version: 2.7.1-beta1
Show newest version
package org.cryptomator.cryptofs.common;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@Singleton
public class FinallyUtil {

	@Inject
	public FinallyUtil() {
	}

	@SuppressWarnings({"unchecked"})
	public  void guaranteeInvocationOf(RunnableThrowingException... tasks) throws E {
		this.guaranteeInvocationOf(Arrays.stream(tasks));
	}

	public  void guaranteeInvocationOf(Iterable> tasks) throws E {
		this.guaranteeInvocationOf(StreamSupport.stream(tasks.spliterator(), false));
	}

	@SuppressWarnings({"unchecked"})
	public  void guaranteeInvocationOf(Stream> tasks) throws E {
		this.guaranteeInvocationOf(tasks.map(t -> (RunnableThrowingException) t).iterator());
	}

	@SuppressWarnings("unchecked")
	public  void guaranteeInvocationOf(Iterator> tasks) throws E {
		if (tasks.hasNext()) {
			RunnableThrowingException next = tasks.next();
			try {
				next.run();
			} catch (Exception e) {
				throw (E) e;
			} finally {
				guaranteeInvocationOf(tasks);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy