tempus-fugit

Java micro-library for writing & testing concurrent code

Miscellaneous

Wrap Exceptions

The ExceptionWrapper class allows you to run arbitrary code in a Callable block catching any Throwable and rethrowing as an exception of your choice. The thrown exception embeds the originating exception as it's cause. For example,

1
2
3
4
5
6
ExceptionWrapper.wrapAnyException(new Callable<Object>() {
    @Override
    public Object call() throws ServiceException {
        // nasty code throwing a bunch of exceptions
    }
}, WithException.with(CalendarException.class));

If you'd rather just convert checked exceptions to RuntimeException, just use the 'wrapAsRuntimeException` method. Again, it will embed the originating exception so you won't loose the information.

The example below has the creation of the anonymous Callable class pushed into the method something.

1
ExceptionWrapper.wrapAsRuntimeException(something());

Default Thread Factory

Nothing fancy, the DefaultThreadFactory offers the default implementation of java.util.concurrent.ThreadFactory shown below.

1
2
3
4
5
6
public class DefaultThreadFactory implements ThreadFactory {

    public Thread newThread(Runnable runnable) {
        return new Thread(runnable);
    }
}

Next, Concurrency Utilities »