StepIterator.java 2.13 KB
package com.example.mina.client.experiment;

import com.example.mina.client.base.MatrixCommand;
import com.example.mina.client.base.StationPair;

import java.util.Iterator;

/**
 * This is used for handover iterations
 * Usage:
 * StationPair stationPair = StationPair.builder().out(5).inList(Arrays.asList(1,2,3)).build();
 *         StepIterator stepIterator = new StepIterator(stationPair, 3, 43, 10);
 *
 *
 *         while (stepIterator.hasNext()) {
 *             MatrixCommand[] cmds = stepIterator.next();
 *             System.out.println(cmds[0] + "----------"+ cmds[1]);
 *         }
 *
 */

public class StepIterator implements Iterator{

    private StationPair stationPair;
    private int minAttn;
    private int maxAttn;
    private int step;

    private int curInputIndex;
    private int nextInputIndex;

    private int curAttn1;
    private int curAttn2;

    public StepIterator(StationPair station, int min, int max, int step) {
        this.stationPair = station;
        this.minAttn = min;
        this.maxAttn = max;
        this.step = step;

        curInputIndex = 0;
        nextInputIndex = curInputIndex + 1;
        curAttn1 = minAttn;
        curAttn2 = maxAttn;
    }

    @Override
    public boolean hasNext() {
        return curInputIndex<stationPair.getInList().size() && curAttn2<=maxAttn;
    }

    @Override
    public MatrixCommand[] next() {
        MatrixCommand[] cmds = new MatrixCommand[2];

        cmds[0] = MatrixCommand.builder().row(stationPair.getInList().get(curInputIndex))
                .col(stationPair.getOut()).attn(curAttn1).build();
        cmds[1] = MatrixCommand.builder().row(stationPair.getInList().get(nextInputIndex))
                .col(stationPair.getOut()).attn(curAttn2).build();
        if(curAttn1 >= maxAttn) {
            cmds[0].setAttn(maxAttn);
            cmds[1].setAttn(minAttn);
            curInputIndex++;
            nextInputIndex = (curInputIndex + 1) % stationPair.getInList().size();

            curAttn1 = minAttn;
            curAttn2 = maxAttn;
        }else{
            curAttn1 = curAttn1 + step;
            curAttn2 = curAttn2 - step;
        }

        return cmds;
    }
}