Author: markt
Date: Mon Jul 15 14:50:45 2013
New Revision: 1503281
URL: http://svn.apache.org/r1503281
Log:
Refactor to reduce duplication
Modified:
tomcat/trunk/java/org/apache/el/stream/Stream.java
Modified: tomcat/trunk/java/org/apache/el/stream/Stream.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/stream/Stream.java?rev=1503281&r1=1503280&r2=1503281&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/el/stream/Stream.java (original)
+++ tomcat/trunk/java/org/apache/el/stream/Stream.java Mon Jul 15 14:50:45 2013
@@ -36,41 +36,9 @@ public class Stream {
public Stream filter(final LambdaExpression le) {
- Iterator<Object> filterIterator = new Iterator<Object>() {
-
- private boolean foundNext = false;
- private Object next;
-
- @Override
- public boolean hasNext() {
- if (foundNext) {
- return true;
- }
- findNext();
- return foundNext;
- }
-
+ Iterator<Object> downStream = new OpIterator() {
@Override
- public Object next() {
- if (foundNext) {
- foundNext = false;
- return next;
- }
- findNext();
- if (foundNext) {
- foundNext = false;
- return next;
- } else {
- throw new NoSuchElementException();
- }
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- private void findNext() {
+ protected void findNext() {
while (iterator.hasNext()) {
Object obj = iterator.next();
if (ELSupport.coerceToBoolean(
@@ -82,55 +50,22 @@ public class Stream {
}
}
};
- return new Stream(filterIterator);
+ return new Stream(downStream);
}
public Stream map(final LambdaExpression le) {
- Iterator<Object> filterIterator = new Iterator<Object>() {
-
- private boolean foundNext = false;
- private Object next;
-
- @Override
- public boolean hasNext() {
- if (foundNext) {
- return true;
- }
- findNext();
- return foundNext;
- }
-
+ Iterator<Object> downStream = new OpIterator() {
@Override
- public Object next() {
- if (foundNext) {
- foundNext = false;
- return next;
- }
- findNext();
- if (foundNext) {
- foundNext = false;
- return next;
- } else {
- throw new NoSuchElementException();
- }
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- private void findNext() {
- while (iterator.hasNext()) {
+ protected void findNext() {
+ if (iterator.hasNext()) {
Object obj = iterator.next();
next = le.invoke(obj);
foundNext = true;
- break;
}
}
};
- return new Stream(filterIterator);
+ return new Stream(downStream);
}
@@ -141,4 +76,41 @@ public class Stream {
}
return result;
}
+
+
+ private abstract static class OpIterator implements Iterator<Object> {
+ protected boolean foundNext = false;
+ protected Object next;
+
+ @Override
+ public boolean hasNext() {
+ if (foundNext) {
+ return true;
+ }
+ findNext();
+ return foundNext;
+ }
+
+ @Override
+ public Object next() {
+ if (foundNext) {
+ foundNext = false;
+ return next;
+ }
+ findNext();
+ if (foundNext) {
+ foundNext = false;
+ return next;
+ } else {
+ throw new NoSuchElementException();
+ }
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ protected abstract void findNext();
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]