package utility.merge; import exceptions.BoundaryViolationException; import exceptions.EmptyListException; import exceptions.InvalidPositionException; import position.NodePositionList; import position.Position; import position.PositionList; import utility.DefaultComparator; import java.util.Comparator; import java.util.Iterator; /** * Created with xgiovio.macbookair. * User: xgiovio * Date: 14/05/14 * Time: 14:48 */ public abstract class MergeTemplate { protected PositionList s = new NodePositionList(); protected Comparator c = null; public MergeTemplate (PositionList A, PositionList B){ this(A,B,new DefaultComparator()); } public MergeTemplate (PositionList A, PositionList B, Comparator in_c){ c = in_c; E a = null ,b = null; Iterator Ait, Bit; Ait = A.iterator(); Bit = B.iterator(); if ( Ait.hasNext() && Bit.hasNext() ){ a = Ait.next(); b = Bit.next(); for (; true; ) { if (c.compare(a, b) < 0) { aIsLess(a); if (Ait.hasNext()) a = Ait.next(); else break; } else { if (c.compare(a, b) > 0) { bIsLess(b); if (Bit.hasNext()) b = Bit.next(); else break; } else {// se b = a bothAreEqual(a, b); if (Ait.hasNext() && Bit.hasNext()) { a = Ait.next(); b = Bit.next(); } else break; } } } } while ( Ait.hasNext() ) { a = Ait.next(); aIsLess(a); } while ( Bit.hasNext()) { b = Bit.next(); bIsLess(b); } } abstract protected void aIsLess (E a); abstract protected void bIsLess (E b); abstract protected void bothAreEqual (E a, E b); public PositionList getResult () { return s; } }