Author: markt Date: Tue Jul 16 12:34:36 2013 New Revision: 1503683 URL: http://svn.apache.org/r1503683 Log: EL 3.0 collections operations. Implement max and min.
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=1503683&r1=1503682&r2=1503683&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/el/stream/Stream.java (original) +++ tomcat/trunk/java/org/apache/el/stream/Stream.java Tue Jul 16 12:34:36 2013 @@ -25,6 +25,7 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.Set; +import javax.el.ELException; import javax.el.LambdaExpression; import org.apache.el.lang.ELSupport; @@ -287,6 +288,87 @@ public class Stream { } + public Optional max() { + return compare(true); + } + + + public Optional max(LambdaExpression le) { + return compare(true, le); + } + + + public Optional min() { + return compare(false); + } + + + public Optional min(LambdaExpression le) { + return compare(false, le); + } + + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private Optional compare(boolean isMax) { + Comparable result = null; + + if (iterator.hasNext()) { + Object obj = iterator.next(); + if ((obj instanceof Comparable)) { + result = (Comparable) obj; + } else { + throw new ELException(); + } + } + + while (iterator.hasNext()) { + Object obj = iterator.next(); + if ((obj instanceof Comparable)) { + if (isMax && ((Comparable) obj).compareTo(result) > 0) { + result = (Comparable) obj; + } else if (!isMax && ((Comparable) obj).compareTo(result) < 0) { + result = (Comparable) obj; + } + } else { + throw new ELException(); + } + } + + if (result == null) { + return Optional.EMPTY; + } else { + return new Optional(result); + } + } + + + private Optional compare(boolean isMax, LambdaExpression le) { + Object result = null; + + if (iterator.hasNext()) { + Object obj = iterator.next(); + result = obj; + } + + while (iterator.hasNext()) { + Object obj = iterator.next(); + if (isMax && ELSupport.coerceToNumber(le.invoke(obj, result), + Integer.class).intValue() > 0) { + result = obj; + } else if (!isMax && ELSupport.coerceToNumber(le.invoke(obj, result), + Integer.class).intValue() < 0) { + result = obj; + } + } + + if (result == null) { + return Optional.EMPTY; + } else { + return new Optional(result); + } + } + + private static class LambdaExpressionComparator implements Comparator<Object>{ 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=1503683&r1=1503682&r2=1503683&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.java (original) +++ tomcat/trunk/test/org/apache/el/stream/TestCollectionOperations.java Tue Jul 16 12:34:36 2013 @@ -360,4 +360,124 @@ public class TestCollectionOperations { Assert.assertEquals(Long.valueOf(25), result); } + + + @Test + public void testMax01() { + ELProcessor processor = new ELProcessor(); + + Object result = processor.getValue( + "[1,2,3,4,5].stream().max()", + Object.class); + + Assert.assertEquals(Long.valueOf(5), ((Optional) result).get()); + } + + + @Test + public void testMax02() { + ELProcessor processor = new ELProcessor(); + + Object result = processor.getValue( + "[5,4,3,2,1].stream().max()", + Object.class); + + Assert.assertEquals(Long.valueOf(5), ((Optional) result).get()); + } + + + @Test(expected=ELException.class) + public void testMax03() { + ELProcessor processor = new ELProcessor(); + + Object result = processor.getValue( + "[].stream().max()", + Object.class); + + ((Optional) result).get(); + } + + + @Test(expected=ELException.class) + public void testMax04() { + ELProcessor processor = new ELProcessor(); + processor.defineBean("beans", beans); + + processor.getValue( + "beans.stream().max()", + Object.class); + } + + + @Test + public void testMaxLambda01() { + ELProcessor processor = new ELProcessor(); + processor.defineBean("beans", beans); + + Object result = processor.getValue( + "beans.stream().max((x,y)->x.name.compareTo(y.name))", + Object.class); + + Assert.assertEquals(bean03, ((Optional) result).get()); + } + + + @Test + public void testMin01() { + ELProcessor processor = new ELProcessor(); + + Object result = processor.getValue( + "[1,2,3,4,5].stream().min()", + Object.class); + + Assert.assertEquals(Long.valueOf(1), ((Optional) result).get()); + } + + + @Test + public void testMin02() { + ELProcessor processor = new ELProcessor(); + + Object result = processor.getValue( + "[5,4,3,2,1].stream().min()", + Object.class); + + Assert.assertEquals(Long.valueOf(1), ((Optional) result).get()); + } + + + @Test(expected=ELException.class) + public void testMin03() { + ELProcessor processor = new ELProcessor(); + + Object result = processor.getValue( + "[].stream().min()", + Object.class); + + ((Optional) result).get(); + } + + + @Test(expected=ELException.class) + public void testMin04() { + ELProcessor processor = new ELProcessor(); + processor.defineBean("beans", beans); + + processor.getValue( + "beans.stream().min()", + Object.class); + } + + + @Test + public void testMinLambda01() { + ELProcessor processor = new ELProcessor(); + processor.defineBean("beans", beans); + + Object result = processor.getValue( + "beans.stream().min((x,y)->x.name.compareTo(y.name))", + Object.class); + + Assert.assertEquals(bean01, ((Optional) result).get()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org