---
source: framec/tests/java_snapshots.rs
expression: "compile_fixture(\"13_lifecycle_args\", \"java\")"
---
class LifecycleArgsFrameEvent {
    String _message;
    java.util.ArrayList<Object> _parameters;

    LifecycleArgsFrameEvent(String message) {
        this._message = message;
        this._parameters = new java.util.ArrayList<>();
    }

    LifecycleArgsFrameEvent(String message, java.util.ArrayList<Object> parameters) {
        this._message = message;
        this._parameters = parameters;
    }
}

class LifecycleArgsFrameContext {
    LifecycleArgsFrameEvent _event;
    Object _return;
    java.util.HashMap<String, Object> _data;
    boolean _transitioned = false;

    LifecycleArgsFrameContext(LifecycleArgsFrameEvent event, Object defaultReturn) {
        this._event = event;
        this._return = defaultReturn;
        this._data = new java.util.HashMap<>();
        this._transitioned = false;
    }
}

class LifecycleArgsCompartment {
    String state;
    java.util.ArrayList<Object> state_args;
    java.util.HashMap<String, Object> state_vars;
    java.util.ArrayList<Object> enter_args;
    java.util.ArrayList<Object> exit_args;
    LifecycleArgsFrameEvent forward_event;
    LifecycleArgsCompartment parent_compartment;

    LifecycleArgsCompartment(String state) {
        this.state = state;
        this.state_args = new java.util.ArrayList<>();
        this.state_vars = new java.util.HashMap<>();
        this.enter_args = new java.util.ArrayList<>();
        this.exit_args = new java.util.ArrayList<>();
        this.forward_event = null;
        this.parent_compartment = null;
    }

    LifecycleArgsCompartment copy() {
        LifecycleArgsCompartment c = new LifecycleArgsCompartment(this.state);
        c.state_args = new java.util.ArrayList<>(this.state_args);
        c.state_vars = new java.util.HashMap<>(this.state_vars);
        c.enter_args = new java.util.ArrayList<>(this.enter_args);
        c.exit_args = new java.util.ArrayList<>(this.exit_args);
        c.forward_event = this.forward_event;
        c.parent_compartment = this.parent_compartment;
        return c;
    }
}

public class LifecycleArgs {
    private java.util.ArrayList<LifecycleArgsCompartment> _state_stack;
    private LifecycleArgsCompartment __compartment;
    private LifecycleArgsCompartment __next_compartment;
    private java.util.ArrayList<LifecycleArgsFrameContext> _context_stack;
    public int sum = 0;
    public String label = "";

    public LifecycleArgs() {
        _state_stack = new java.util.ArrayList<>();
        _context_stack = new java.util.ArrayList<>();
        this.__compartment = __prepareEnter("Idle", new java.util.ArrayList<>(), new java.util.ArrayList<>());
        this.__next_compartment = null;
    }

    public static LifecycleArgs __create() {
        LifecycleArgs c = new LifecycleArgs();
        LifecycleArgsFrameEvent __e = new LifecycleArgsFrameEvent("$>", c.__compartment.enter_args);
        LifecycleArgsFrameContext __ctx = new LifecycleArgsFrameContext(__e, null);
        c._context_stack.add(__ctx);
        c.__kernel(__e);
        c._context_stack.remove(c._context_stack.size() - 1);
        return c;
    }

    private java.util.HashMap<String, java.util.ArrayList<String>> hsm_chain() {
        java.util.HashMap<String, java.util.ArrayList<String>> m = new java.util.HashMap<>();
        m.put("Idle", new java.util.ArrayList<>(java.util.Arrays.asList("Idle")));
        m.put("Active", new java.util.ArrayList<>(java.util.Arrays.asList("Active")));
        return m;
    }
    private LifecycleArgsCompartment __prepareEnter(String leaf, java.util.ArrayList<Object> state_args, java.util.ArrayList<Object> enter_args) {
        LifecycleArgsCompartment comp = null;
        for (String name : hsm_chain().get(leaf)) {
            LifecycleArgsCompartment new_comp = new LifecycleArgsCompartment(name);
            new_comp.state_args = new java.util.ArrayList<>(state_args);
            new_comp.enter_args = new java.util.ArrayList<>(enter_args);
            new_comp.parent_compartment = comp;
            comp = new_comp;
        }
        return comp;
    }

    private void __prepareExit(java.util.ArrayList<Object> exit_args) {
        LifecycleArgsCompartment comp = __compartment;
        while (comp != null) {
            comp.exit_args = new java.util.ArrayList<>(exit_args);
            comp = comp.parent_compartment;
        }
    }

