changeset 4723:ec7c2aab5d8b

Add Iterable.count() and test. Fix test warnings.
author smarks
date Wed, 14 Mar 2012 17:39:25 -0700
parents d96afa0fa487
children 1c08773d3b02 6bef01ceeb94
files src/share/classes/java/lang/Iterable.java test-ng/build.xml test-ng/tests/java/util/IterableTest.java test-ng/tests/java/util/LambdaTestHelpers.java test-ng/tests/java/util/functions/BlocksTest.java
diffstat 5 files changed, 24 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/Iterable.java	Tue Jan 10 17:16:26 2012 -0800
+++ b/src/share/classes/java/lang/Iterable.java	Wed Mar 14 17:39:25 2012 -0700
@@ -213,4 +213,8 @@
     public Iterable<T> uniqueElements() default {
         return Iterables.uniqueElements(this);
     }
+
+    public long count() default {
+        return Iterables.count(this);
+    }
 }
--- a/test-ng/build.xml	Tue Jan 10 17:16:26 2012 -0800
+++ b/test-ng/build.xml	Wed Mar 14 17:39:25 2012 -0700
@@ -26,7 +26,9 @@
 
     <target name="test-compile" depends="prepare">
         <javac destdir="${test.classes.dir}" debug="on" srcdir="${test.src.dir}" fork="true"
-               classpathref="test.class.path"/>
+               classpathref="test.class.path">
+            <compilerarg value="-Xlint:all"/>
+        </javac>
     </target>
 
     <target name="test" depends="test-compile" >
--- a/test-ng/tests/java/util/IterableTest.java	Tue Jan 10 17:16:26 2012 -0800
+++ b/test-ng/tests/java/util/IterableTest.java	Wed Mar 14 17:39:25 2012 -0700
@@ -42,6 +42,12 @@
         assertFalse(countTo(10).isEmpty());
     }
 
+    public void testCount() {
+        assertEquals(0, countTo(0).count());
+        assertEquals(1, countTo(1).count());
+        assertEquals(10, countTo(10).count());
+    }
+
     public void testFirst() {
         assertEquals(null, countTo(0).getFirst());
         assertEquals(1, (int) countTo(1).getFirst());
--- a/test-ng/tests/java/util/LambdaTestHelpers.java	Tue Jan 10 17:16:26 2012 -0800
+++ b/test-ng/tests/java/util/LambdaTestHelpers.java	Wed Mar 14 17:39:25 2012 -0700
@@ -83,7 +83,7 @@
     }
 
     public static List<Integer> countTo(int n) {
-        ArrayList<Integer> list = new ArrayList<>();
+        ArrayList<Integer> list = new ArrayList<>(n);
         for (int i=1; i<=n; i++) {
             list.add(i);
         }
--- a/test-ng/tests/java/util/functions/BlocksTest.java	Tue Jan 10 17:16:26 2012 -0800
+++ b/test-ng/tests/java/util/functions/BlocksTest.java	Wed Mar 14 17:39:25 2012 -0700
@@ -68,7 +68,8 @@
     public void testChainVarArgs() {
         final boolean results[] = new boolean[3];
 
-        Block<Boolean> blocks[] = (Block<Boolean>[]) new Block[] {
+        @SuppressWarnings("unchecked")
+        Block<Boolean> blocks[] = (Block<Boolean>[]) new Block<?>[] {
             (Block<Boolean>) b -> { results[0] = b; },
             (Block<Boolean>) b -> { results[1] = b; },
             (Block<Boolean>) b -> { results[2] = b; }
@@ -86,7 +87,8 @@
         Block<Boolean> block1 = b -> { results[1] = b; };
         Block<Boolean> block2 = b -> { results[2] = b; };
 
-        Block<Boolean> blocks[] = (Block<Boolean>[]) new Block[]{
+        @SuppressWarnings("unchecked")
+        Block<Boolean> blocks[] = (Block<Boolean>[]) new Block<?>[]{
             block1,
             block2};
 
@@ -98,7 +100,9 @@
    @Test
    public void testChainIterable() {
        final boolean results[] = new boolean[3];
-       final Block<Boolean> blocks[] = (Block<Boolean>[]) new Block[] {
+
+       @SuppressWarnings("unchecked")
+       final Block<Boolean> blocks[] = (Block<Boolean>[]) new Block<?>[] {
             (Block<Boolean>) b -> { results[0] = b; },
             (Block<Boolean>) b -> { results[1] = b; },
             (Block<Boolean>) b -> { results[2] = b; },
@@ -111,7 +115,9 @@
     @Test
    public void testChainIterable2() {
        final boolean results[] = new boolean[3];
-       Block<Boolean> blocks[] = new Block[]{
+
+       @SuppressWarnings("unchecked")
+       Block<Boolean> blocks[] = (Block<Boolean>[]) new Block<?>[]{
             (Block<Boolean>) b -> { results[1] = b; },
             (Block<Boolean>) b -> { results[2] = b; }
         };