简化Java中的代码

时间:2012-06-01 03:29:12

标签: java simplify

我有一个Java方法,它必须多次执行一段代码:

            // Obtengo los 7 bits de direccion
            clockStateIndex = dataStateIndex;
            for(int bit = 7; bit > 0; --bit){           
                clockStateIndex = clockSource.nextClearBit(clockStateIndex);                    // Siguiente bit en SCL
                clockStateIndex = clockSource.nextSetBit(clockStateIndex);
                testIndex = (clockSource.nextClearBit(clockStateIndex) - clockStateIndex)/2;    // Busco la mitad del bit clock
                bitSet(direccion, data.get(testIndex), bit);                // Compruebo SDA en la mitad del bit SCL
            }
            // Obttengo el bit R/W
            clockStateIndex = clockSource.nextClearBit(clockStateIndex);    // Siguiente bit en SCL
            clockStateIndex = clockSource.nextSetBit(clockStateIndex);
            testIndex = (clockSource.nextClearBit(clockStateIndex) - clockStateIndex)/2;    // Busco la mitad del bit clock
            rwBit = data.get(testIndex);        // Obtengo el estado del bit R/W en el medio del bit SCL

            // Obtengo el bit de ACK
            clockStateIndex = clockSource.nextClearBit(clockStateIndex);    // Siguiente bit en SCL
            clockStateIndex = clockSource.nextSetBit(clockStateIndex);
            testIndex = (clockSource.nextClearBit(clockStateIndex) - clockStateIndex)/2;    // Busco la mitad del bit clock
            ackBit = data.get(testIndex);       // Obtengo el estado del bit R/W en el medio del bit SCL

            // Si tengo un ACK obtengo los 8 bits de dato
            if(ackBit == false){
                for(int bit = 8; bit > 0; --bit){           
                    clockStateIndex = clockSource.nextClearBit(clockStateIndex);    
                    clockStateIndex = clockSource.nextSetBit(clockStateIndex);
                    testIndex = (clockSource.nextClearBit(clockStateIndex) - clockStateIndex)/2;    
                    bitSet(i2cData, data.get(testIndex), bit);      
                }
            }

正如您所见:

clockStateIndex = clockSource.nextClearBit(clockStateIndex);    
clockStateIndex = clockSource.nextSetBit(clockStateIndex);
testIndex = (clockSource.nextClearBit(clockStateIndex) - clockStateIndex)/2;    

重复很多次。我无法创建#defines来简化代码,例如nextBit();替换该代码。有没有其他方法来简化它? 如果创建一个方法,我将不得不传递两个BitSet,其大小约为200个元素,并返回2个int元素(我不知道在Java中做的很热,因为我没有C中的指针)

希望你能帮助我:)。

2 个答案:

答案 0 :(得分:3)

我将它包装在一个函数中:

// Instance based (inside classes)
{SCOPE MODIFIER} void ClockWork()
{
    clockStateIndex = clockSource.nextClearBit(clockStateIndex);    
    clockStateIndex = clockSource.nextSetBit(clockStateIndex);
    testIndex = (clockSource.nextClearBit(clockStateIndex) - clockStateIndex) / 2;  
}

对于基于静态的方法,它取决于您实现代码的位置。

答案 1 :(得分:-1)

最简单的解决方案是为此操作创建一个对象:

class ClockState {
    private int clearBit;
    private int setBit;
    private int testIndex;

    public ClockState( int clearBit, int setBit, int testIndex ) {
        this.clearBit = clearBit;
        this.setBit = setBit;
        this.testIndex = testIndex;
    }

    public int getClearBit() { return this.clearBit; }
    public int getSetBit() { return this.setBit; }
    public int getTestIndex { return this.textIndex; }

    public static ClockState create( ClockSource clockSource, int clockStateIndex ) {
      int clearBit = clockSource.nextClearBit(clockStateIndex);    
      int setBit = clockSource.nextSetBit(clearBit);
      int testIndex = (clockSource.nextClearBit(setBit) - setBit)/2; 
      return new ClockState( clearBit, setBit, testIndex );
    }

}

然后你的代码会是这样的:

        clockStateIndex = dataStateIndex;
        for(int bit = 7; bit > 0; --bit){           
            ClockState state = ClockState.create( clockSource, clockStateIndex );
            clockStateIndex = state.getSetBit();
            bitSet(direccion, data.get(state.getTestIndex()), bit);
        }

等等所有其他电话。