    private void __kernel(LifecycleArgsFrameEvent __e) {
        // Route event to current state.
        __router(__e);
        // Drain any transitions queued by the handler.
        while (__next_compartment != null) {
            LifecycleArgsCompartment next_compartment = __next_compartment;
            __next_compartment = null;
            // Exit the current (leaf) state.
            LifecycleArgsFrameEvent exit_event = new LifecycleArgsFrameEvent("<$", __compartment.exit_args);
            __router(exit_event);
            // Switch to the new compartment.
            __compartment = next_compartment;
            // Three-branch forward-event handling.
            LifecycleArgsFrameEvent forward_event = next_compartment.forward_event;
            next_compartment.forward_event = null;
            if (forward_event == null) {
                // No forwarded event — synthesize a fresh $>.
                LifecycleArgsFrameEvent enter_event = new LifecycleArgsFrameEvent("$>", __compartment.enter_args);
                __router(enter_event);
            } else if (forward_event._message.equals("$>")) {
                // Forwarded event IS $> — dispatch directly so the
                // destination's $> handler receives the caller's payload.
                __router(forward_event);
            } else {
                // Forwarded event is not $> — initialize the destination
                // with a fresh $>, then dispatch the forward.
                LifecycleArgsFrameEvent enter_event = new LifecycleArgsFrameEvent("$>", __compartment.enter_args);
                __router(enter_event);
                __router(forward_event);
            }
            for (LifecycleArgsFrameContext ctx : _context_stack) {
                ctx._transitioned = true;
            }
        }
    }

    private void __router(LifecycleArgsFrameEvent __e) {
        if (__compartment.state.equals("Idle")) {
            _state_Idle(__e, __compartment);
        } else if (__compartment.state.equals("Active")) {
            _state_Active(__e, __compartment);
        }
    }

    private void __transition(LifecycleArgsCompartment next) {
        __next_compartment = next;
    }

    public void load(int n, String label) {
        LifecycleArgsFrameEvent __e = new LifecycleArgsFrameEvent("load", new java.util.ArrayList<>(java.util.Arrays.asList(n, label)));
        LifecycleArgsFrameContext __ctx = new LifecycleArgsFrameContext(__e, null);
        _context_stack.add(__ctx);
        try {
            __kernel(_context_stack.get(_context_stack.size() - 1)._event);
            _context_stack.remove(_context_stack.size() - 1);
        } catch (RuntimeException __frame_err) {
            _context_stack.remove(_context_stack.size() - 1);
            throw __frame_err;
        }
    }

    public int total() {
        LifecycleArgsFrameEvent __e = new LifecycleArgsFrameEvent("total", new java.util.ArrayList<>());
        LifecycleArgsFrameContext __ctx = new LifecycleArgsFrameContext(__e, null);
        _context_stack.add(__ctx);
        try {
            __kernel(_context_stack.get(_context_stack.size() - 1)._event);
            int __result = (int) _context_stack.get(_context_stack.size() - 1)._return;
            _context_stack.remove(_context_stack.size() - 1);
            return __result;
        } catch (RuntimeException __frame_err) {
            _context_stack.remove(_context_stack.size() - 1);
            throw __frame_err;
        }
    }

    public String tag() {
        LifecycleArgsFrameEvent __e = new LifecycleArgsFrameEvent("tag", new java.util.ArrayList<>());
        LifecycleArgsFrameContext __ctx = new LifecycleArgsFrameContext(__e, null);
        _context_stack.add(__ctx);
        try {
            __kernel(_context_stack.get(_context_stack.size() - 1)._event);
            String __result = (String) _context_stack.get(_context_stack.size() - 1)._return;
            _context_stack.remove(_context_stack.size() - 1);
            return __result;
        } catch (RuntimeException __frame_err) {
            _context_stack.remove(_context_stack.size() - 1);
            throw __frame_err;
        }
    }

    private void _state_Idle(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        if (__e._message.equals("load")) {
            this._s_Idle_hdl_user_load(__e, compartment);
            return;
        }
    }

    private void _state_Active(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        if (__e._message.equals("$>")) {
            this._s_Active_hdl_frame_enter(__e, compartment);
            return;
        }
        if (__e._message.equals("tag")) {
            this._s_Active_hdl_user_tag(__e, compartment);
            return;
        }
        if (__e._message.equals("total")) {
            this._s_Active_hdl_user_total(__e, compartment);
            return;
        }
    }

    private void _s_Idle_hdl_user_load(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        int n = ((Number) __e._parameters.get(0)).intValue();
        String label = (String) __e._parameters.get(1);
        LifecycleArgsCompartment __compartment = __prepareEnter("Active", new java.util.ArrayList<>(), new java.util.ArrayList<>(java.util.Arrays.asList(n, label)));
        __transition(__compartment);
        return;
    }

    private void _s_Active_hdl_frame_enter(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        int count = ((Number) compartment.enter_args.get(0)).intValue();
        String name = (String) compartment.enter_args.get(1);
        this.sum = count + 1;
        this.label = name;
    }

    private void _s_Active_hdl_user_tag(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        _context_stack.get(_context_stack.size() - 1)._return = this.label;
                        return
    }

    private void _s_Active_hdl_user_total(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        _context_stack.get(_context_stack.size() - 1)._return = this.sum;
                        return
    }
}
