org.cryptomator.cryptofs.common.FinallyUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cryptofs Show documentation
Show all versions of cryptofs Show documentation
This library provides the Java filesystem provider used by Cryptomator.
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 extends E>... 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