<commit_before><commit_msg>A convenience class for tracking destroyables of all flavors.<commit_after>//
// Triple Play - utilities for use in PlayN-based games
// Copyright (c) 2011-2013, Three Rings Design, Inc. - All rights reserved.
// http://github.com/threerings/tripleplay/blob/master/LICENSE

package tripleplay.util;

import java.util.ArrayList;
import java.util.List;

import react.MultiFailureException;

import playn.core.Layer;

/**
 * Maintains a bag of {@link Destroyable} instances which can all be cleared at once. This is
 * useful for tracking connections and other managed resources on a screen and then clearing them
 * all at once when the screen is hidden or removed.
 */
public class DestroyableBag {

    /** Adds {@code dable} to this bag. No check is made to see if {@code dable} is already in the
     * bag. If you add something twice, it gets destroyed twice, so don't do that. */
    public DestroyableBag add (Destroyable dable) {
        _dables.add(dable);
        return this;
    }

    /** Adds {@code layer} to this bag. {@link Layer#destroy} will be called on destruction. */
    public DestroyableBag add (final Layer layer) {
        return add(new Destroyable() {
            public void destroy () {
                layer.destroy();
            }
        });
    }

    /** Adds {@code conn} to this bag. {@code disconnect} will be called on destruction. */
    public DestroyableBag add (final react.Connection conn) {
        return add(new Destroyable() {
            public void destroy () {
                conn.disconnect();
            }
        });
    }

    /** Adds {@code conn} to this bag. {@code disconnect} will be called on destruction. */
    public DestroyableBag add (final playn.core.Connection conn) {
        return add(new Destroyable() {
            public void destroy () {
                conn.disconnect();
            }
        });
    }

    /** Destroys all destroyables in this bag and clears its contents. */
    public void clear () {
        MultiFailureException mfe = null;
        for (Destroyable dable : _dables) {
            try {
                dable.destroy();
            } catch (Exception e) {
                if (mfe == null) mfe = new MultiFailureException();
                mfe.addFailure(e);
            }
        }
        _dables.clear();
        if (mfe != null) mfe.trigger();
    }

    protected List<Destroyable> _dables = new ArrayList<Destroyable>();
}
{'fraction_non_alphanumeric': 0.10097719869706841, 'fraction_numerical': 0.003257328990228013, 'mean_word_length': 2.2244094488188977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2137718', 'n_tokens_mistral': 682, 'n_tokens_neox': 653, 'n_words': 243}