summaryrefslogtreecommitdiff
path: root/WPIJavaCV/src/edu/wpi/first/wpijavacv/WPIMemoryPool.java
blob: 1d9a3b4dc73c2ce50f8da12bd98fdaf05362a442 (plain)
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
31
32
33
34
package edu.wpi.first.wpijavacv;

/**
 * This class allows a bunch of disposable items to be put into a pool which will do something if all of them are disposed.
 * Subclasses should override the dispose method to react to when everything in the pool is disposed.
 * @author Joe Grinstead
 */
public abstract class WPIMemoryPool extends WPIDisposable {

    /** The number of elements remaining in the pool */
    private int remaining;

    /**
     * Adds the given disposable item to the memory pool
     * @param disposable the item
     */
    public synchronized void addToPool(WPIDisposable disposable) {
        validateDisposed();
        disposable.setPool(this);
        remaining++;
    }

    /**
     * Removes the given disposable item from the memory pool
     * @param disposable
     */
    public synchronized void removeFromPool(WPIDisposable disposable) {
        validateDisposed();
        disposable.setPool(null);
        if (--remaining <= 0) {
            dispose();
        }
    }
}