Questions tagged [supplier]

70 questions
30
votes
3 answers

Why are Optional's or and flatMap methods' supplier type parameters wildcards?

The Optional.or method was added in Java 9. This is the method signature public Optional or​(Supplier> supplier) Why is the type parameter of the Supplier taking ? extends Optional rather than just Optional, since…
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
10
votes
2 answers

Method reference does not always seem to capture instance

I know there are many questions on the subject even a very recent one but I still can't work my head around one thing. Consider the following functional interface: @FunctionalInterface interface PersonInterface { String getName(); } And this…
Bentaye
  • 9,403
  • 5
  • 32
  • 45
10
votes
1 answer

Java 8 supplier behaviour: final variable might not be initialized

Java disallows usage of final variable inside a supplier as it might not be initialized, yet prepending "(this)." to variable makes it compile and run fine. Furthermore calling such supplier results in NullPointerException instead of compiler error…
kristjank
  • 103
  • 6
9
votes
3 answers

Why does Optional not implement Supplier?

We all know that Optional has a method T get(), so why does it not implement Supplier? If there happens to be no reason why, would it break any previous code if Oracle were to implement it into a future version of Java?
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
7
votes
3 answers

Using Lambda functions to Consume all objects provided by a Supplier

Looking for how to use Java lambda functions so Consumer can process all objects provided by a Supplier, and get rid of the explicit while loop and null checks. I have a Supplier of String keys for a database and I want to use a Consumer to process…
ScottK
  • 1,526
  • 1
  • 16
  • 23
6
votes
2 answers

Practical uses for the option to pass `Supplier` as a message supplier in JUnit 5

The Assertions class in JUnit 5 allows for passing an Supplier as a messageSupplier, an object that provides the text of a message to report when the test fails. For example, assertEquals: public static void assertEquals​( char expected, …
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
6
votes
1 answer

java: cache database table with 1 hour refresh

I need some cache solution for java. I need to store content of 1 db table with 1 hour eviction then clear all values and populate data from table again. I was looking into guava cache but it seems it does not provide required api in CacheLoader.…
Normal
  • 1,347
  • 4
  • 17
  • 34
4
votes
3 answers

BiSupplier in Java8

I have seen BiConsumer, BiPredicate, BiFunction but not BiSupplier or similar. I tried the below code but got an exception saying: "Multiple non-overriding abstract methods found in BiSupplier". @FunctionalInterface public interface BiSupplier
Alex Man
  • 4,746
  • 17
  • 93
  • 178
4
votes
3 answers

Java how to store getters in map

I would like to call a dynamic getter by a given enum. I want to define it in a static map. But I'm not sure how my object will use the method. I have the Color enum and the object Library. public enum Color { Red, Blue, Black, Green, …
userit1985
  • 961
  • 1
  • 13
  • 28
3
votes
1 answer

Why is it compiled? - T = Supplier?

I have an 'Animal' interface and 'Dog' class that implements 'Animal'. public interface Animal { void makeVoice(); } public class Dog implements Animal { @Override public void makeVoice() { System.out.println("aou aou"); …
3
votes
2 answers

Testing lazy initialization by j.u.f.Supplier with Mockito

I have a class Sut with lazy initialization implemented using java.util.function.Supplier. In fact it's more complicated that the code below, but this is the simplest form that Mockito cannot test. The test below throws an error Wanted but not…
Peter
  • 1,512
  • 1
  • 22
  • 40
3
votes
1 answer

Java: Get class type of return object of generic supplier

Let's assume the following definition is given: final Supplier supplier = MyClass::new; Is there a way I can get MyClass.class without actually invoking .get() on the supplier? Why? I have to know the specified class to make some logical…
товіаѕ
  • 2,881
  • 4
  • 23
  • 53
3
votes
2 answers

Can Scala's "call by name" be considered as a syntactic sugar of Java8's Functional Interface API?

Example of Scala's "call be name": def giveMeName(b: => String) Java result: public class some.package.CallByNameEx { public void giveMeName(scala.Function0); public some.package.CallByNameEx(); } Example of Java's…
6harat
  • 542
  • 6
  • 18
2
votes
1 answer

Reduce cannot infer proper method version

I would like to make my code more generic. And in order to achieve that, I proved as method parameters both a list and a supplier. It turns out that reduce() cannot infer specific method in case where there are multiple to choose (see screenshot…
asdfgh
  • 2,823
  • 4
  • 21
  • 26
2
votes
1 answer

Jlanguage does not implement the requested interface java.util.function.Supplier

CODE: Same code with out Microservice(Run as main class) giving correct output but after making it @service class(converting into microservice), it fails. I still do not know which supplier I have to implement. getSuggestedReplacements() has…
Yashika Chandra
  • 163
  • 3
  • 13
1
2 3 4 5