On Sep 14, 2010, at 4:01 AM, Alen Vrečko wrote:
> Sure I mean DRY. Imho you can drop the generics on the
> InjectableRunnable type and get them exclusively from
> InjectableRunnable#key. Fruthermore you can put a provider in the
> c'tor instead of injector and key. But you must have your reasons. ;)
The generics on InjectableRunnable were just there to try and get rid of the
nasty erasure runtime exception. However, I did remove them and tried relying
on just passing a Key sub-class around and that didn't work either. I have a
feeling that something is fishy.
If I use a Provider that just incurs even more overhead of constructing the
objects. I control everything so why not just let Guice do that work.
The injector is by far the least work if I could get the generic Keys working.
This way is only a single line of code for each new Updatable. Anything else
could be much more code than that, including a Provider for each Updatable or a
Runnable for each Updatable.
Because I couldn't get the generics working (no matter what I did), I
refactored the InjectableRunnable to be an UpdatableRunnable instead. Then I
just pass the Updatable Class into that. That gets rid of the Updater and the
Key. Here's that code:
public class UpdatableRunnable<T extends Updatable> implements Runnable {
private final Injector injector;
private final Class<T> type;
public UpdatableRunnable(Injector injector, Class<T> type) {
this.injector = injector;
this.type = type;
}
@Override
public void run() {
Updatable instance = injector.getInstance(type);
instance.update();
}
}
-bp
--
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.