Created
February 28, 2014 17:05
-
-
Save kamichidu/9274995 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Iterator; | |
| import java.util.Random; | |
| // example 1 | |
| // using java.util.Iterator | |
| class InfiniteSequence1 implements Iterator<Integer> | |
| { | |
| public InfiniteSequence1(Random rng) | |
| { | |
| this.rng= rng; | |
| } | |
| @Override | |
| public Integer next() | |
| { | |
| return this.rng.nextInt(); | |
| } | |
| @Override | |
| public boolean hasNext() | |
| { | |
| return true; | |
| } | |
| @Override | |
| public void remove() | |
| { | |
| throw new UnsupportedOperationException(); | |
| } | |
| private final Random rng; | |
| } | |
| // example 2 | |
| // using own type | |
| interface InfiniteSequence<T> | |
| { | |
| T next(); | |
| } | |
| class InfiniteSequence2 implements InfiniteSequence<Integer> | |
| { | |
| public InfiniteSequence2(Random rng) | |
| { | |
| this.rng= rng; | |
| } | |
| @Override | |
| public Integer next() | |
| { | |
| return this.rng.nextInt(); | |
| } | |
| private final Random rng; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment