/** * @return Generate a weak Etag using the {@link #path()}, {@link #lastModified()} and * {@link #length()}. */ @Nonnull default String etag() { try { StringBuilder b = new StringBuilder(32); b.append("W/\""); BaseEncoding b64 = BaseEncoding.base64(); int lhash = resource().toURI().hashCode(); b.append(b64.encode(Longs.toByteArray(lastModified() ^ lhash))); b.append(b64.encode(Longs.toByteArray(length() ^ lhash))); b.append('"'); return b.toString(); } catch (URISyntaxException x) { throw Throwing.sneakyThrow(x); } }
throw Throwing.sneakyThrow(ex.getCause()); } catch (Exception ex) { throw Throwing.sneakyThrow(ex);
@Override public V get() { throw Throwing.sneakyThrow(x); }
@Override default boolean test(V1 v1, V2 v2) { try { return tryTest(v1, v2); } catch (Throwable x) { throw sneakyThrow(x); } } }
private static <V> V fnWrap(Supplier<V> fn, java.util.function.Function<Throwable, Exception> wrapper) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } }
private static <X extends Throwable> void runOnFailure(Runnable action, Class<? extends X> type, java.util.function.Consumer<X> consumer) { try { action.tryRun(); } catch (Throwable x) { if (type.isInstance(x)) { consumer.accept(type.cast(x)); } throw sneakyThrow(x); } }
private static void runAction(Runnable action) { try { action.tryRun(); } catch (Throwable x) { throw sneakyThrow(x); } }
private static <V> V fn(Supplier<V> fn) { try { return fn.tryGet(); } catch (Throwable x) { throw sneakyThrow(x); } }
@Override default boolean test(V v) { try { return tryTest(v); } catch (Throwable x) { throw sneakyThrow(x); } } }
private static <V, X extends Throwable> V fnOnFailure(Supplier<V> fn, Class<? extends X> type, java.util.function.Consumer<X> consumer) { try { return fn.tryGet(); } catch (Throwable x) { if (type.isInstance(x)) { consumer.accept(type.cast(x)); } throw sneakyThrow(x); } }
private static void runWrap(Runnable action, java.util.function.Function<Throwable, Exception> wrapper) { try { action.tryRun(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } }
private static <V, X extends Throwable> V fnUnwrap(Supplier<V> fn, Class<? extends X> type) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } }
private static <R, X extends Throwable> R fnRecover(Supplier<R> fn, Class<? extends X> type, java.util.function.Function<X, R> recover) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } if (type.isInstance(x)) { return recover.apply(type.cast(x)); } throw sneakyThrow(x); } }
private static <X extends Throwable> void runUnwrap(Runnable action, Class<? extends X> type) { try { action.tryRun(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } }
public R orElseThrow(Throwing.Supplier<Throwable> exception) { return toOptional().orElseThrow(() -> Throwing.sneakyThrow(exception.get())); }
/** * @return Generate a weak Etag using the {@link #path()}, {@link #lastModified()} and * {@link #length()}. */ @Nonnull default String etag() { try { StringBuilder b = new StringBuilder(32); b.append("W/\""); BaseEncoding b64 = BaseEncoding.base64(); int lhash = resource().toURI().hashCode(); b.append(b64.encode(Longs.toByteArray(lastModified() ^ lhash))); b.append(b64.encode(Longs.toByteArray(length() ^ lhash))); b.append('"'); return b.toString(); } catch (URISyntaxException x) { throw Throwing.sneakyThrow(x); } }
private static <R> R fn(Supplier<R> fn, Supplier<R> orElse) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } return orElse.get(); } }
/** * Get the success value or throw an exception created by the exception provider. * * @param provider Exception provider. * @return Success value. */ public V orElseThrow(Throwing.Function<Throwable, Throwable> provider) { if (isSuccess()) { return get(); } throw Throwing.sneakyThrow(provider.apply(getCause().get())); }