@@ -0,0 +1,2981 @@
+package com.github.nikita_volkov.java.iterators;%0A%0Aimport java.util.*;%0A%0Apublic final class InterleavingIterator%3Celement%3E implements Iterator%3Celement%3E %7B%0A%0A  private final Iterator%3Celement%3E leftIterator;%0A  private final Iterator%3Celement%3E rightIterator;%0A  private final Comparator%3Celement%3E comparator;%0A  private final LinkedList%3Celement%3E leftBuffer = new LinkedList%3C%3E();%0A  private final LinkedList%3Celement%3E rightBuffer = new LinkedList%3C%3E();%0A%0A  public InterleavingIterator(Iterator%3Celement%3E leftIterator, Iterator%3Celement%3E rightIterator, Comparator%3Celement%3E comparator) %7B%0A    this.leftIterator = leftIterator;%0A    this.rightIterator = rightIterator;%0A    this.comparator = comparator;%0A  %7D%0A%0A  @Override%0A  public boolean hasNext() %7B%0A    return !leftBuffer.isEmpty() %7C%7C !rightBuffer.isEmpty() %7C%7C leftIterator.hasNext() %7C%7C rightIterator.hasNext();%0A  %7D%0A%0A  @Override%0A  public element next() %7B%0A    element leftBufferedElement = leftBuffer.poll();%0A    element rightBufferedElement = rightBuffer.poll();%0A    if (leftBufferedElement != null) %7B%0A      if (rightBufferedElement != null) %7B%0A        if (comparator.compare(leftBufferedElement, rightBufferedElement) %3C= 0) %7B%0A          leftBuffer.addFirst(leftBufferedElement);%0A          return rightBufferedElement;%0A        %7D else %7B%0A          rightBuffer.addFirst(rightBufferedElement);%0A          return leftBufferedElement;%0A        %7D%0A      %7D else if (rightIterator.hasNext()) %7B%0A        element nextRightElement = rightIterator.next();%0A        if (comparator.compare(leftBufferedElement, nextRightElement) %3C= 0) %7B%0A          leftBuffer.addFirst(leftBufferedElement);%0A          return nextRightElement;%0A        %7D else %7B%0A          rightBuffer.addLast(nextRightElement);%0A          return leftBufferedElement;%0A        %7D%0A      %7D else %7B%0A        return leftBufferedElement;%0A      %7D%0A    %7D else if (rightBufferedElement != null) %7B%0A      if (leftIterator.hasNext()) %7B%0A        element nextLeftElement = leftIterator.next();%0A        if (comparator.compare(nextLeftElement, rightBufferedElement) %3C= 0) %7B%0A          leftBuffer.addLast(nextLeftElement);%0A          return rightBufferedElement;%0A        %7D else %7B%0A          rightBuffer.addFirst(rightBufferedElement);%0A          return nextLeftElement;%0A        %7D%0A      %7D else %7B%0A        return rightBufferedElement;%0A      %7D%0A    %7D else %7B%0A      if (leftIterator.hasNext()) %7B%0A        element nextLeftElement = leftIterator.next();%0A        if (rightIterator.hasNext()) %7B%0A          element nextRightElement = rightIterator.next();%0A          if (comparator.compare(nextLeftElement, nextRightElement) %3C= 0) %7B%0A            leftBuffer.addLast(nextLeftElement);%0A            return nextRightElement;%0A          %7D else %7B%0A            rightBuffer.addLast(nextRightElement);%0A            return nextLeftElement;%0A          %7D%0A        %7D else %7B%0A          return nextLeftElement;%0A        %7D%0A      %7D else if (rightIterator.hasNext()) %7B%0A        return rightIterator.next();%0A      %7D else %7B%0A        throw new NoSuchElementException();%0A      %7D%0A    %7D%0A  %7D%0A%0A%7D%0A

Add InterleavingIterator