CloseableSet

A set of closeables.

Android API <19 does not support try-with-resources. This set is useful to cope with this problem by sticking pointers and other closeables into it to make it easier to close them, all at once.

When used in AsyncTask, the usual use case would be something like this:

@Override
protected Result doInBackground(Params... params)
{
    CloseableSet cs = new CloseableSet();
    try
    {
        ObjectWithPointer owp = cs.add(someMethodReturningPointer());
        ⋮
        AnotherObjectWithPointer aowp = cs.add(someMethodReturningAnotherPointer());
        ⋮
    }
    finally
    {
        cs.close();
    }
    return res;
}

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
open fun <T : Closeable?> add(@Nullable closeable: T): T
Insert a closeable to the set.
Link copied to clipboard
open fun close()
Close all stored closeables.
Link copied to clipboard
open fun remove(@Nullable closeable: Closeable): Boolean
Remove a closeable from the set.