TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

ChainedSupplier #53

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The idea behind a ChainedSupplier is that the ChainedSupplier sits on top
of a list of suppliers. When get() is called it moves through the list
calling get() on each Supplier object in the list passing the supplied
object through a predicate. If the predicate returns true it returns that
object. If the entire list of suppliers fails to pass the predicate
ChainedSupplier then returns the supplied object from the default supplier.

Usage would look like this:
List<Supplier<String>> supplierList = new ArrayList<Supplier<String>>();
supplierList.add(Suppliers.ofInstance("A"));
supplierList.add(Suppliers.ofInstance("B"));
supplierList.add(Suppliers.ofInstance("C"));

Supplier<String> supplier = Suppliers.chained(Predicates.isEqualTo("B"),
  supplierList,Suppliers.ofInstance("Q"));

supplier.get() in this case would return "B" but if we change the predicate

Supplier<String> supplier = Suppliers.chained(Predicates.isEqualTo("Z"),
  supplierList,Suppliers.ofInstance("Q"));

supplier.get() would return the default supplier's get() ("Q") because no
supplier would be able to pass the predicate.

Original issue reported on code.google.com by TrashH...@gmail.com on 28 Mar 2008 at 12:11

Attachments:

GoogleCodeExporter commented 9 years ago
What's the use case?

Original comment by limpbizkit on 28 Mar 2008 at 1:23

GoogleCodeExporter commented 9 years ago
This class would allow for a supplier that would be a composition of smaller 
more
specific suppliers. Lets say we have

ChipSupplier
PretzelSupplier
CheesePoofSupplier
CandySupplier

Then we could create a SnackSupplier with the list order being PretzelSupplier,
CheesePoofSupplier, ChipSupplier and the default supplier being CandySupplier 
because
there is always candy laying around. The predicate we would pass in checks to 
see if
there is enough of that snack available to eat.

The list of suppliers provides a preference order. Try to find pretzels, if 
there are
no pretzels try and find cheese poofs if there are no cheese poofs try and find 
chips.

The SnackSupplier can be written without a ChainSupplier by placing all the 
logic
from the different suppliers into a large if else if else if else etc block. 
But if
you are using a declarative programming framework with google collections such 
as
Spring then creating the SnackSupplier via a ChainSupplier wouldn't even 
require a
single line of code.

<bean id="snackSupplier" class="com.google.common.base.Suppliers" 
factory-method="chain">
  <constructor-arg>
    <ref bean="availablePredicate" />
  </constructor-arg>
  <constructor-arg>
    <list>
      <ref bean="pretzelSupplier" />
      <ref bean="cheesePoofSupplier" />
      <ref bean="chipSupplier" /> 
    </list>
  </constructor-arg>
  <constructor-arg>
    <ref bean="candySupplier" />
  </constructor-arg>
</bean>

Original comment by TrashH...@gmail.com on 28 Mar 2008 at 6:14

GoogleCodeExporter commented 9 years ago
Do you have a more concrete example? I haven't seen this come up before.

For what it's worth, I'd take the single line of code over 15 lines of XML.

Original comment by limpbizkit on 28 Mar 2008 at 6:30

GoogleCodeExporter commented 9 years ago
Any distributed business object would use a similar concept. If server A fails 
to
give me a result go to server B and so on and so on. 

Another example is when you have a base interface with multiple implementing 
classes
and you wish to implement a supplier to get a object out of that base interface.

Original comment by TrashH...@gmail.com on 28 Mar 2008 at 6:42

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:57

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 6:02

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09