Github user paplorinc commented on a diff in the pull request:
https://github.com/apache/groovy/pull/360#discussion_r69130766
--- Diff: src/main/groovy/lang/ObjectRange.java ---
@@ -357,160 +363,198 @@ public String toString() {
return reverse ? "" + to + ".." + from : "" + from + ".." + to;
}
+ @Override
public String inspect() {
- String toText = InvokerHelper.inspect(to);
- String fromText = InvokerHelper.inspect(from);
+ final String toText = InvokerHelper.inspect(to);
+ final String fromText = InvokerHelper.inspect(from);
return reverse ? "" + toText + ".." + fromText : "" + fromText +
".." + toText;
}
/**
* iterates over all values and returns true if one value matches.
* Also see containsWithinBounds.
*/
+ @Override
public boolean contains(Object value) {
- Iterator it = new StepIterator(this, 1);
- if (value == null) return false;
- while (it.hasNext()) {
- try {
- if (DefaultTypeTransformation.compareEqual(value,
it.next())) return true;
- } catch (ClassCastException e) {
- return false;
+ if (value == null) {
+ return false;
+ }
+
+ for (Iterator it = iterator(); it.hasNext(); ) {
+ if (compareEqual(value, it.next())) {
+ return true;
}
}
return false;
}
+ @Override
public void step(int step, Closure closure) {
- if (step == 0 && compareTo(from, to) == 0) {
+ step((Number) step, closure);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void step(Number step, Closure closure) {
+ if (compareEqual(step, 0) && compareTo(from, to) == 0) {
return; // from == to and step == 0, nothing to do, so return
}
- StepIterator iter = new StepIterator(this, step);
- while (iter.hasNext()) {
- closure.call(iter.next());
+
+ for (Iterator it = new LazySteppingIterator(this, step);
it.hasNext(); ) {
+ closure.call(it.next());
}
}
/**
* {@inheritDoc}
*/
+ @Override
public Iterator iterator() {
- // non thread-safe iterator
- final Iterator innerIterator = new StepIterator(this, 1);
- Iterator safeIterator = new Iterator() {
+ return new Iterator() {
+ private final Iterator delegate = new
LazySteppingIterator(ObjectRange.this, 1);
+
@Override
public synchronized boolean hasNext() {
- return innerIterator.hasNext();
+ return delegate.hasNext();
}
+
@Override
public synchronized Object next() {
- return innerIterator.next();
+ return delegate.next();
}
+
@Override
public void remove() {
- innerIterator.remove();
+ throw new UnsupportedOperationException();
--- End diff --
ok, will also synchronize it then
---
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.
---