Use more precise generics in the guts of the implementation.
This lets you write code like: taskMap.keySet().forEach(r ->
r.get().owner().close());
@@ -74,7 +74,7 @@ class EvictionTimer {
@Override
public void run() {
synchronized (EvictionTimer.class) {
- for (final Entry<WeakReference<Runnable>, WeakRunner> entry : taskMap.entrySet()) {
+ for (final Entry<WeakReference<BaseGenericObjectPool<?>.Evictor>, WeakRunner<BaseGenericObjectPool<?>.Evictor>> entry : taskMap.entrySet()) {
if (entry.getKey().get() == null) {
executor.remove(entry.getValue());
taskMap.remove(entry.getKey());
@@ -92,17 +92,18 @@ class EvictionTimer {
/**
* Runnable that runs the referent of a weak reference. When the referent is no
* no longer reachable, run is no-op.
+ * @param <R> The kind of Runnable.
*/
- private static class WeakRunner implements Runnable {
+ private static class WeakRunner<R extends Runnable> implements Runnable {
- private final WeakReference<Runnable> ref;
+ private final WeakReference<R> ref;
/**
* Constructs a new instance to track the given reference.
*
* @param ref the reference to track.
*/
- private WeakRunner(final WeakReference<Runnable> ref) {
+ private WeakRunner(final WeakReference<R> ref) {
this.ref = ref;
}
@@ -123,7 +124,9 @@ class EvictionTimer {
private static ScheduledThreadPoolExecutor executor; //@GuardedBy("EvictionTimer.class")
/** Keys are weak references to tasks, values are runners managed by executor. */
- private static final HashMap<WeakReference<Runnable>, WeakRunner> taskMap = new HashMap<>(); // @GuardedBy("EvictionTimer.class")
+ private static final HashMap<
+ WeakReference<BaseGenericObjectPool<?>.Evictor>,
+ WeakRunner<BaseGenericObjectPool<?>.Evictor>> taskMap = new HashMap<>(); // @GuardedBy("EvictionTimer.class")
/**
* Removes the specified eviction task from the timer.
@@ -176,7 +179,7 @@ class EvictionTimer {
* @param evictor Eviction task to remove
*/
private static void remove(final BaseGenericObjectPool<?>.Evictor evictor) {
- for (final Entry<WeakReference<Runnable>, WeakRunner> entry : taskMap.entrySet()) {
+ for (final Entry<WeakReference<BaseGenericObjectPool<?>.Evictor>, WeakRunner<BaseGenericObjectPool<?>.Evictor>> entry : taskMap.entrySet()) {
if (entry.getKey().get() == evictor) {
executor.remove(entry.getValue());
taskMap.remove(entry.getKey());
@@ -203,8 +206,8 @@ class EvictionTimer {
executor.setRemoveOnCancelPolicy(true);
executor.scheduleAtFixedRate(new Reaper(), delay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS);
}
- final WeakReference<Runnable> ref = new WeakReference<>(task);
- final WeakRunner runner = new WeakRunner(ref);
+ final WeakReference<BaseGenericObjectPool<?>.Evictor> ref = new WeakReference<>(task);
+ final WeakRunner<BaseGenericObjectPool<?>.Evictor> runner = new WeakRunner<>(ref);
final ScheduledFuture<?> scheduledFuture = executor.scheduleWithFixedDelay(runner, delay.toMillis(),
period.toMillis(), TimeUnit.MILLISECONDS);
task.setScheduledFuture(scheduledFuture);
