List of usage examples for java.util.function Supplier get
T get();
From source file:co.runrightfast.core.utils.LoggingUtils.java
/** * Helper for logging JSON messages// w w w . j a va 2 s . c o m * * @param logger * @param level * @param className * @param method * @param message * @param exception */ static void log(@NonNull final Logger logger, final Level level, final String className, final String method, @NonNull final Supplier<JsonObject> message, @NonNull final Throwable exception) { logger.logp(level, className, method, exception, () -> message.get().toString()); }
From source file:io.apiman.cli.util.DeclarativeUtil.java
/** * Check for the presence of an item using the given Supplier. * * @param supplier the Supplier of the item * @param <T>//from www . j a v a 2 s . c o m * @return the item or {@link Optional#empty()} */ public static <T> Optional<T> checkExists(Supplier<T> supplier) { try { // attempt to return the item return ofNullable(supplier.get()); } catch (RetrofitError re) { // 404 indicates the item does not exist - anything else is an error if (ofNullable(re.getResponse()) .filter(response -> HttpURLConnection.HTTP_NOT_FOUND == response.getStatus()).isPresent()) { return empty(); } throw new DeclarativeException("Error checking for existence of existing item", re); } }
From source file:io.syndesis.rest.v1.state.ClientSideStateProperties.java
private static <T> T value(final T given, final Consumer<T> setter, final Supplier<T> generator) { if (given != null && !String.valueOf(given).trim().isEmpty()) { return given; }/*from w w w . j a v a 2s . com*/ final T newValue = generator.get(); setter.accept(newValue); return newValue; }
From source file:Main.java
/** * Converts the given iterator to collection created via the provided {@link Supplier}. * <p>/*w ww . java2s. co m*/ * Example use: <code>List<String> list = CollectionUtils.toCollection(iterator, LinkedList::new);</code> * * @param <T> * the generic type * @param <C> * the generic type * @param iterator * the iterator * @param newInstance * the new instance * @return the c */ public static <T, C extends Collection<T>> C toCollection(Iterator<T> iterator, Supplier<C> newInstance) { Objects.requireNonNull(iterator, "The iterator is required"); Objects.requireNonNull(newInstance, "New Instance supplier is required"); C list = newInstance.get(); iterator.forEachRemaining(list::add); return list; }
From source file:com.spotify.styx.api.Middlewares.java
public static Middleware<AsyncHandler<? extends Response<?>>, AsyncHandler<? extends Response<ByteString>>> clientValidator( Supplier<Optional<List<String>>> supplier) { return innerHandler -> requestContext -> { if (requestContext.request().header("User-Agent") .map(header -> supplier.get().orElse(ImmutableList.of()).contains(header)).orElse(false)) { // TODO: fire some stats return CompletableFuture.completedFuture(Response.forStatus( Status.NOT_ACCEPTABLE.withReasonPhrase("blacklisted client version, please upgrade"))); } else {/*from w ww . j av a 2 s.c o m*/ // noinspection unchecked return (CompletionStage<Response<ByteString>>) innerHandler.invoke(requestContext); } }; }
From source file:ch.ivyteam.ivy.maven.engine.deploy.MarkerFileDeployer.java
private static void wait(Supplier<Boolean> condition, long duration, TimeUnit unit) throws TimeoutException { long waitMs = unit.toMillis(duration); long startMs = System.currentTimeMillis(); long maxMs = waitMs + startMs; while (!condition.get()) { try {// w w w . j a v a 2 s.c o m if (System.currentTimeMillis() > maxMs) { throw new TimeoutException("Operation reached timeout of " + duration + " " + unit); } Thread.sleep(100); } catch (InterruptedException ex) { } } }
From source file:Main.java
/** * Runs arbitrary code in the Event Dispatch Thread. * <p>//from w w w . j av a2 s.c o m * The first operation performed by this method is fetching the {@code Object} to return. If {@code supplier} is not {@code null}, its {@code get()} method will be called to fetch the {@code Object}. But if {@code supplier} is {@code null}, or it * produces a {@code null} "value" from its {@code get()} method, then {@code defaultObject}, which may also be {@code null}, will be used instead. * <p> * The second operation performed by this method is handling the {@code Object} to return. So if neither {@code consumer} nor the fetched {@code Object} are {@code null}, the fetched {@code Object} will be handed to {@code consumer}. * <p> * Returns the fetched {@code Object}, which may be {@code null}. * * @param <T> the type of the returned result * @param supplier the {@code Supplier}, which may be {@code null} * @param consumer the {@code Consumer}, which may be {@code null} * @param defaultObject the default {@code Object}, which may be {@code null} * @return the fetched {@code Object}, which may be {@code null} */ public static <T> T runInEDT(final Supplier<T> supplier, final Consumer<T> consumer, final T defaultObject) { final AtomicReference<T> atomicReference = new AtomicReference<>(); if (supplier != null) { runInEDT(() -> atomicReference.set(supplier.get())); } if (defaultObject != null) { atomicReference.compareAndSet(null, defaultObject); } if (atomicReference.get() != null && consumer != null) { runInEDT(() -> consumer.accept(atomicReference.get())); } return atomicReference.get(); }
From source file:org.fuin.esc.eshttp.ESHttpEventStoreIT.java
private static void waitFor(final Supplier<Boolean> finished, final int maxTries) { int tries = 0; while (!finished.get() && (tries < maxTries)) { if (!finished.get()) { sleep(100);/*from w w w . j av a 2s . c om*/ tries++; } } if (!finished.get()) { throw new IllegalStateException("Waiting for result failed!"); } }
From source file:nu.yona.server.subscriptions.entities.Buddy.java
/** * Determines the name of the user (first or last) through an algorithm that ensures the best possible value is returned, but * never null. It first tries calling the getter that is supposed to take it from the buddy entity or message. If that returns * null and a user is given and that user is not yet migrated (i.e. the name is not removed from it), it tries to get that. If * that doesn't return anything either, it builds a string based on the given nickname. * // www . j a v a 2 s . c om * @param buddyUserNameGetter Getter to fetch the name from the buddy entity or a message * @param user Optional user entity * @param userNameGetter Getter to fetch the name (first or last) from the user entity * @param fallbackMessageId The ID of the translatable message to build the fallback string * @param nickname The nickname to include in the fallback string * @return The name or a substitute for it (never null) */ public static String determineName(Supplier<String> buddyUserNameGetter, Optional<User> user, Function<User, String> userNameGetter, String fallbackMessageId, String nickname) { String name = buddyUserNameGetter.get(); if (name != null) { return name; } if ((user.isPresent()) && (user.get().getPrivateDataMigrationVersion() < VERSION_OF_NAME_MIGRATION)) { // User is not deleted yet and not yet migrated, so get the name from the user entity name = userNameGetter.apply(user.get()); } if (name != null) { return name; } // We're apparently in a migration process to move first and last name to the private data // The app will fetch the message, causing processing of all unprocessed messages. That'll fill in the first and last // name in the buddy entity, so from then onward, the user will see the right data return Translator.getInstance().getLocalizedMessage(fallbackMessageId, nickname); }
From source file:org.fuin.esc.eshttp.ESHttpEventStoreIT.java
private static void executeMultipleAndWaitFor(final Runnable runnable, final Supplier<Boolean> finished, final int maxTries) { int tries = 0; do {/* w w w . j a va2 s.c o m*/ runnable.run(); if (!finished.get()) { sleep(100); tries++; } } while (!finished.get() || (tries == maxTries)); if (!finished.get()) { throw new IllegalStateException("Waiting for result failed!"); } }