diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java b/classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java index 35ab92f0a..41202ae44 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java @@ -15,8 +15,11 @@ */ package org.teavm.classlib.java.util; +import java.util.function.IntPredicate; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.*; +import org.teavm.classlib.java.util.stream.TIntStream; +import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl; import org.teavm.interop.Rename; public class TBitSet extends TObject implements TCloneable, TSerializable { @@ -40,7 +43,7 @@ public class TBitSet extends TObject implements TCloneable, TSerializable { public static TBitSet valueOf(long[] longs) { int[] ints = new int[longs.length * 2]; for (int i = 0; i < longs.length; ++i) { - ints[i * 2 + 1] = (int) longs[i]; + ints[i * 2] = (int) longs[i]; ints[i * 2 + 1] = (int) (longs[i] >>> TInteger.SIZE); } return new TBitSet(ints); @@ -511,6 +514,29 @@ public class TBitSet extends TObject implements TCloneable, TSerializable { return sb.toString(); } + public TIntStream stream() { + return new BitSetStream(); + } + + private class BitSetStream extends TSimpleIntStreamImpl { + private int current; + + private BitSetStream() { + this.current = nextSetBit(0); + } + + @Override + public boolean next(IntPredicate consumer) { + while (current >= 0) { + if (!consumer.test(current)) { + return true; + } + current = nextSetBit(current + 1); + } + return false; + } + } + @Rename("clone") public TObject clone0() { return new TBitSet(TArrays.copyOf(data, data.length)); diff --git a/tests/src/test/java/org/teavm/classlib/java/util/BitSetTest.java b/tests/src/test/java/org/teavm/classlib/java/util/BitSetTest.java index 6962d3d2b..a2067a29a 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/BitSetTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/BitSetTest.java @@ -47,6 +47,7 @@ */ package org.teavm.classlib.java.util; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -98,6 +99,24 @@ public class BitSetTest { } } + @Test + public void constructFromLongs() { + BitSet bs = BitSet.valueOf(new long[] { 7, 2, 5, 1L << 36 }); + assertTrue(bs.get(0) && bs.get(1) && bs.get(2) && bs.get(Long.SIZE + 1) + && bs.get(2 * Long.SIZE) && bs.get(2 * Long.SIZE + 2) && bs.get(3 * Long.SIZE + 36)); + assertFalse(bs.get(3) || bs.get(Long.SIZE + 6) || bs.get(2 * Long.SIZE + 15) || bs.get(3 * Long.SIZE)); + } + + @Test + public void testStream() { + assertArrayEquals(new int[] { 0, 1, 2, Long.SIZE + 1, 2 * Long.SIZE, 2 * Long.SIZE + 2, 3 * Long.SIZE + 36 }, + BitSet.valueOf(new long[] { 7, 2, 5, 1L << 36 }).stream().toArray()); + BitSet bs = new BitSet(); + assertEquals(0, bs.stream().count()); + bs.set(1); + assertArrayEquals(new int[] { 1 }, bs.stream().toArray()); + } + @Test public void clonePerformed() { BitSet bs;