Spliterator issue?
Paul Sandoz
paul.sandoz at oracle.com
Tue Mar 26 13:12:56 PDT 2013
Hi Ben,
I think we can go ahead and commit this but i would like to iterate further on it. Did you see my email on testing? I think we can improve the scope of testing by copying the example i presented and we can widen the test data, for example we should have 0 length strings and regexes that match no input, just to test empty data.
I can commit tomorrow if you like. I may tidy up the javadoc a little and add some extra characteristics.
Paul.
On Mar 24, 2013, at 1:11 PM, Ben Evans <benjamin.john.evans at gmail.com> wrote:
> OK, looks like a Heisenbug.
>
> Cleaning up & rebuilding from scratch seems to have fixed it. Thanks
> to Boaz for doing a quick test for me.
>
> New webrev at: http://www.java7developer.com/webrev-kittylyst.003/
>
> Thanks,
>
> Ben
>
> On Sat, Mar 23, 2013 at 3:25 PM, Ben Evans
> <benjamin.john.evans at gmail.com> wrote:
>> Hi,
>>
>> I've rewritten the MatcherIterator for the Pattern point
>> lambdafication to properly cache values and have an idempotent
>> hasNext(), like this:
>>
>> private static class MatcherIterator implements Iterator<CharSequence> {
>> private final Matcher curMatcher;
>> private final CharSequence input;
>> private int current = 0;
>> private CharSequence nextElement = null;
>> private boolean valueReady = false;
>>
>> MatcherIterator(CharSequence in, Matcher m) {
>> input = in;
>> curMatcher = m;
>> }
>>
>> public void accept(CharSequence t) {
>> valueReady = true;
>> nextElement = t;
>> }
>>
>> public CharSequence next() {
>> if (!valueReady && !hasNext())
>> throw new NoSuchElementException();
>> else {
>> valueReady = false;
>> return nextElement;
>> }
>> }
>>
>> public boolean hasNext() {
>> if (!valueReady) {
>> if (current == input.length()) return false;
>> if (curMatcher.find()) {
>> nextElement = input.subSequence(current, curMatcher.start());
>> current = curMatcher.end();
>> valueReady = true;
>> } else {
>> nextElement = input.subSequence(current, input.length());
>> current = input.length();
>> valueReady = true;
>> }
>> }
>> return true;
>> }
>> }
>>
>> public Stream<CharSequence> splitAsStream(final CharSequence input) {
>> return Streams.stream(Spliterators.spliteratorUnknownSize(new
>> MatcherIterator(input, p.matcher(input)), Spliterator.ORDERED));
>> }
>>
>> which seems to behave the way that I expect, except that calling
>> splitAsStream() causes a NoSuchElementException to always be thrown.
>>
>> Has something changed in the implementation of Spliterator which
>> causes this to happen?
>>
>> Thanks,
>>
>> Ben
More information about the lambda-dev
mailing list