Order of Multibindings

1,792 views
Skip to first unread message

Dominik Obermaier

unread,
Jun 14, 2011, 9:52:55 AM6/14/11
to google...@googlegroups.com
Hi,

I want to use the Multibinding feature of Google Guice. I searched the docs and googled for it, but I did not find any answer to this question:

Is it possible to define an order of the elements in an injected Collection (e.g. List) ? And if, how do I achieve this?

Thanks!

Dominik Obermaier

Fred Faber

unread,
Jun 14, 2011, 10:09:11 AM6/14/11
to google...@googlegroups.com
You can't explicitly define that, because that depends on the non-guaranteed method of module traversal when creating the injector.

Other solutions include:
- sorting the collection at its injection point
- using a MapBinder and defining a sort order on the keys of the map

Fred


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Sam Berlin

unread,
Jun 14, 2011, 10:11:08 AM6/14/11
to google...@googlegroups.com
No, there isn't a way.  The main reason is that there's no defined order for how Modules will be configured, and Multibinder collates all the bindings from different modules.  If you only want to expose a List from a single Module, it's easy enough to write an @Provides method that returns the List with the contents.  If you want to join different Modules together, you lose the ability to specific a consistent ordering, so there's no point in exposing something that implies there would be a consistent ordering.

sam

On Tue, Jun 14, 2011 at 9:52 AM, Dominik Obermaier <dominik....@googlemail.com> wrote:

limpb...@gmail.com

unread,
Jun 14, 2011, 11:04:08 AM6/14/11
to google-guice
FYI, for multibindings registered in the same module, the order in the
module will equal the order in the set.

Dominik Obermaier

unread,
Jun 14, 2011, 11:11:19 AM6/14/11
to google...@googlegroups.com
Thanks everyone for the answers!

Jesse, thanks for the hint! Since the registering happens in the same module, this is a suitable solution for me!

I thinks this tip should be documented in the Wiki. (Of course) I have no rights to edit the Multibinding Wiki Page. Perhaps one of the people with edit rights could do this :)

Thanks again!

Dominik

On Jun 14, 2011, at 5:04 PM, je...@swank.ca wrote:

> FYI, for multibindings registered in the same module, the order in the
> module will equal the order in the set.
>

Reply all
Reply to author
Forward
0 new messages