Discussion:
List of named components as arg to constructor param
joe panico
2009-09-11 10:38:44 UTC
Permalink
Hello,

I'm using PicoContainer 2.8.3.

I would like Pico to explicitly wire specific named (keyed) components
into a constructor arg for a List type param. For example:

class Element{
private final String config;
public Element(String config){
this.config = config
}
}

class Composition{
private final List<Element> elements;
public Composition(List<Element> elements){
this.elements = elements
}
}

pico.addComponent("elementA", Element.class, new ConstantParameter("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter("configC"))

pico.addComponent("compositionAB", Composition.class, ?tell it to use
keys "elementA" + "elementB"?)
pico.addComponent("compositionBC", Composition.class, ?tell it to use
keys "elementB" + "elementC"?)

How do I achieve those last two lines of pseudo code? I don't care if
the solution is not typesafe with respect to generics.

thanks for any pointers.

Joe

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Paul Hammant
2009-09-12 14:57:44 UTC
Permalink
pico = new DefaultPicoContainer(new NullComponentMonitor() {

public Object noComponentFound(MutablePicoContainer container,
Object componentKey);
String[] keys = componentKey.split(",");
List<Element>() elements = new ArrayList<Element>();
for (String key in keys) {
elements.add(container.get(key));
}
return new Composition(elements);
}

});


pico.addComponent("elementA", Element.class, new ConstantParameter
("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter
("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter
("configC"))

abComposition = pico.getComponent("elementA,elementB");
bcComposition = pico.getComponent("elementB,elementC");

-----

No need to add Composition to the container for this specialized case,
it'll be cached anyway for the key in question, unless you don't do
caching in the PicoContainer setup. You obviously have to be
comfortable with participating in the List creation.

You could aim at some smaller language in the missing key definition ,
of you were prepared to do more string processing in the
noComponentFound(..).

abComposition = pico.getComponent("A,B");
abComposition = pico.getComponent("AB");
abComposition = pico.getComponent("compositionAB");
abComposition = pico.getComponent("elems(A,B)");

Regards,

- Paul
Post by joe panico
Hello,
I'm using PicoContainer 2.8.3.
I would like Pico to explicitly wire specific named (keyed) components
class Element{
private final String config;
public Element(String config){
this.config = config
}
}
class Composition{
private final List<Element> elements;
public Composition(List<Element> elements){
this.elements = elements
}
}
pico.addComponent("elementA", Element.class, new ConstantParameter
("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter
("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter
("configC"))
pico.addComponent("compositionAB", Composition.class, ?tell it to use
keys "elementA" + "elementB"?)
pico.addComponent("compositionBC", Composition.class, ?tell it to use
keys "elementB" + "elementC"?)
How do I achieve those last two lines of pseudo code? I don't care if
the solution is not typesafe with respect to generics.
thanks for any pointers.
Joe
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Paul Hammant
2009-09-15 12:06:29 UTC
Permalink
Actually, a bug in PicoContainer itself would prevent that from
working. There's a SNAPSHOT that's been uploaded that will work
better ( picocontainer 2.9-20090915.120308-1 )

Watch for some new architecture in the next release that will allow
for regex to be used in component gathering for situations like this.

Regards,

- Paul
Post by Paul Hammant
pico = new DefaultPicoContainer(new NullComponentMonitor() {
public Object noComponentFound(MutablePicoContainer container,
Object componentKey);
String[] keys = componentKey.split(",");
List<Element>() elements = new ArrayList<Element>();
for (String key in keys) {
elements.add(container.get(key));
}
return new Composition(elements);
}
});
pico.addComponent("elementA", Element.class, new ConstantParameter
("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter
("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter
("configC"))
abComposition = pico.getComponent("elementA,elementB");
bcComposition = pico.getComponent("elementB,elementC");
-----
No need to add Composition to the container for this specialized
case, it'll be cached anyway for the key in question, unless you
don't do caching in the PicoContainer setup. You obviously have to
be comfortable with participating in the List creation.
You could aim at some smaller language in the missing key
definition , of you were prepared to do more string processing in
the noComponentFound(..).
abComposition = pico.getComponent("A,B");
abComposition = pico.getComponent("AB");
abComposition = pico.getComponent("compositionAB");
abComposition = pico.getComponent("elems(A,B)");
Regards,
- Paul
Post by joe panico
Hello,
I'm using PicoContainer 2.8.3.
I would like Pico to explicitly wire specific named (keyed)
components
class Element{
private final String config;
public Element(String config){
this.config = config
}
}
class Composition{
private final List<Element> elements;
public Composition(List<Element> elements){
this.elements = elements
}
}
pico.addComponent("elementA", Element.class, new ConstantParameter
("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter
("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter
("configC"))
pico.addComponent("compositionAB", Composition.class, ?tell it to use
keys "elementA" + "elementB"?)
pico.addComponent("compositionBC", Composition.class, ?tell it to use
keys "elementB" + "elementC"?)
How do I achieve those last two lines of pseudo code? I don't care if
the solution is not typesafe with respect to generics.
thanks for any pointers.
Joe
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...