List of usage examples for java.util.function Supplier get
T get();
From source file:onl.area51.filesystem.http.client.HttpUtils.java
public static void send(char[] path, Function<char[], String> remoteUri, Function<char[], Path> getPath, Supplier<String> userAgent) throws IOException { if (path == null || path.length == 0) { throw new FileNotFoundException("/"); }//ww w . ja v a 2 s .c o m String uri = remoteUri.apply(path); if (uri != null) { HttpEntity entity = new PathEntity(getPath.apply(path)); LOG.log(Level.FINE, () -> "Sending " + uri); HttpPut put = new HttpPut(uri); put.setHeader(USER_AGENT, userAgent.get()); put.setEntity(entity); try (CloseableHttpClient client = HttpClients.createDefault()) { try (CloseableHttpResponse response = client.execute(put)) { int returnCode = response.getStatusLine().getStatusCode(); LOG.log(Level.FINE, () -> "ReturnCode " + returnCode + ": " + response.getStatusLine().getReasonPhrase()); } } } }
From source file:cz.lbenda.gui.controls.TextAreaFrmController.java
public static EventHandler<ActionEvent> openEventHandler(String windowTitle, @Nonnull Supplier<String> oldValueSupplier, @Nonnull Consumer<String> newValueConsumer) { String title = windowTitle == null ? msgDefaultWindowTitle : windowTitle; return event -> { Tuple2<Parent, TextAreaFrmController> tuple2 = TextAreaFrmController.createNewInstance(); tuple2.get2().textProperty().setValue(oldValueSupplier.get()); tuple2.get2().textProperty()/* w w w . j a v a2s . co m*/ .addListener((observable, oldValue, newValue) -> newValueConsumer.accept(newValue)); Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); DialogHelper.getInstance().openWindowInCenterOfStage(stage, tuple2.get2().getMainPane(), title); }; }
From source file:me.rojo8399.placeholderapi.impl.utils.TypeUtils.java
public static <T> T tryOrNull(Supplier<T> funct) { try {//from w ww . ja va 2 s . c om return funct.get(); } catch (Exception e) { return null; } }
From source file:me.rojo8399.placeholderapi.impl.utils.TypeUtils.java
/** * Will only work on unchecked but catchable exceptions. *///from ww w. j a v a 2s . c o m public static <T> Optional<T> tryOptional(Supplier<T> fun) { try { return Optional.ofNullable(fun.get()); } catch (Exception e) { return Optional.empty(); } }
From source file:fr.landel.utils.commons.MapUtils2.java
/** * Create a map from {@code objects}.//from ww w. j a v a 2 s. c om * * <pre> * Map<String, String> map = MapUtils2.newMap(TreeMap::new, Pair.of("key1", "value1"), Pair.of("key2", "value2")); * * // equivalent * Map<String, String> map = new TreeMap<>(); * map.put("key1", "value1"); * map.put("key2", "value2"); * </pre> * * @param mapProvider * map constructor supplier * @param objects * objects pair to put in the new {@link Map} * @param <K> * the type of map key * @param <V> * the type of map value * @param <M> * the type of map * @return the new {@link Map} * @throws NullPointerException * if {@code mapProvider} is {@code null} * @throws IllegalArgumentException * if {@code objects} is {@code null} or empty */ @SafeVarargs public static <K, V, M extends Map<K, V>> M newMap(final Supplier<M> mapProvider, Pair<K, V>... objects) { Objects.requireNonNull(mapProvider); ObjectUtils.requireNonNull(objects, ERROR_OBJECTS_SUPPLIER); final M map = mapProvider.get(); for (Pair<K, V> pair : objects) { map.put(pair.getKey(), pair.getValue()); } return map; }
From source file:com.evolveum.midpoint.schema.SelectorOptions.java
public static <T> Collection<SelectorOptions<T>> updateRootOptions(Collection<SelectorOptions<T>> options, Consumer<T> updater, Supplier<T> newValueSupplier) { if (options == null) { options = new ArrayList<>(); }/* w ww . j a v a 2 s.c om*/ T rootOptions = findRootOptions(options); if (rootOptions == null) { rootOptions = newValueSupplier.get(); options.add(new SelectorOptions<>(rootOptions)); } updater.accept(rootOptions); return options; }
From source file:com.wrmsr.kleist.util.Itertools.java
public static <T> Iterator<T> lazyIterator(Supplier<? extends T> supplier) { return new Iterator<T>() { private boolean done; @Override// w ww. j a va2 s . c o m public boolean hasNext() { return !done; } @Override public T next() { if (done) { throw new IllegalStateException(); } done = true; return supplier.get(); } }; }
From source file:org.apache.jena.rdfconnection.RDFConnectionRemote.java
/** Convert HTTP status codes to exceptions */ static protected <X> X exec(Supplier<X> action) { try {//ww w .j ava 2 s . c o m return action.get(); } catch (HttpException ex) { handleHttpException(ex, true); return null; } }
From source file:com.simiacryptus.mindseye.applications.ArtistryUtil.java
/** * Log exception with default t./*from w w w. j a v a 2 s.com*/ * * @param <T> the type parameter * @param log the log * @param fn the fn * @param defaultValue the default value * @return the t */ public static <T> T logExceptionWithDefault(@Nonnull final NotebookOutput log, Supplier<T> fn, T defaultValue) { try { return fn.get(); } catch (Throwable throwable) { try { log.code(() -> { return throwable; }); } catch (Throwable e2) { } return defaultValue; } }
From source file:fr.landel.utils.commons.MapUtils2.java
/** * Create a map from {@code objects}.//from w w w. j av a 2s .co m * * <pre> * Map<String, String> map = MapUtils2.newMap(TreeMap::new, "key1", "value1", "key2", "value2"); * * // equivalent * Map<String, String> map = new TreeMap<>(); * map.put("key1", "value1"); * map.put("key2", "value2"); * </pre> * * @param mapProvider * map constructor supplier * @param objects * objects pair to put in the new {@link Map} * @param <K> * the type of map key * @param <V> * the type of map value * @param <M> * the type of map * @return the new {@link Map} * @throws NullPointerException * if {@code mapProvider} is {@code null} * @throws IllegalArgumentException * if {@code objects} is {@code null} or empty or if * {@code objects} length is even */ @SuppressWarnings("unchecked") @SafeVarargs public static <K, V, M extends Map<K, V>> M newMap(final Supplier<M> mapProvider, Object... objects) { Objects.requireNonNull(mapProvider); if (objects == null || objects.length % 2 != 0) { throw new IllegalArgumentException(ERROR_OBJECTS_ODD); } final M map = mapProvider.get(); for (int i = 0; i < objects.length; i += 2) { map.put((K) objects[i], (V) objects[i + 1]); } return map; }