From 8cada6d0a231e67deca0edcab6312bea5e50f1b6 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Sun, 30 Apr 2017 00:21:29 +0200 Subject: [PATCH] Guava convenience functions instead of reinventing the wheel --- .../java/org/cryptomator/common/LazyInitializer.java | 12 +++++------- .../launcher/InterProcessCommunicator.java | 4 +++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/main/commons/src/main/java/org/cryptomator/common/LazyInitializer.java b/main/commons/src/main/java/org/cryptomator/common/LazyInitializer.java index 4d166bbb3..0d14ce339 100644 --- a/main/commons/src/main/java/org/cryptomator/common/LazyInitializer.java +++ b/main/commons/src/main/java/org/cryptomator/common/LazyInitializer.java @@ -4,6 +4,8 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import com.google.common.base.Throwables; + public final class LazyInitializer { private LazyInitializer() { @@ -41,11 +43,8 @@ public final class LazyInitializer { try { return reference.updateAndGet(invokeFactoryIfNull(factory)); } catch (InitializationException e) { - if (exceptionType.isInstance(e.getCause())) { - throw exceptionType.cast(e.getCause()); - } else { - throw e; - } + Throwables.throwIfInstanceOf(e.getCause(), exceptionType); + throw e; } } } @@ -55,9 +54,8 @@ public final class LazyInitializer { if (currentValue == null) { try { return factory.get(); - } catch (RuntimeException e) { - throw e; // don't catch unchecked exceptions } catch (Exception e) { + Throwables.throwIfUnchecked(e); // don't catch unchecked exceptions throw new InitializationException(e); } } else { diff --git a/main/launcher/src/main/java/org/cryptomator/launcher/InterProcessCommunicator.java b/main/launcher/src/main/java/org/cryptomator/launcher/InterProcessCommunicator.java index 3c77aadf0..ed72e17be 100644 --- a/main/launcher/src/main/java/org/cryptomator/launcher/InterProcessCommunicator.java +++ b/main/launcher/src/main/java/org/cryptomator/launcher/InterProcessCommunicator.java @@ -26,6 +26,8 @@ import org.apache.commons.lang3.SystemUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.io.MoreFiles; + /** * First running application on a machine opens a server socket. Further processes will connect as clients. */ @@ -221,7 +223,7 @@ abstract class InterProcessCommunicator implements InterProcessCommunicationProt ByteBuffer buf = ByteBuffer.allocate(Integer.BYTES); buf.putInt(port); buf.flip(); - Files.createDirectories(path.getParent()); + MoreFiles.createParentDirectories(path); try (WritableByteChannel ch = Files.newByteChannel(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { if (ch.write(buf) != Integer.BYTES) { throw new IOException("Did not write expected number of bytes.");