Github user paplorinc commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/360#discussion_r69119539
  
    --- Diff: src/main/groovy/lang/ObjectRange.java ---
    @@ -426,89 +419,52 @@ public void remove() {
                     innerIterator.remove();
                 }
             };
    -        return safeIterator;
         }
     
         /**
          * convenience class to serve in other methods.
          * It's not thread-safe, and lazily produces the next element only on 
calls of hasNext() or next()
          */
    -    private static class StepIterator implements Iterator {
    -        // actual step, can be +1 when desired step is -1 and direction is 
from high to low
    -        private final int step;
    +    static class StepIterator implements Iterator {
             private final ObjectRange range;
    -        private int index = -1;
    -        private Comparable value;
    -        private boolean nextFetched = true;
    +        private final boolean isAscending;
    +        private Object next;
     
    -        private StepIterator(ObjectRange range, final int desiredStep) {
    +        StepIterator(ObjectRange range, int desiredStep) {
                 if (desiredStep == 0 && compareNotEqual(range.getFrom(), 
range.getTo())) {
                     throw new GroovyRuntimeException("Infinite loop detected 
due to step size of 0");
                 }
    +
                 this.range = range;
    -            if (range.isReverse()) {
    -                step = -desiredStep;
    +            if (desiredStep < 0) {
    +                isAscending = range.isReverse();
                 } else {
    -                step = desiredStep;
    -            }
    -            if (step > 0) {
    -                value = range.getFrom();
    -            } else {
    -                value = range.getTo();
    +                isAscending = !range.isReverse();
                 }
    +            next = isAscending ? range.getFrom() : range.getTo();
             }
     
             @Override
             public void remove() {
    -            range.remove(index);
    +            throw new UnsupportedOperationException();
             }
     
             @Override
             public Object next() {
    -            // not thread safe
    -            if (!nextFetched) {
    -                value = peek();
    -                nextFetched = true;
    +            if (!hasNext()) {
    +                return null;
                 }
    -            nextFetched = false;
    -            index++;
    -            return value;
    +
    +            final Object result = next;
    +            next = isAscending ? increment(next) : decrement(next);
    +            return result;
             }
     
             @Override
             public boolean hasNext() {
    --- End diff --
    
    You're perfectly right would you like me to document it or make it lazy?


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to