Author: markt
Date: Tue Jul 16 14:32:13 2013
New Revision: 1503734
URL: http://svn.apache.org/r1503734
Log:
EL 3.0 collections operations.
Implement anyMatch, allMatch noneMatch and findFirst.
Modified:
tomcat/trunk/java/org/apache/el/stream/Stream.java
tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.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=1503734&r1=1503733&r2=1503734&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/el/stream/Stream.java (original)
+++ tomcat/trunk/java/org/apache/el/stream/Stream.java Tue Jul 16 14:32:13 2013
@@ -349,6 +349,60 @@ public class Stream {
}
+ public Optional anyMatch(LambdaExpression le) {
+ if (!iterator.hasNext()) {
+ return Optional.EMPTY;
+ }
+
+ Boolean match = Boolean.FALSE;
+
+ while (!match.booleanValue() && iterator.hasNext()) {
+ match = (Boolean) le.invoke(iterator.next());
+ }
+
+ return new Optional(match);
+ }
+
+
+ public Optional allMatch(LambdaExpression le) {
+ if (!iterator.hasNext()) {
+ return Optional.EMPTY;
+ }
+
+ Boolean match = Boolean.TRUE;
+
+ while (match.booleanValue() && iterator.hasNext()) {
+ match = (Boolean) le.invoke(iterator.next());
+ }
+
+ return new Optional(match);
+ }
+
+
+ public Optional noneMatch(LambdaExpression le) {
+ if (!iterator.hasNext()) {
+ return Optional.EMPTY;
+ }
+
+ Boolean match = Boolean.FALSE;
+
+ while (!match.booleanValue() && iterator.hasNext()) {
+ match = (Boolean) le.invoke(iterator.next());
+ }
+
+ return new Optional(new Boolean(!match.booleanValue()));
+ }
+
+
+ public Optional findFirst() {
+ if (iterator.hasNext()) {
+ return new Optional(iterator.next());
+ } else {
+ return Optional.EMPTY;
+ }
+ }
+
+
@SuppressWarnings({ "rawtypes", "unchecked" })
private Optional compare(boolean isMax) {
Comparable result = null;
Modified: tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.java?rev=1503734&r1=1503733&r2=1503734&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.java
(original)
+++ tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.java Tue
Jul 16 14:32:13 2013
@@ -573,4 +573,161 @@ public class TestCollectionOperations {
Assert.assertTrue("Result: " + result.toString(),
ELSupport.equals(Long.valueOf(0), result));
}
+
+
+ @Test
+ public void testAnyMatch01() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().anyMatch(x->x==7)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.FALSE, result.get());
+ }
+
+
+ @Test
+ public void testAnyMatch02() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().anyMatch(x->x==3)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.TRUE, result.get());
+ }
+
+
+ @Test(expected=ELException.class)
+ public void testAnyMatch03() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[].stream().anyMatch(x->x==7)",
+ Object.class);
+
+ result.get();
+ }
+
+
+ @Test
+ public void testAllMatch01() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().allMatch(x->x>3)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.FALSE, result.get());
+ }
+
+
+ @Test
+ public void testAllMatch02() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().allMatch(x->x>0)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.TRUE, result.get());
+ }
+
+
+ @Test
+ public void testAllMatch03() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().allMatch(x->x>10)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.FALSE, result.get());
+ }
+
+
+ @Test(expected=ELException.class)
+ public void testAllMatch04() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[].stream().allMatch(x->x==7)",
+ Object.class);
+
+ result.get();
+ }
+
+
+ @Test
+ public void testNoneMatch01() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().allMatch(x->x>3)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.FALSE, result.get());
+ }
+
+
+ @Test
+ public void testNoneMatch02() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().noneMatch(x->x>0)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.FALSE, result.get());
+ }
+
+
+ @Test
+ public void testNoneMatch03() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[1,2,3,4,5].stream().noneMatch(x->x>10)",
+ Object.class);
+
+ Assert.assertEquals(Boolean.TRUE, result.get());
+ }
+
+
+ @Test(expected=ELException.class)
+ public void testNoneMatch04() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[].stream().noneMatch(x->x==7)",
+ Object.class);
+
+ result.get();
+ }
+
+
+ @Test
+ public void testFindFirst01() {
+ ELProcessor processor = new ELProcessor();
+ processor.defineBean("beans", beans);
+
+ Optional result = (Optional) processor.getValue(
+ "beans.stream().findFirst()",
+ Object.class);
+
+ Assert.assertEquals(bean01, result.get());
+ }
+
+
+ @Test(expected=ELException.class)
+ public void testFindFirst02() {
+ ELProcessor processor = new ELProcessor();
+
+ Optional result = (Optional) processor.getValue(
+ "[].stream().findFirst()",
+ Object.class);
+
+ result.get();
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]