You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by ataylor284 <gi...@git.apache.org> on 2016/01/24 08:59:47 UTC

[GitHub] groovy pull request: Added new class BufferedIterator.

GitHub user ataylor284 opened a pull request:

    https://github.com/apache/groovy/pull/247

    Added new class BufferedIterator.

    BufferedIterator is a wrapper for Iterator that adds a new method
    `head` to return the next item without consuming it.  A helper method
    was also added to DefaultGroovyMethods to create a buffered wrapper by
    calling iterator.buffered() on any Iterator.  BufferedIterator is
    loosely based on the Scala class of the same name.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ataylor284/groovy feature/buffered-iterator

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/groovy/pull/247.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #247
    
----
commit 2c4c03af8729ba1b51dbf5c7ff7fa5ac9f6c7633
Author: Andrew Taylor <at...@redtoad.ca>
Date:   2016-01-24T07:45:52Z

    Added BufferedIterator.
    
    BufferedIterator is a wrapper for Iterator that adds a new method
    `head` to return the next item without consuming it.  A helper method
    was also added to DefaultGroovyMethods to create a buffered wrapper by
    calling iterator.buffered() on any Iterator.  BufferedIterator is
    loosely based on the Scala class of the same name.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by jwagenleitner <gi...@git.apache.org>.
Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51361938
  
    --- Diff: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---
    @@ -16045,6 +16048,51 @@ public void remove() {
         }
     
         /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert [1, 2, 3, 4].iterator().buffered().with { [head(), toList()] } == [1, [1, 2, 3, 4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a BufferedIterator wrapping self
    +     * @since 2.5.0
    +     */
    +    public static <T> BufferedIterator<T> buffered(Iterator<T> self) {
    +        return new IteratorBufferedIterator<T>(self);
    +    }
    +
    +    /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert new LinkedHashSet([1,2,3,4]).bufferedIterator().with { [head(), toList()] } == [1, [1,2,3,4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a BufferedIterator wrapping an iterator for self
    +     * @since 2.5.0
    +     */
    +    public static <T> BufferedIterator<T> bufferedIterator(Iterable<T> self) {
    +        return new IteratorBufferedIterator<T>(self.iterator());
    +    }
    +
    +    /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert [1, 2, 3, 4].bufferedIterator().with { [head(), toList()] } == [1, [1, 2, 3, 4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a ListBufferedIterator wrapping self
    --- End diff --
    
    Might be good to refer to the general `BufferedIterator` and not the specific implementation type.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by jwagenleitner <gi...@git.apache.org>.
Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51186698
  
    --- Diff: src/main/groovy/util/BufferedIterator.java ---
    @@ -0,0 +1,69 @@
    +/**
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing,
    + *  software distributed under the License is distributed on an
    + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + *  KIND, either express or implied.  See the License for the
    + *  specific language governing permissions and limitations
    + *  under the License.
    + */
    +package groovy.util;
    +
    +import java.util.Iterator;
    +
    +/**
    + * An iterator that allows examining the next element without consuming it.
    + *
    + * @author Andrew Taylor
    + * @since 2.5.0
    + */
    +public class BufferedIterator<T> implements Iterator<T> {
    +
    +    private final Iterator<T> iter;
    +    private boolean hasBuffered;
    +    private T buffered;
    +
    +    public BufferedIterator(Iterator<T> iter) {
    +        this.iter = iter;
    +        this.hasBuffered = false;
    +    }
    +
    +    public boolean hasNext() {
    +        return hasBuffered || iter.hasNext();
    +    }
    +
    +    public T next() {
    +        if (hasBuffered) {
    +            T buffered = this.buffered;
    +            this.buffered = null;
    +            hasBuffered = false;
    +            return buffered;
    +        } else {
    +            return iter.next();
    +        }
    +    }
    +
    +    public void remove() {
    +        throw new UnsupportedOperationException();
    --- End diff --
    
    Good point, my mistake, I was thinking in terms of `remove()` removing the element returned from the last call to either `head` or `next` but this would be confusing if you wanted to delete the current value based on the next value.
    
    ```
    value = itr.next() // 1
    if (itr.head() == 2) {
        itr.remove() // removes 2 not 1
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by jwagenleitner <gi...@git.apache.org>.
Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51169714
  
    --- Diff: src/main/groovy/util/BufferedIterator.java ---
    @@ -0,0 +1,69 @@
    +/**
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing,
    + *  software distributed under the License is distributed on an
    + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + *  KIND, either express or implied.  See the License for the
    + *  specific language governing permissions and limitations
    + *  under the License.
    + */
    +package groovy.util;
    +
    +import java.util.Iterator;
    +
    +/**
    + * An iterator that allows examining the next element without consuming it.
    + *
    + * @author Andrew Taylor
    + * @since 2.5.0
    + */
    +public class BufferedIterator<T> implements Iterator<T> {
    +
    +    private final Iterator<T> iter;
    +    private boolean hasBuffered;
    +    private T buffered;
    +
    +    public BufferedIterator(Iterator<T> iter) {
    +        this.iter = iter;
    +        this.hasBuffered = false;
    +    }
    +
    +    public boolean hasNext() {
    +        return hasBuffered || iter.hasNext();
    +    }
    +
    +    public T next() {
    +        if (hasBuffered) {
    +            T buffered = this.buffered;
    +            this.buffered = null;
    +            hasBuffered = false;
    +            return buffered;
    +        } else {
    +            return iter.next();
    +        }
    +    }
    +
    +    public void remove() {
    +        throw new UnsupportedOperationException();
    --- End diff --
    
    Any reason not to delegate this and then clear buffered/hasBuffered?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by ataylor284 <gi...@git.apache.org>.
Github user ataylor284 commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51177000
  
    --- Diff: src/main/groovy/util/BufferedIterator.java ---
    @@ -0,0 +1,69 @@
    +/**
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing,
    + *  software distributed under the License is distributed on an
    + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + *  KIND, either express or implied.  See the License for the
    + *  specific language governing permissions and limitations
    + *  under the License.
    + */
    +package groovy.util;
    +
    +import java.util.Iterator;
    +
    +/**
    + * An iterator that allows examining the next element without consuming it.
    + *
    + * @author Andrew Taylor
    + * @since 2.5.0
    + */
    +public class BufferedIterator<T> implements Iterator<T> {
    +
    +    private final Iterator<T> iter;
    +    private boolean hasBuffered;
    +    private T buffered;
    +
    +    public BufferedIterator(Iterator<T> iter) {
    +        this.iter = iter;
    +        this.hasBuffered = false;
    +    }
    +
    +    public boolean hasNext() {
    +        return hasBuffered || iter.hasNext();
    +    }
    +
    +    public T next() {
    +        if (hasBuffered) {
    +            T buffered = this.buffered;
    +            this.buffered = null;
    +            hasBuffered = false;
    +            return buffered;
    +        } else {
    +            return iter.next();
    +        }
    +    }
    +
    +    public void remove() {
    +        throw new UnsupportedOperationException();
    --- End diff --
    
    If the `head` method has been called and an item is buffered, the underlying iterator is pointing at the wrong element.  Delegating would remove the item after the one it should.  This case could be detected but there's no way to handle it correctly.  Rather than failing in some cases, I think it's better to just not support removal.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by jwagenleitner <gi...@git.apache.org>.
Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51361896
  
    --- Diff: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---
    @@ -16045,6 +16048,51 @@ public void remove() {
         }
     
         /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert [1, 2, 3, 4].iterator().buffered().with { [head(), toList()] } == [1, [1, 2, 3, 4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a BufferedIterator wrapping self
    +     * @since 2.5.0
    +     */
    +    public static <T> BufferedIterator<T> buffered(Iterator<T> self) {
    +        return new IteratorBufferedIterator<T>(self);
    +    }
    +
    +    /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert new LinkedHashSet([1,2,3,4]).bufferedIterator().with { [head(), toList()] } == [1, [1,2,3,4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    --- End diff --
    
    s/iterator/iterable/


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by jwagenleitner <gi...@git.apache.org>.
Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51361877
  
    --- Diff: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---
    @@ -16045,6 +16048,51 @@ public void remove() {
         }
     
         /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert [1, 2, 3, 4].iterator().buffered().with { [head(), toList()] } == [1, [1, 2, 3, 4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a BufferedIterator wrapping self
    +     * @since 2.5.0
    +     */
    +    public static <T> BufferedIterator<T> buffered(Iterator<T> self) {
    +        return new IteratorBufferedIterator<T>(self);
    --- End diff --
    
    In case `self` is a `BufferedIterator` should this return `self` instead of re-wrapping?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by jwagenleitner <gi...@git.apache.org>.
Github user jwagenleitner commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/247#discussion_r51361900
  
    --- Diff: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---
    @@ -16045,6 +16048,51 @@ public void remove() {
         }
     
         /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert [1, 2, 3, 4].iterator().buffered().with { [head(), toList()] } == [1, [1, 2, 3, 4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a BufferedIterator wrapping self
    +     * @since 2.5.0
    +     */
    +    public static <T> BufferedIterator<T> buffered(Iterator<T> self) {
    +        return new IteratorBufferedIterator<T>(self);
    +    }
    +
    +    /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert new LinkedHashSet([1,2,3,4]).bufferedIterator().with { [head(), toList()] } == [1, [1,2,3,4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    +     * @return a BufferedIterator wrapping an iterator for self
    +     * @since 2.5.0
    +     */
    +    public static <T> BufferedIterator<T> bufferedIterator(Iterable<T> self) {
    +        return new IteratorBufferedIterator<T>(self.iterator());
    +    }
    +
    +    /**
    +     * Returns a <code>BufferedIterator</code> that allows examining the next element without
    +     * consuming it.
    +     * <pre class="groovyTestCase">
    +     * assert [1, 2, 3, 4].bufferedIterator().with { [head(), toList()] } == [1, [1, 2, 3, 4]]
    +     * </pre>
    +     *
    +     * @param self an iterator object
    --- End diff --
    
    s/iterator/list/


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request: Added new class BufferedIterator.

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/groovy/pull/247


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---