Insert additional code in Java for loops

I have a batch of sprites (textured by OpenGL ES 2.0 Quads) that I iterate over to move. Here is a simplified version of my code:

//'sprite' & other values have been are assumed to exist for the purpose of the question

public void moveQuadBatch(){

    //Loop for as many sprites as there are to draw
    for (loop = 0; loop < sprite.number; loop++){

        moveQuadNumber(loop); //this method will move the sprite that corresponds to the number loops so we can move through the entire batch and move each individual sprite


    }
}

Now for some parties there is a countdown timer or some other condition (and for others there is, for example, above). Therefore, I created a similar method for such objects:

public void moveQuadBatchWithCheck(){

    //Loop for as many sprites as there are to draw
    for (loop = 0; loop < sprite.number; loop++){

        //Only do this if the particular sprite countdown/delay counter has reached 0 (counting down from another method not seen here)
        if (sprite.delay[loop]<0){

            moveQuadNumber(loop); //this method will move the sprite that corresponds to the number loops so we can move through the entire batch and move each individual sprite

        }
    }
}

However, I am not completely satisfied with this, as there is a lot of code duplication. Instead of using these 2 methods, can I use the first and somehow "slipstream" additional check in a for loop? Or else cut back on the duplication that I have? This is a simple example, there are others, and currently I have several methods that are very similar.

Edit

, . , ( , ), 2 .

+4
4
public void moveQuadBatch(bool checkDelay) {
    for (loop = 0; loop < sprite.number; loop++){
        if (!checkDelay || sprite.delay[loop] < 0) {
             moveQuadNumber(loop);
        }
    }
}

moveQuadBatch(false) - , moveQuadBatch(true) - .


" ", . Python , , :

    def moveQuadBatch(predicate=None):
        for loop, sprite in enumerate(self.sprites): 
            if not predicate or predicate(loop, sprite):
                self.moveQuadNumber(loop)

:

inst.moveQuadBatch()  
inst.moveQuadBatch(lambda loop, sprite: sprite.delay[loop] < 0)
inst.moveQuadBatch(lambda loop, sprite: sprite.doesItBlend(loop))

Java, : . . . .

public interface QuadBatchPredicate {
    public boolean shouldMove(int loop, Sprite sprite);
}

:

public void moveQuadBatch(QuadBatchPredicate pred) {
    for (loop = 0; loop < sprite.number; loop++){
        if (pred == null || pred(loop, sprite)) {
             moveQuadNumber(loop);
        }
    }
}
public void moveQuadBatch() {
    moveQuadBatch(null);
}

:

moveQuadBatch();
moveQuadBatch(new QuadBatchPredicate() {
    public boolean shouldMove(int loop, Sprite sprite) {
        return sprite.delay[loop] < 0;
    }
});
moveQuadBatch(new QuadBatchPredicate() {
    public boolean shouldMove(int loop, Sprite sprite) {
        return sprite.doesItBlend();
    }
});

, Python, . " " , , , , . , , :

QuadBatchPredicate checkBlends = new QuadBatchPredicate() {
    public boolean shouldMove(int loop, Sprite sprite) {
        return sprite.doesItBlend();
    }
};
+3

:

public interface MyInteface {
void do_something(int loop);
}

, .

public class MyInterfaceImpl {
    public void do_something(int loop) {
         if (!checkDelay || sprite.delay[loop] < 0) {
              moveQuadNumber(loop);
         }
    }

:

public void moveQuadBatch(MyInterface interface) {
    for (int loop = 0; loop < sprite.number; loop++){
        interface.do_something(loop)
    }
}
+2

"" .

public class Sprite {
    private boolean active = true;
    private ArrayList<Effect> effects = new ArrayList<>();

    public void update(int time) {
        for(Effect e: effects) {
            e.update(this, time);
        }
    }

    public void addEffect(Effect effect) {
        effects.add(effect);
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}

public interface Effect {
    void update(Sprite sprite, int time);
}

public class Delay implements Effect {
    private int delay;

    public Delay(int delay) {
        this.delay = delay;
    }

    public void update(Sprite sprite, int time) {
        delay -= time;
        if(delay > 0) sprite.setActive(false);
        else sprite.setActive(true);
    }
}
+1

:

public void moveQuadBatchWithCheck(bool check){
    for (loop = 0; loop < sprite.number; loop++){
        if(check){
            if (sprite.delay[loop]<0){
                moveQuadNumber(loop); 
                }
       }
       else{
           moveQuadNumber(loop);
           }
    }

, true .

Not sure if this is what you are looking for, but I hope this helps.

0
source

Source: https://habr.com/ru/post/1527050/


All Articles