package net.gtaun.shoebill.common;

import net.gtaun.shoebill.object.Destroyable;
import net.gtaun.util.event.EventManager;
import net.gtaun.util.event.EventManagerNode;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public abstract class AbstractShoebillContext implements Destroyable {
    protected final EventManager rootEventManager;

    private boolean isInited = false;
    protected EventManagerNode eventManagerNode;
    private Set<Destroyable> destroyables = new HashSet<>();


    public AbstractShoebillContext(EventManager rootEventManager) {
        this.rootEventManager = rootEventManager;
    }

    @Override
    protected void finalize() throws Throwable {
        destroy();
        super.finalize();
    }

    protected void addDestroyable(Destroyable destroyable) {
        destroyables.add(destroyable);
    }

    protected void removeDestroyable(Destroyable destroyable)
    {
        destroyables.remove(destroyable);
    }

    public final void init() {
        try {
            if (isInited) return;

            eventManagerNode = rootEventManager.createChildNode();
            onInit();
            isInited = true;
        } catch (Throwable e) {
            e.printStackTrace();
            destroy();
        }
    }

    @Override
    public final void destroy() {
        if (isDestroyed()) return;

        onDestroy();
        for (Destroyable destroyable : destroyables) destroyable.destroy();
        destroyables.clear();

        eventManagerNode.destroy();
        eventManagerNode = null;

        isInited = false;
    }

    @Override
    public boolean isDestroyed() {
        return isInited();
    }

    public boolean isInited()
    {
        return isInited;
    }

    protected abstract void onInit();

    protected abstract void onDestroy();
}

@@ -194,60 +194,8 @@
 et;%0A
-import java.util.LinkedList;%0Aimport java.util.List;%0A
 impo
@@ -212,16 +212,16 @@
 il.Set;%0A
+
 %0Apublic 
@@ -309,35 +309,37 @@
 al EventManager 
-roo
+paren
 tEventManager;%0A%0A
@@ -536,19 +536,21 @@
 Manager 
-roo
+paren
 tEventMa
@@ -571,19 +571,21 @@
    this.
-roo
+paren
 tEventMa
@@ -592,19 +592,21 @@
 nager = 
-roo
+paren
 tEventMa
@@ -1073,11 +1073,13 @@
 e = 
-roo
+paren
 tEve

Correct variable name