Good question, seriously I didn't tried the other way around and you made me
curious :D
With the class being generic, you can specify at bind time what you need:
@Inject
public YourClass(YourProvider<TypeYouNeed> yourProvider) {
}
And since it's really easy to test, I usually avoid the other pattern. The
problem here is that you would need to inject a factory for every type you
need and I see why you don't want that to happen.
I will probably replace the new I used for a provider like you, I'll tell
you what I did then :D
Cheers,
On Sat, Nov 20, 2010 at 1:29 PM, Bill de hÓra <[email protected]> wrote:
>
> I think I have a slightly different problem. This,
>
> public interface PrintableFactory<T> { Printable<T> create(List<T>
> items, Map<String, Column<T, ?>> columns); }
>
> needs to be this ,
> public interface PrintableFactory {
> <T> Printable<T> create(List<T> items, Map<String, Column<T, ?>>
> columns);
> }
>
> no generic on the class. Does your approach still work?
>
> Bill
>
>
>
> On Sat, 2010-11-20 at 13:22 -0500, Christian Goudreau wrote:
>
> You know, my printable factory actually creates a CellTable !
>
>
>
> You might be interested in this:
>
> http://pastebin.com/F62Wdwn2
>
>
>
> Cheers,
>
> On Sat, Nov 20, 2010 at 1:13 PM, Bill de hÓra <[email protected]> wrote:
>
> On Sat, 2010-11-20 at 12:42 -0500, Christian Goudreau wrote:
>
> Am I right in saying Guice doesn't supporting binding generic methods
> currently even with TypeLiteral/FactoryProvider tricks?
>
> That does work Using Guice 3.0 Assisted injection and TypeLiteral:
>
> install(new GinFactoryModuleBuilder().implement(new
> TypeLiteral<Printable<Country>>() { }, new
> TypeLiteral<PrintableImpl<Country>>() { }).build(new
> TypeLiteral<PrintableFactory<Country>>() {
> }));
>
>
> Well, the caveat is that I have to bind every permutation.
>
>
>
> Right. I don't have concrete types for classes at compile time to give
> to a TypeLiteral, I have method generics like these -
>
> Column<String, String> c1 = Builder.newColumn("name1", "value");
> Column<byte[], String> c2 = Builder.newColumn(key, "value");
> Column<TimeUUID, byte[]> c2 = Builder.newColumn(tkey, value);
>
> etc
>
>
>
> I'm sure something similar can be done with normal provider. Also given
> the code you gave, I think assisted injection would be a cleaner solution.
>
>
>
> I'd love to see something working.
>
> Bill
>
>
>
>
>
>
> Cheers,
>
> On Sat, Nov 20, 2010 at 10:36 AM, Bill de hÓra <[email protected]> wrote:
>
> Thanks Fred, the ProviderFactory's 'free' implementation had lead me down
> the wrong path. - normal Providers were exactly what I needed. I ended up
> with this
>
> public class Module extends AbstractPropertiesModule
> {
> @Override
> protected void configureModule() {
> bind(ColumnFactory.class).toProvider(ColumnFactoryProvider.class);
> }
>
> static class ColumnFactoryProvider implements Provider<ColumnFactory>
>
> {
> @Override
> public ColumnFactory get() {
> return new ColumnFactory()
> {
> @Override
>
> public <N, V> Column<N, V> create(N name, V value) {
> return new ColumnImpl<N, V>(name, value);
> }
>
> @Override
> public <N, V> Column<N, V> create(N name) {
> return new ColumnImpl<N, V>(name);
> }
>
> @Override
> public <N, V> Column<N, V> create(N name, V value, Long
> timestamp) {
> return new ColumnImpl<N, V>(name, value, timestamp);
>
> }
> };
> }
> }
> }
>
> public class Builder
> {
> private static Injector theInjector = Guice.createInjector(new
> datagrid.api.module.Module());
> private static ColumnFactory theColumnFactory =
> theInjector.getInstance(ColumnFactory.class);
>
>
> public static <K, V> Column<K, V> newColumn(K name, V value) {
> return theColumnFactory.create(name, value);
> }
>
> public static <K, V> Column<K, V> newColumn(K name) {
> return theColumnFactory.create(name);
> }
>
> public static <K, V> Column<K, V> newColumn(K name, V value, Long
> timestamp) {
> return theColumnFactory.create(name, value, timestamp);
> }
>
> }
>
> also a nice approach for legacy factory/builder code.
>
>
> Bill
>
>
>
> On Sat, 2010-11-20 at 09:21 -0500, Fred Faber wrote:
>
> I envision your interface as:
>
>
> interface ColumnFactory {
> <K, V> Column<K, V> newColumn(
> K name, V value, long timestampMillis);
> }
>
> Then you could adapt your current implementation:
>
>
> bind(ColumnFactory.class)
> .toProvider(new Provider<ColumnFactory>(){
> @Override public ColumnFactory get() {
> return new ColumnFactory() {
> @Override public <K, V> Column<K, V>
> newColumn(
> K name, V value, long timestampMillis) {
> return Builder.newColumn(
> name, value, timestampMillis);
> }
> );
> }
> }});
>
>
> Nothing to worry about here with generic methods.
>
>
> -Fred
>
>
>
> On Sat, Nov 20, 2010 at 8:58 AM, Bill de hÓra <[email protected]> wrote:
>
>
> I have a Builder object that generates objects with generic methods like
> this -
>
> public class Builder
> {
> public static <K, V> Column<K, V> newColumn(K name, V value, long
> tstamp) {
> return new ColumnImpl<K, V>(name, value, tstamp);
> }
> }
>
> ie, it's akin to Guava's Maps/Lists classes. I'd like the impl classes to
> be provided by Guice, eg
>
> public class Builder
> {
> private static Injector theInjector = Guice.createInjector(new
> datagrid.api.module.Module());
> private static ColumnFactory theColumnFactory =
> theInjector.getInstance(ColumnFactory.class);
>
> public static <K, V> Column<K, V> newColumn(K name, V value, long
> tstamp) {
> return theColumnFactory.create(name, value, long);
> }
> }
>
> Am I right in saying Guice doesn't supporting binding generic methods
> currently even with TypeLiteral/FactoryProvider tricks?
>
> Bill
> --
> 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%[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]<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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
>
>
> --
> Christian Goudreau
> www.arcbees.com
>
>
>
> --
> 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%[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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
>
>
> --
> Christian Goudreau
>
> www.arcbees.com
>
>
>
>
> --
> 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%[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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
--
Christian Goudreau
www.arcbees.com
--
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.