diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/util/disposable/Disposable.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/disposable/Disposable.java | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/util/disposable/Disposable.java b/src/uk/ac/ox/cs/pagoda/util/disposable/Disposable.java new file mode 100644 index 0000000..b208cc3 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/util/disposable/Disposable.java | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.util.disposable; | ||
| 2 | |||
| 3 | |||
| 4 | /** | ||
| 5 | * Every public method of a subclass of this class, | ||
| 6 | * as first instruction, should check if the object has already been disposed | ||
| 7 | * and, if so, should throw a <tt>DisposedException</tt>. | ||
| 8 | */ | ||
| 9 | public abstract class Disposable { | ||
| 10 | |||
| 11 | private boolean disposed = false; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * This method must be called after the use of the object. | ||
| 15 | * <p> | ||
| 16 | * Every overriding method must call <tt>super.dispose()</tt> as first instruction. | ||
| 17 | */ | ||
| 18 | public void dispose() { | ||
| 19 | if(isDisposed()) throw new AlreadyDisposedException(); | ||
| 20 | disposed = true; | ||
| 21 | } | ||
| 22 | |||
| 23 | public final boolean isDisposed() { | ||
| 24 | return disposed; | ||
| 25 | } | ||
| 26 | |||
| 27 | private class AlreadyDisposedException extends RuntimeException { | ||
| 28 | |||
| 29 | public AlreadyDisposedException() { | ||
| 30 | super(); | ||
| 31 | } | ||
| 32 | |||
| 33 | public AlreadyDisposedException(String msg) { | ||
| 34 | super(msg); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | } | ||
