aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/uk/ac/ox/cs/pagoda/util/disposable/Disposable.java
blob: 4015b66bb7f80b30b7eae379844349100cd03afe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package uk.ac.ox.cs.pagoda.util.disposable;


/**
 * Every public method of a subclass of this class,
 * as first instruction, should check if the object has already been disposed
 * and, if so, should throw a <tt>DisposedException</tt>.
 */
public abstract class Disposable {

    private boolean disposed = false;

    /**
     * This method must be called after the use of the object.
     * <p>
     * Every overriding method must call <tt>super.dispose()</tt> as first instruction.
     */
    public void dispose() {
        if(isDisposed()) throw new AlreadyDisposedException();
        disposed = true;
    }

    public final boolean isDisposed() {
        return disposed;
    }

    private class AlreadyDisposedException extends RuntimeException {
    }

}