35

Please explain the use of the interface Supplier(in Guava) with a suitable example .

Emil
  • 12,811
  • 17
  • 65
  • 106

6 Answers6

41

The Supplier interface is simply an abstraction of a no-arg function that returns a value... it is a means of getting some instance or instances of an object. Since it is so general, it can be used as many things. Jared explained how the Multimaps factories utilize it as a factory for creating a new instance of a Collection of some type for values.

Given the simplicity of the interface, it also allows for some very powerful decoration of a Supplier's behavior by wrapping it in another Supplier that alters its behavior somehow. Memoization is one example of that. I've used the Suppliers.memoizeWithExpiration method myself as an easy way to make it so some data will only be read from a server at most once in a given period of time.

I'd also recommend taking a look at Guice and how the Provider interface is used in it. Provider is exactly equivalent to Supplier and is central to how Guice works.

  • Provider allows users to define a custom way of creating new objects of a given class. Users can write a get() method which can execute whatever code is needed to create a new object, so they aren't limited to having Guice use constructors alone to create objects. Here, they are using it to define a custom factory for new instance of an object.
  • Guice allows injection of a Provider of any dependency. This may return a new instance every time get() is called or it may always return a single instance or anything in between, depending on how the binding the Provider represents is scoped. This also allows for "lazy instantiation" of dependencies... the Provider gives a class a means of creating an object without needing to actually create the object ahead of time. An instance of the object does not need to be created until when, and if, get() is called.
  • As indicated above, Providers form the basis of scoping in Guice. If you take a look at the Scope interface, you'll notice its single method Provider<T> scope(Key<T> key, Provider<T> unscoped) is defined in terms of Providers. This method takes something that creates a new instance of an object (the Provider<T> unscoped) and returns a Provider<T> based on that which applies whatever policy the scope defines, potentially returning some cached instance of the object rather than creating a new one. The default NO_SCOPE scope simply passes along the unscoped provider, meaning a new instance will be created each time. The SINGLETON scope caches the result of the first call to unscoped.get() and thereafter returns that single instance, ensuring that everything that depends on the singleton-scoped object gets a reference to that single object. Note that the Provider returned by the SINGLETON scope's scope method does essentially the same thing as the Supplier returned by Suppliers.memoize (though it's a bit more complicated).
martieva
  • 131
  • 4
  • 10
ColinD
  • 103,631
  • 27
  • 195
  • 199
14

The main reason we included Supplier in Guava was to support the Multimaps methods that generate arbitrary Multimaps, such as

public static <K,V> Multimap<K,V> newMultimap(Map<K,Collection<V>> map,
    Supplier<? extends Collection<V>> factory)

The Supplier creates a Collection that holds all of the values for a given key. The Multimap uses the Supplier whenever you store a key-value pair with a key that's not already in the Multimap.

deekshith
  • 1,266
  • 12
  • 15
Jared Levy
  • 1,846
  • 12
  • 12
  • Do you know about mnemonize in suppliers ?If so please give a sample for that too. – Emil Nov 10 '10 at 07:55
  • 1
    @Emil: `memoize` gets use e.g. [here](http://stackoverflow.com/questions/3636244/thread-safe-cache-of-one-object-in-java/3636791#3636791) – maaartinus Sep 14 '11 at 08:27
7

It's a way to provide an indirect object. You may want to provide another object each time Supplier.get() is called.

For example, i have a singleton class called SmtpMailSender, which takes a hostname for the smtp server. However, the hostname can change at runtime, so instead of taking a String hostname, it takes a Supplier<String> hostname.

Christoffer Hammarström
  • 24,236
  • 4
  • 44
  • 54
3

Another example use of Supplier:

http://javawayoflife.blogspot.com/2010/06/unit-test-and-new-date.html

amertum
  • 31
  • 1
1

See the Suppliers class and I guess the methods there will somehow answer your question.

Matt
  • 70,063
  • 26
  • 142
  • 172
nanda
  • 23,828
  • 12
  • 65
  • 90
1

Another great use of the class is decoupling - if a component only uses another to obtain a value, do not depend on the concrete implementation, but on this interface.

Anyway, there is some example code here: http://www.slideshare.net/tfnico/google-guava

thSoft
  • 19,314
  • 5
  • 82
  • 97