| Index: src/org/adblockplus/libadblockplus/Disposer.java |
| diff --git a/src/org/adblockplus/libadblockplus/Disposer.java b/src/org/adblockplus/libadblockplus/Disposer.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..feb46e8c3290731175de3c03f77d8a8eef1e2131 |
| --- /dev/null |
| +++ b/src/org/adblockplus/libadblockplus/Disposer.java |
| @@ -0,0 +1,94 @@ |
| +/* |
| + * This file is part of Adblock Plus <http://adblockplus.org/>, |
| + * Copyright (C) 2006-2014 Eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +package org.adblockplus.libadblockplus; |
| + |
| +import java.lang.ref.ReferenceQueue; |
| +import java.lang.ref.WeakReference; |
| +import java.util.HashSet; |
| + |
| +public final class Disposer extends WeakReference<Disposable> |
| +{ |
| + final static ReferenceQueue<Disposable> referenceQueue = new ReferenceQueue<Disposable>(); |
| + final static HashSet<Disposer> disposerSet = new HashSet<Disposer>(); |
|
Felix Dahlke
2014/04/30 15:34:08
Should be "static final".
René Jeschke
2014/04/30 15:43:16
Done.
|
| + private final Disposable disposable; |
| + private volatile boolean isDisposed = false; |
|
Felix Dahlke
2014/04/30 15:34:08
Should be "disposed" according to the bean convent
René Jeschke
2014/04/30 15:43:16
Done.
|
| + |
| + static |
| + { |
| + final Thread thread = new Thread(new Cleaner()); |
| + thread.setName(Cleaner.class.getCanonicalName()); |
| + thread.setDaemon(true); |
| + thread.start(); |
| + } |
| + |
| + public Disposer(final Disposable referent, final Disposable disposable) |
|
Felix Dahlke
2014/04/30 15:34:08
How about renaming "disposable" to "reference"? Th
René Jeschke
2014/04/30 15:43:16
'referent' is the reference that get stored in the
Felix Dahlke
2014/04/30 15:50:43
Isn't disposable the object that _gets_ disposed o
|
| + { |
| + super(referent, referenceQueue); |
|
Felix Dahlke
2014/04/30 15:34:08
Shouldn't it be the referentQueue if it stores the
René Jeschke
2014/04/30 15:43:16
It stores the value from a local variable called '
|
| + this.disposable = disposable; |
| + |
| + synchronized (disposerSet) |
| + { |
| + disposerSet.add(this); |
| + } |
| + } |
| + |
| + public synchronized void dispose() |
| + { |
| + if (!this.isDisposed) |
| + { |
| + try |
| + { |
| + this.disposable.dispose(); |
| + } |
| + catch (final Throwable t) |
| + { |
| + // catch to set state to 'disposed' on all circumstances |
| + } |
| + |
| + this.isDisposed = true; |
| + synchronized (disposerSet) |
| + { |
| + disposerSet.remove(this); |
| + } |
| + } |
| + } |
| + |
| + private static final class Cleaner implements Runnable |
| + { |
| + public Cleaner() |
| + { |
| + // |
| + } |
| + |
| + @Override |
| + public void run() |
| + { |
| + for (;;) |
| + { |
| + try |
| + { |
| + ((Disposer) Disposer.referenceQueue.remove()).dispose(); |
| + } |
| + catch (final Throwable t) |
| + { |
| + // ignored |
| + } |
| + } |
| + } |
| + } |
| +} |