question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suppliers class should provide a valueOf(Supplier) method

See original GitHub issue

Suppliers class has an ofInstance(T instance) method which accept a object of type T as value and return a supplier that provides that value. It would be better off that it also has a method for the opposite direction: given a supplier, return it’s value. Although we can easily get the value of a supplier by calling supplier.get(), it is feasible only when we already defined a supplier explicitly, we can not do it with a Lambda expression. For example:

String str = (() -> { return "abc" + "def"; }).get();

The code above is invalid. We have to write this:

Supplier<String> supplier = () -> {return "abc" + "def"; }; 
String str = supplier.get();

But if Suppliers class can have a valueOf method, we can write this:

String str = valueOf(() -> {return "abc" + "def"; });

That would be much more elegant. Of course, with a very short amount of inner code, it’s not necessary to put code into lambda, but if there are many lines of code, putting it into a Lambda will give us better code separation of concern, hence easier to understand. Besides, providing such a method does no harm to the class.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cardamoncommented, Oct 23, 2017

but if there are many lines of code

Wouldn’t you then just typically put the code in a separate private method and just call it?

0reactions
liachcommented, Oct 24, 2017

You can just cast it into one supplier.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supplier Interface in Java with Examples - GeeksforGeeks
Suppliers are useful when we don't need to supply any value and obtain a result at the same time. The Supplier interface consists...
Read more >
Java Supplier interface example for functional programming
For this Supplier interface tutorial, we will demonstrate how the functional component works by creating a class named RandomDigitSupplier which ...
Read more >
Java 8 Supplier Examples - Mkyong.com
In Java 8, Supplier is a functional interface; it takes no arguments and returns a result. Supplier.java. @FunctionalInterface public interface ...
Read more >
Java Supplier Example - ConcretePage.com
Java Supplier is a functional interface that represents a supplier of results. The functional method of Supplier is get() .
Read more >
When we should use Supplier in Java 8? - Stack Overflow
Let's assume you have a class named MyAmazingClass , and you have a method in it with the name MyEvenBetterMethod (which is static),...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found