Skip to content

Instantly share code, notes, and snippets.

@manzke
Created August 24, 2013 20:43
Show Gist options
  • Save manzke/6330322 to your computer and use it in GitHub Desktop.
Save manzke/6330322 to your computer and use it in GitHub Desktop.
Example how an iterator can look like to use a BlockingQueue in java's foreach so this blocks if nothing in it.
public class Iterables {
public static <Type> Iterable<Type> iterable(final BlockingQueue<Type> queue){
return new Iterable<Type>() {
@Override
public Iterator<Type> iterator() {
return new Iterator<Type>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public Type next() {
try {
return queue.take();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void remove() {
}
};
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment