static void processConfigOptions(String rootDir, String module, String packageName, String pathPrefix, ThrowingConsumer<Class<?>, IOException> classConsumer) throws IOException, ClassNotFoundException { Path configDir = Paths.get(rootDir, module, pathPrefix, packageName.replaceAll("\\.", "/")); try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) { for (Path entry : stream) { String fileName = entry.getFileName().toString(); Matcher matcher = CLASS_NAME_PATTERN.matcher(fileName); if (matcher.matches()) { final String className = packageName + '.' + matcher.group(CLASS_NAME_GROUP); if (!EXCLUSIONS.contains(className)) { Class<?> optionsClass = Class.forName(className); classConsumer.accept(optionsClass); } } } } }
/** * Converts a {@link ThrowingConsumer} into a {@link Consumer} which throws checked exceptions * as unchecked. * * @param throwingConsumer to convert into a {@link Consumer} * @param <A> input type * @return {@link Consumer} which throws all checked exceptions as unchecked */ public static <A> Consumer<A> uncheckedConsumer(ThrowingConsumer<A, ?> throwingConsumer) { return (A value) -> { try { throwingConsumer.accept(value); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
/** * This method supplies all elements from the input to the consumer. Exceptions that happen on elements are * suppressed until all elements are processed. If exceptions happened for one or more of the inputs, they are * reported in a combining suppressed exception. * * @param inputs iterator for all inputs to the throwingConsumer. * @param throwingConsumer this consumer will be called for all elements delivered by the input iterator. * @param <T> the type of input. * @throws Exception collected exceptions that happened during the invocation of the consumer on the input elements. */ public static <T> void applyToAllWhileSuppressingExceptions( Iterable<T> inputs, ThrowingConsumer<T, ? extends Exception> throwingConsumer) throws Exception { if (inputs != null && throwingConsumer != null) { Exception exception = null; for (T input : inputs) { if (input != null) { try { throwingConsumer.accept(input); } catch (Exception ex) { exception = ExceptionUtils.firstOrSuppressed(ex, exception); } } } if (exception != null) { throw exception; } } }
/** * Converts a {@link ThrowingConsumer} into a {@link Consumer} which throws checked exceptions * as unchecked. * * @param throwingConsumer to convert into a {@link Consumer} * @param <A> input type * @return {@link Consumer} which throws all checked exceptions as unchecked */ public static <A> Consumer<A> uncheckedConsumer(ThrowingConsumer<A, ?> throwingConsumer) { return (A value) -> { try { throwingConsumer.accept(value); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
/** * This method supplies all elements from the input to the consumer. Exceptions that happen on elements are * suppressed until all elements are processed. If exceptions happened for one or more of the inputs, they are * reported in a combining suppressed exception. * * @param inputs iterator for all inputs to the throwingConsumer. * @param throwingConsumer this consumer will be called for all elements delivered by the input iterator. * @param <T> the type of input. * @throws Exception collected exceptions that happened during the invocation of the consumer on the input elements. */ public static <T> void applyToAllWhileSuppressingExceptions( Iterable<T> inputs, ThrowingConsumer<T, ? extends Exception> throwingConsumer) throws Exception { if (inputs != null && throwingConsumer != null) { Exception exception = null; for (T input : inputs) { if (input != null) { try { throwingConsumer.accept(input); } catch (Exception ex) { exception = ExceptionUtils.firstOrSuppressed(ex, exception); } } } if (exception != null) { throw exception; } } }
/** * This method supplies all elements from the input to the consumer. Exceptions that happen on elements are * suppressed until all elements are processed. If exceptions happened for one or more of the inputs, they are * reported in a combining suppressed exception. * * @param inputs iterator for all inputs to the throwingConsumer. * @param throwingConsumer this consumer will be called for all elements delivered by the input iterator. * @param <T> the type of input. * @throws Exception collected exceptions that happened during the invocation of the consumer on the input elements. */ public static <T> void applyToAllWhileSuppressingExceptions( Iterable<T> inputs, ThrowingConsumer<T, ? extends Exception> throwingConsumer) throws Exception { if (inputs != null && throwingConsumer != null) { Exception exception = null; for (T input : inputs) { if (input != null) { try { throwingConsumer.accept(input); } catch (Exception ex) { exception = ExceptionUtils.firstOrSuppressed(ex, exception); } } } if (exception != null) { throw exception; } } }
private void tryRemoveCompletedCheckpoint(CompletedCheckpoint completedCheckpoint, ThrowingConsumer<CompletedCheckpoint, Exception> discardCallback) { try { if (tryRemove(completedCheckpoint.getCheckpointID())) { executor.execute(() -> { try { discardCallback.accept(completedCheckpoint); } catch (Exception e) { LOG.warn("Could not discard completed checkpoint {}.", completedCheckpoint.getCheckpointID(), e); } }); } } catch (Exception e) { LOG.warn("Failed to subsume the old checkpoint", e); } }
private void tryRemoveCompletedCheckpoint(CompletedCheckpoint completedCheckpoint, ThrowingConsumer<CompletedCheckpoint, Exception> discardCallback) { try { if (tryRemove(completedCheckpoint.getCheckpointID())) { executor.execute(() -> { try { discardCallback.accept(completedCheckpoint); } catch (Exception e) { LOG.warn("Could not discard completed checkpoint {}.", completedCheckpoint.getCheckpointID(), e); } }); } } catch (Exception e) { LOG.warn("Failed to subsume the old checkpoint", e); } }
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate( SupplierWithException<TtlValue<V>, SE> getter, ThrowingConsumer<TtlValue<V>, CE> updater, ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE { TtlValue<V> ttlValue = getter.get(); if (ttlValue == null) { return null; } else if (expired(ttlValue)) { stateClear.run(); if (!returnExpired) { return null; } } else if (updateTsOnRead) { updater.accept(rewrapWithNewTs(ttlValue)); } return ttlValue; } }
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate( SupplierWithException<TtlValue<V>, SE> getter, ThrowingConsumer<TtlValue<V>, CE> updater, ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE { TtlValue<V> ttlValue = getter.get(); if (ttlValue == null) { return null; } else if (expired(ttlValue)) { stateClear.run(); if (!returnExpired) { return null; } } else if (updateTsOnRead) { updater.accept(rewrapWithNewTs(ttlValue)); } return ttlValue; } }