package dk.itu.oop.lecture5; import java.util.Iterator; public class AlternateMerge extends MergeIterators{ // inv: chooseFirst = true means that choose chose i2 last time, and i1 // will be chosen next time. chooseFirst = false means that choose chose // i1 last time, and will choose i2 next time. private boolean chooseFirst; public AlternateMerge(OOPIterator itr1, OOPIterator itr2){ super(itr1,itr2); chooseFirst = true; } /*pre: i1 and i2 both have elements left *post: return either i1 or i2, but not the same twice in a row. */ protected OOPIterator choose(OOPIterator i1, OOPIterator i2){ if (chooseFirst){ chooseFirst = false; return i1; }else{ chooseFirst = true; return i2; } } }