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 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 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.