Skip to content

Instantly share code, notes, and snippets.

@myui
Created June 25, 2018 05:16
Show Gist options
  • Save myui/0ea41466bfed4ffd772f179c3ad29edc to your computer and use it in GitHub Desktop.
Save myui/0ea41466bfed4ffd772f179c3ad29edc to your computer and use it in GitHub Desktop.
package puzzle;
import java.util.LinkedList;
import java.util.Queue;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
public class MergeSortedStreams implements SortedIntegerStream {
private final SortedIntegerStream x, y;
// Feel free to add more instance fields.
public MergeSortedStreams(@CheckForNull SortedIntegerStream x,
@CheckForNull SortedIntegerStream y) {
}
@Override
public boolean hasNext() {
}
@Override
public int getNext() throws IndexOutOfBoundsException {
return ret;
}
}
interface SortedIntegerStream {
/**
* Returns true if the stream has a remaining value.
*
* <p>
* It does not consume the stream.
*/
boolean hasNext();
/**
* Returns the next value in the stream.
*
* <p>
* It consumes one integer value in the stream.
*
* <p>
* It throws {@code IndexOutOfBoundsException} when the stream is empty.
*/
int getNext() throws IndexOutOfBoundsException;
}
class IntArrayStream implements SortedIntegerStream {
@Nonnull
private final Queue<Integer> queue;
IntArrayStream(@Nonnull Integer... integers) {
this.queue = new LinkedList<>();
for (Integer i : integers) {
queue.offer(i);
}
}
@Override
public boolean hasNext() {
return !queue.isEmpty();
}
@Override
public int getNext() throws IndexOutOfBoundsException {
if (queue.isEmpty()) {
throw new IndexOutOfBoundsException();
}
return queue.poll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment