Spot on, Fred. I grok. Thank you! -Brian
On Mar 12, 12:57 pm, Fred Faber <[email protected]> wrote: > The general scenario is when you'd want to inject a predictably sequenced > collection. Multibinder is able to inject Set<T>, but the iteration order > over its elements is undefined. In some cases, such as in the chain of > responsibility pattern, you want to contribute elements to a collection, and > you want the sequence of your contributions to be equal to the iteration > order. > > Multibinder is useful tool for many reasons, the main two of which I see as: > (1) it has a rich binding language ala LinkedBindingBuilder, which will > accept contributions of classes, providers, and instances > (2) it allows for contributions to be made across different modules > > In a case in which we need sequencing of elements in a collection, at least > one of the following will be true: > (a) we will be using comparable items that can be sorted post-injection time > (b) we will be using globally known keys, which themselves are comparable, > and can be associated to each element > (c) we will be making all contributions in the same module, and as such the > sequence in which contributions are made becomes the comparable element > > The example in case within this email thread is (c). Neither MapBinder nor > Multibinder (aka SetBinder) guarantees that the injected collection has an > iteration order mirroring that of contribution order. Hence we need to > finagle ourselves into either case (a) or case (b) if we want to preserve > the contribution order of elements. > > MapBinder<Integer, TemplateFinder> mapBinder = MapBinder.newMapBinder(...); > mapBinder.addBinding(1).to(FirstTemplateFinder.class); > mapBinder.addBinding(2).to(SecondTemplateFinder.class); > ... > mapBinder.addBinding(n).to(NthTemplateFinder.class); > > Then using the Provider<List<TemplateFinder>> sketched in the previous > reply, we can inject an ordered List of TemplateFinders. > > I'm not sure if this follow-up will hurt or help the headache. > > -Fred > > > > On Fri, Mar 12, 2010 at 12:36 PM, bklough <[email protected]> wrote: > > Fred, granted its Friday, but this just cramped my frontal lobe. If > > you get a shot, could you elaborate on what scenario I might use > > something like this in? Thx and Happy Friday! > > > On Mar 12, 12:09 pm, Fred Faber <[email protected]> wrote: > > > Incidentally, and as a general note to similar problems, if you need an > > > ordering and you know you're binding all contributions from the same > > module > > > then you can use a MapBinder with an incrementing key value, then use a > > > Provider as such: > > > > bind(new TypeLiteral<List<TemplateFinder>>(){}) > > > .toProvider(new Provider<List<TemplateFinder>(){ > > > > @Inject Map<Integer, TemplateFinder> templates; > > > > �...@override > > > public List<TemplateFinder> get() { > > > // sort the keyset of templates > > > // iterate over the sorted keyset, and add > > templates.get(key) > > > to the list > > > } > > > > } > > > On Fri, Mar 12, 2010 at 11:55 AM, Fred Faber <[email protected]> wrote: > > > > There are a number of ways to do this, but if you don't need ordering, > > then > > > > can you use a Multibinder? > > > > >http://code.google.com/p/google-guice/wiki/Multibindings > > > > > You would simply inject ArtifactLocator with Set<TemplateFinder>, and > > add > > > > each TemplateFinder to the Multibinder in your module. > > > > > -Fred > > > > > On Fri, Mar 12, 2010 at 11:00 AM, Ilya Sterin <[email protected]> > > wrote: > > > > >> I have a guice project with multiple guice modules when are then used > > > >> to create an injector. Nothing unusual. > > > > >> I have a chain of responsibility implementation where there is a > > > >> object, which needs to accumulate all command objects to initialize. > > > > >> So there is a class Locator, which has a list of Finder instances. > > > >> Some Finder instances have dependencies which I want wired by guice. > > > > >> I want to perform this type of initialization in the module (I would > > > >> think this is the best place for it). The problem is that doing the > > > >> initialization yourself and then using toInstance() doesn't inject any > > > >> of the Finder setter dependencies. Here is an example... > > > > >> ArtifactLocator locator = new ArtifactLocator(); > > > >> locator.addFinder(new RaceTemplateFinder()); > > > >> locator.addFinder(new EventTemplateFinder()); > > > >> locator.addFinder(new ClasspathTemplateFinder()); > > > > >> bind(ArtifactLocator.class).toInstance(locator); > > > > >> The issue here is that Race and Event template finders have a > > > >> dependency I want wired by guice. I know I can possibly bypass this > > > >> with Providers, but it just seems rather ugly, to have to write a > > > >> provider for every Finder we might implement. Is there a better way > > > >> of doing this, or am I missing something? > > > > >> Thanks. > > > > >> Ilya > > > > >> -- > > > >> You received this message because you are subscribed to the Google > > Groups > > > >> "google-guice" group. > > > >> To post to this group, send email to [email protected]. > > > >> To unsubscribe from this group, send email to > > > >> [email protected]<google-guice%2bunsubscr...@googlegroups.com> > > <google-guice%2bunsubscr...@googlegroups.com> > > > >> . > > > >> For more options, visit this group at > > > >>http://groups.google.com/group/google-guice?hl=en.-Hide quoted text - > > > > - Show quoted text - > > > -- > > You received this message because you are subscribed to the Google Groups > > "google-guice" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]<google-guice%2bunsubscr...@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/google-guice?hl=en.- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
