logo
down
shadow

Adapt `Callable` to `Supplier` in Java8


Adapt `Callable` to `Supplier` in Java8

Content Index :

Adapt `Callable` to `Supplier` in Java8
Tag : function-programming , By : Amin Amini
Date : November 24 2020, 01:01 AM

wish helps you There is nothing wrong with classes providing static methods (or even a single static method). I’m not sure what you actually want to do, especially as the need for additional parameters is not clear but let’s suppose you are using it somewhere in the exception handling code and treating everything else as you have written, I guess what you actually want to do is:
public class FooDelegate {
    public static <T> Supplier<T> call(Key key, Callable<T> callable) {
        return () -> {
            try {
                return callable.call();
            }
            catch(ExecutionException e) {
                //...
                throw new RuntimeException(); // or return fall-back
            }
            catch(Exception ex) {
                //... handle exceptions etc.
                throw new RuntimeException(); // or return fall-back
            }
        };
    }
}
final Key key = null;//...;
return CompletableFuture.supplyAsync(FooDelegate.call( key, () -> doSomething() ));
final Key key = null;//...;
return CompletableFuture.supplyAsync(FooDelegate.call(key, MyClass::doSomething ));
final Key key = null;//...;
return CompletableFuture.supplyAsync(FooDelegate.call(key, this::doSomething ));
public class FooDelegate {
    public static <T> Supplier<T> supplier(Key key, Callable<T> callable) {
        return () -> call(key, callable);
    }
    public static <T> T call(Key key, Callable<T> callable) {
        try {
            return callable.call();
        }
        catch(ExecutionException e) {
            //...
            throw new RuntimeException(); // or return fall-back
        }
        catch(Exception ex) {
            //... handle exceptions etc.
            throw new RuntimeException(); // or return fall-back
        }
    }
}
final Key key = null;//...;
return CompletableFuture.supplyAsync(FooDelegate.supplier(key, () -> doSomething() ));

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Java8 Supplier that is an optional Consumer


Tag : java , By : kangfoo2
Date : March 29 2020, 07:55 AM
should help you out I always favour passing mandatory settings via the constructor (or a static factory method) The IoC style I prefer is
app.setFooSupplier(() -> new FooImpl(app.component()));
Component comp = app.component();
app.setFooSupplier(() -> new FooImpl(comp));

java8 - use Function interface to replace Consumer or Supplier


Tag : development , By : nemof
Date : March 29 2020, 07:55 AM
it helps some times A Consumer can be viewed as a Function. A Supplier can be viewed as a Function. You'll have to return null from your consumer written as a function, and take (and ignore) a Void as argument from your supplier written as a function.
Not sure I get the point of this, though.

Java8 Supplier interface to provide the proper typed instance


Tag : java , By : Nick
Date : March 29 2020, 07:55 AM
I hope this helps . Because you're using wildcards as the generic type of Thingy within thingyclasses, you're essentially saying: The types of Thingy can be anything; however, this prohibits the compiler from inferring anything about the type, hence why you require an explicit cast. You can use Supplier to improve it slightly, but you'll still have the warning regarding an unchecked cast:
class ThingyFactory {
    private Map<String, Supplier<Thingy<?, ?>>> providers = new ConcurrentHashMap<>();

    @SuppressWarnings("unchecked")
    public <K, V> Supplier<Thingy<K, V>> getInstance(String classname) {
        return () -> (Thingy<K, V>) providers.get(classname);
    }
}

Java8: Using an IntStream as a Supplier parameter for custom Collector


Tag : java , By : CrookedNumber
Date : March 29 2020, 07:55 AM
it helps some times By calling concat, you are not mutating s1. concat returns the concatenated stream. Streams are not mutable in the first place.
This means that the Stream.empty you started with never changed, and it is used in the concat method again and again. However, you can't use a stream in concat twice. That counts as "operate upon the stream". So when you try to do it the second time, you got an illegal state exception as documented:
public static int uniqueLetters(Stream<String> stream){
    return (int)stream.flatMapToInt(String::chars).distinct().count();
}

Callable vs Supplier interface in java


Tag : java , By : Scott Everts
Date : March 29 2020, 07:55 AM
hope this fix your issue Their difference in usage can be seen from their respective documentation:
Callable:
Related Posts Related QUESTIONS :
shadow
Privacy Policy - Terms - Contact Us © scrbit.com