Skip to content

Instantly share code, notes, and snippets.

@kamichidu
Created February 28, 2014 17:05
Show Gist options
  • Select an option

  • Save kamichidu/9274995 to your computer and use it in GitHub Desktop.

Select an option

Save kamichidu/9274995 to your computer and use it in GitHub Desktop.
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