JavaScript匿名函数数组到Java转换

时间:2014-03-24 06:45:21

标签: java javascript arrays function-pointers anonymous-function

在我得到任何好的反馈/帮助之前,我问了一个类似的问题。所以我正在用更多信息重写这个问题。

这是一个部分转换的Javascript函数到Java ..它几乎完成了。 我知道我不能在Java中的数组中使用匿名函数,更不用说匿名函数,只有最接近这个功能的东西才是内部类,而不是我想要的。

我知道这段代码仍然可以转换为多个函数而不是使用匿名函数数组,如if语句或switch(...)

的混合

下面的函数也只是使用递归调用来初始化匿名函数的LINECONTROL数组。

稍后在这个javscript模拟器中它会像这样使用

LCDCONTROL = (LCDisOn) ? LINECONTROL : DISPLAYOFFCONTROL;

DISPLAYOFFCONTROL是一个空的匿名函数。 如此宣布

 DISPLAYOFFCONTROL = [function() {}];

您永远不会在LINECONTROL之外调用initalizeLCDController数组,只能调用LCDCONTROL对象。

喜欢这样

//LCDCONTROL[actualScanLine]();

我只是需要一些建议如何转换为下面的功能可能只是一个关于它的先声。

以下是JavaScript中部分翻译的功能 - >爪哇

public void initializeLCDController() {
    long a = 0;
    while (a < 154) {
        if (a < 143) {
            LINECONTROL[a] = function () {
                if (LCDTicks < 80) {
                    scanLineMode2();
                } else if (LCDTicks < 252) {
                    scanLineMode3();
                } else if (LCDTicks < 456) {
                    scanLineMode0();
                } else {
                    LCDTicks -= 456;
                    if (STATTracker != 3) {
                        if (STATTracker != 2) {
                            if (STATTracker == 0 && mode2TriggerSTAT) {
                                interruptsRequested |= 2;
                            }
                            incrementScanLineQueue();
                        }
                        if (hdmaRunning) {
                            executeHDMA();
                        }
                        if (mode0TriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    }
                    actualScanLine = ++memory[65348];
                    if (actualScanLine == memory[65349]) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    checkIRQMatching();
                    STATTracker = 0;
                    modeSTAT = 2;
                    LINECONTROL[actualScanLine]();
                }
            }
        } else if (a == 143) {
            LINECONTROL[143] = function () {
                if (LCDTicks < 80) {
                    scanLineMode2();
                } else if (LCDTicks < 252) {
                    scanLineMode3();
                } else if (LCDTicks < 456) {
                    scanLineMode0();
                } else {
                    LCDTicks -= 456;
                    if (STATTracker != 3) {
                        if (STATTracker != 2) {
                            if (STATTracker == 0 && mode2TriggerSTAT) {
                                interruptsRequested |= 2;
                            }
                            incrementScanLineQueue();
                        }
                        if (hdmaRunning) {
                            executeHDMA();
                        }
                        if (mode0TriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    }
                    actualScanLine = memory[65348] = 144;
                    if (memory[65349] == 144) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    STATTracker = 0;
                    modeSTAT = 1;
                    interruptsRequested |= (mode1TriggerSTAT) ? 3 : 1;
                    checkIRQMatching();
                    if (drewBlank == 0) {
                        if (totalLinesPassed < 144 || (totalLinesPassed == 144 && midScanlineOffset > -1)) {
                            graphicsJITVBlank();
                            prepareFrame();
                        }
                    } else {
                        --drewBlank;
                    }
                    LINECONTROL[144]();
                }
            }
        } else if (a < 153) {
            LINECONTROL[a] = function () {
                if (LCDTicks >= 456) {
                    LCDTicks -= 456;
                    actualScanLine = ++memory[65348];
                    if (actualScanLine == memory[65349]) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                            checkIRQMatching();
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    LINECONTROL[actualScanLine]();
                }
            }
        } else {
            LINECONTROL[153] = function () {
                if (LCDTicks >= 8) {
                    if (STATTracker != 4 && memory[65348] == 153) {
                        memory[65348] = 0;
                        if (memory[65349] == 0) {
                            memory[65345] |= 4;
                            if (LYCMatchTriggerSTAT) {
                                interruptsRequested |= 2;
                                checkIRQMatching();
                            }
                        } else {
                            memory[65345] &= 123;
                        }
                        STATTracker = 4;
                    }
                    if (LCDTicks >= 456) {
                        LCDTicks -= 456;
                        STATTracker = actualScanLine = 0;
                        LINECONTROL[0]();
                    }
                }
            }
        }
        ++a;
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个聪明的枚举方法。您可以通过执行ACTIONS[x].act(this)来使用它。它本质上是一个类型安全的跳转表。

修改:已添加getAction。要使用它,请执行getAction(a).act(this),但此时,您最好不要丢失枚举并直接从重命名的getAction()发送。你从枚举中获得的唯一好处是每个枚举只有一个实例(只有非静态内部类的优点)和跳转表。

class Foo {

    protected enum Action {
        FUNCTION_0 {
            public void act(Foo foo) { ... }
        },

        FUNCTION_1 {
            public void act(Foo foo) { ... }
        },

        FUNCTION_2 {
            public void act(Foo foo) { ... }
        },

        FUNCTION_3 {
            public void act(Foo foo) { ... }
        },

        ;

        public abstract void act(Foo foo);
    }

    protected static final Action[] ACTIONS = new Action[154];
    static {
        Arrays.fill(ACTIONS, 0, 143, Action.FUNCTION_0);
        Arrays.fill(ACTIONS, 143, 144, Action.FUNCTION_1);
        Arrays.fill(ACTIONS, 144, 153, Action.FUNCTION_2);
        Arrays.fill(ACTIONS, 153, 154, Action.FUNCTION_3);
    }

        protected Action getAction(int a) {
            if (a < 0) { throw new IllegalArgumentError(); }
            else if (a < 143) { return Action.FUNCTION_0; }
            else if (a < 144) { return Action.FUNCTION_1; }
            else if (a < 153) { return Action.FUNCTION_2; }
            else if (a < 154) { return Action.FUNCTION_3; }
            else { throw new IllegalArgumentError(); }
        }
}