Created
August 24, 2013 20:43
-
-
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.
This file contains 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
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