changeset 7286:367b49b72f37

create a stream from a BitSet Contributed-by: akhil.arora@oracle.com
author mduigou
date Wed, 06 Feb 2013 13:28:23 -0800
parents 90d16fce304d
children 8c87198684fa
files src/share/classes/java/util/BitSet.java test/java/util/BitSet/BitSetStreamTest.java
diffstat 2 files changed, 100 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/BitSet.java	Thu Jan 31 23:55:46 2013 -0800
+++ b/src/share/classes/java/util/BitSet.java	Wed Feb 06 13:28:23 2013 -0800
@@ -1191,6 +1191,16 @@
         return b.toString();
     }
 
+    /**
+     * Returns a stream of indices for which this {@code BitSet}
+     * contains a bit in the set state. The indices are returned
+     * in order, from lowest to highest. The size of the stream
+     * is the number of bits in the set state, equal to the value
+     * returned by the {@link #cardinality()} method.
+
+     * @return a stream of integers representing set indices
+     * @since 1.8
+     */
     public IntStream stream() {
         class BitSetIterator implements IntStream.IntIterator {
             int next = nextSetBit(0);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/BitSet/BitSetStreamTest.java	Wed Feb 06 13:28:23 2013 -0800
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import org.testng.annotations.Test;
+
+import java.lang.Integer;
+import java.lang.Object;
+import java.lang.System;
+import java.util.BitSet;
+import java.util.OptionalInt;
+import java.util.Random;
+import java.util.stream.IntStream;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+
+/**
+ * @test
+ * @run testng BitSetStreamTest
+ * @summary test BitSet stream
+ */
+public class BitSetStreamTest {
+
+    @Test
+    public void testEdgeCases() {
+        final IntStream empty = new BitSet().stream();
+        empty.forEach(i -> fail("stream from empty bitset should be empty"));
+
+        final IntStream one = new BitSet(1).stream(); // 0001
+        one.forEach(i -> assertEquals(i, 0)); // bit at index 0 is set
+
+        final IntStream two = new BitSet(1).stream(); // 0010
+        two.forEach(i -> assertEquals(i, 1)); // bit at index 1 is set
+
+        final IntStream four = new BitSet(1).stream(); // 0100
+        four.forEach(i -> assertEquals(i, 2)); // bit at index 2 is set
+
+        final IntStream six = new BitSet(0x06).stream(); // 0110
+        six.forEach(i -> {
+            // only bits at index 1 and 2 are set
+            if (i != 1 && i != 2) {
+                fail("unexpected bit set at index " + i);
+            }
+        });
+    }
+
+    @Test
+    public void testRandomStream() {
+        final int size = 1024 * 1024;
+        final int[] seeds = {
+                2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
+                43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
+        final byte[] bytes = new byte[size];
+        for (int seed : seeds) {
+            final Random random = new Random(seed);
+            random.nextBytes(bytes);
+            final BitSet bitSet = BitSet.valueOf(bytes);
+            final int cardinality = bitSet.cardinality();
+            final IntStream stream = bitSet.stream();
+            final int[] array = stream.toArray();
+            assertEquals(array.length, cardinality);
+            int nextSetBit = -1;
+            for (int i=0; i < cardinality; i++) {
+                nextSetBit = bitSet.nextSetBit(nextSetBit + 1);
+                assertEquals(array[i], nextSetBit);
+            }
+        }
+    }
+
+}