Another (less common) possibility is to use
public class DirectedHolder {
public DirectedHolder(boolean directed) {
this.directed = directed;
}
public final boolean directed;
}
and
@Inject
SimpleGraphImpl(DirectedHolder directedHolder) {
this.directed = directedHolder.directed;
this.nodes = new HashSet<ISimpleNode>();
this.edges = new HashSet<ISimpleEdge>();
}
and
bind(DirectedHolder.class).toInstance(new DirectedHolder(true));
This may be better sometimes, but the standard way is to use binding
annotations.
On 10-07-06 19:57, Colin Decker wrote:
> Use a binding annotation
> (http://code.google.com/p/google-guice/wiki/BindingAnnotations):
>
> @Inject
> public SimpleGraphImpl(@Directed boolean directed) { // could use, say,
> @Named("directed") as well
> this.directed = directed;
> this.nodes = new HashSet<ISimpleNode>();
> this.edges = new HashSet<ISimpleEdge>();
> }
>
> Then in your module use bindConstant():
>
> bindConstant().annotatedWith(Directed.class).to(false); // or
> Names.named("directed") instead of Directed.class
> bind(ISimpleGraph.class).to(SimpleGraphImpl.class);
>
>
> On Tue, Jul 6, 2010 at 1:17 PM, Jerem's <[email protected]
> <mailto:[email protected]>> wrote:
>
> Hello everybody
>
> I try to use google guice.
> If the test is good, I will propose to my team to use it.
>
> to test it I développe a simple structure of graph (graph edge and
> node).
> My problem is that a graph is directed or not :
>
> So my class graph look like that :
>
> public class SimpleGraphImpl implements ISimpleGraph {
>
> private final Boolean directed;
>
> private final Collection<ISimpleNode> nodes;
>
> private final Collection<ISimpleEdge> edges;
>
> public SimpleGraphImpl(boolean directed) {
> this.directed = directed;
> this.nodes = new HashSet<ISimpleNode>();
> this.edges = new HashSet<ISimpleEdge>();
> }
>
> ....
>
> }
>
> I would like to use guice in this classe.
>
> So I have this module :
> public class SimpleGraphModule extends AbstractModule {
>
> @Override
> protected void configure() {
>
> this.bind(ISimpleGraph.class).to(SimpleGraphImpl.class);
> .....
> }
>
> }
>
>
> but to be able to do that (if I have understood how guice work), I
> have to do that :
>
> @Inject
> public SimpleGraphImpl(boolean directed) {
> this.directed = directed;
> this.nodes = new HashSet<ISimpleNode>();
> this.edges = new HashSet<ISimpleEdge>();
> }
>
>
> bit it is not possible because boolean has no constructor with
> @inject.
>
> this exemple is important because in my team we often work in graph
> structure.
>
> do you know how can I resolve this problem?
>
> (sorry for my bas english)
>
> Jérémie
>
> --
> 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]
> <mailto:[email protected]>.
> To unsubscribe from this group, send email to
> [email protected]
> <mailto:google-guice%[email protected]>.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
> --
> 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.
--
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.