java.util.PrimitiveIterator Java Examples

The following examples show how to use java.util.PrimitiveIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestTypedHeap.java    From presto with Apache License 2.0 6 votes vote down vote up
private static void test(IntStream inputStream, BlockComparator comparator, PrimitiveIterator.OfInt outputIterator)
{
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, INPUT_SIZE);
    inputStream.forEach(x -> BIGINT.writeLong(blockBuilder, x));

    TypedHeap heap = new TypedHeap(comparator, BIGINT, OUTPUT_SIZE);
    heap.addAll(blockBuilder);

    BlockBuilder resultBlockBuilder = BIGINT.createBlockBuilder(null, OUTPUT_SIZE);
    heap.popAll(resultBlockBuilder);

    Block resultBlock = resultBlockBuilder.build();
    assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
    for (int i = 0; i < OUTPUT_SIZE; i++) {
        assertEquals(BIGINT.getLong(resultBlock, i), outputIterator.nextInt());
    }
}
 
Example #2
Source File: LongStream.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #3
Source File: PrimitiveIteratorDefaults.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void testLongForEachRemainingWithNull() {
    PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
        @Override
        public long nextLong() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
}
 
Example #4
Source File: PrimitiveIteratorDefaults.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void testIntForEachRemainingWithNull() {
    PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() {
        @Override
        public int nextInt() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((IntConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null));
}
 
Example #5
Source File: LongStream.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #6
Source File: DoubleStream.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 * will be the provided {@code seed}.  For {@code n > 0}, the element at
 * position {@code n}, will be the result of applying the function {@code f}
 *  to the element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code DoubleStream}
 */
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
        double t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public double nextDouble() {
            double v = t;
            t = f.applyAsDouble(t);
            return v;
        }
    };
    return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #7
Source File: BitSetStreamTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "cases")
public void testBitsetStream(String name, IntStream data) {
    BitSet bs = new BitSet();
    long setBits = data.distinct()
                       .peek(i -> bs.set(i))
                       .count();

    assertEquals(bs.cardinality(), setBits);
    assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));

    PrimitiveIterator.OfInt it = bs.stream().iterator();
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        assertTrue(it.hasNext());
        assertEquals(it.nextInt(), i);
    }
    assertFalse(it.hasNext());
}
 
Example #8
Source File: LongStream.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #9
Source File: IntStream.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #10
Source File: DoubleStream.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 * will be the provided {@code seed}.  For {@code n > 0}, the element at
 * position {@code n}, will be the result of applying the function {@code f}
 *  to the element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code DoubleStream}
 */
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
        double t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public double nextDouble() {
            double v = t;
            t = f.applyAsDouble(t);
            return v;
        }
    };
    return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #11
Source File: PrimitiveIteratorDefaults.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void testIntForEachRemainingWithNull() {
    PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() {
        @Override
        public int nextInt() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((IntConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null));
}
 
Example #12
Source File: LongStream.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #13
Source File: BitSetStreamTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "cases")
public void testBitsetStream(String name, IntStream data) {
    BitSet bs = new BitSet();
    long setBits = data.distinct()
                       .peek(i -> bs.set(i))
                       .count();

    assertEquals(bs.cardinality(), setBits);
    assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));

    PrimitiveIterator.OfInt it = bs.stream().iterator();
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        assertTrue(it.hasNext());
        assertEquals(it.nextInt(), i);
    }
    assertFalse(it.hasNext());
}
 
Example #14
Source File: PrimitiveIteratorDefaults.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void testDoubleForEachRemainingWithNull() {
    PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
        @Override
        public double nextDouble() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
}
 
Example #15
Source File: IntStream.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #16
Source File: IntStream.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #17
Source File: PrimitiveIteratorDefaults.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testDoubleForEachRemainingWithNull() {
    PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
        @Override
        public double nextDouble() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
}
 
Example #18
Source File: PrimitiveIteratorDefaults.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testLongForEachRemainingWithNull() {
    PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
        @Override
        public long nextLong() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
}
 
Example #19
Source File: PrimitiveIteratorDefaults.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void testLongForEachRemainingWithNull() {
    PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
        @Override
        public long nextLong() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
}
 
Example #20
Source File: IntStream.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #21
Source File: DoubleStream.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 * will be the provided {@code seed}.  For {@code n > 0}, the element at
 * position {@code n}, will be the result of applying the function {@code f}
 *  to the element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return a new sequential {@code DoubleStream}
 */
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
        double t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public double nextDouble() {
            double v = t;
            t = f.applyAsDouble(t);
            return v;
        }
    };
    return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #22
Source File: IntStream.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #23
Source File: LongStream.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
Example #24
Source File: BitSetStreamTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "cases")
public void testBitsetStream(String name, IntStream data) {
    BitSet bs = new BitSet();
    long setBits = data.distinct()
                       .peek(i -> bs.set(i))
                       .count();

    assertEquals(bs.cardinality(), setBits);
    assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));

    PrimitiveIterator.OfInt it = bs.stream().iterator();
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        assertTrue(it.hasNext());
        assertEquals(it.nextInt(), i);
    }
    assertFalse(it.hasNext());
}
 
Example #25
Source File: PrimitiveIteratorDefaults.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void testDoubleForEachRemainingWithNull() {
    PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
        @Override
        public double nextDouble() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
}
 
Example #26
Source File: PrimitiveIteratorDefaults.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void testDoubleForEachRemainingWithNull() {
    PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
        @Override
        public double nextDouble() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
}
 
Example #27
Source File: CharSequence.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}
 
Example #28
Source File: IntNodeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private Node.OfInt degenerateTree(PrimitiveIterator.OfInt it) {
    if (!it.hasNext()) {
        return Nodes.node(new int[0]);
    }

    int i = it.nextInt();
    if (it.hasNext()) {
        return new Nodes.ConcNode.OfInt(Nodes.node(new int[] {i}), degenerateTree(it));
    }
    else {
        return Nodes.node(new int[] {i});
    }
}
 
Example #29
Source File: StringLiteral.java    From sylph with Apache License 2.0 5 votes vote down vote up
static String formatStringLiteral(String s)
    {
        s = s.replace("'", "''");
//        if (CharMatcher.inRange((char) 0x20, (char) 0x7E).matchesAllOf(s)) {
//            return "'" + s + "'";
//        }
        if (charMatches((char) 0x20, (char) 0x7E, s)) {
            return "'" + s + "'";
        }

        StringBuilder builder = new StringBuilder();
        builder.append("U&'");
        PrimitiveIterator.OfInt iterator = s.codePoints().iterator();
        while (iterator.hasNext()) {
            int codePoint = iterator.nextInt();
            checkArgument(codePoint >= 0, "Invalid UTF-8 encoding in characters: %s", s);
            if (isAsciiPrintable(codePoint)) {
                char ch = (char) codePoint;
                if (ch == '\\') {
                    builder.append(ch);
                }
                builder.append(ch);
            }
            else if (codePoint <= 0xFFFF) {
                builder.append('\\');
                builder.append(String.format("%04X", codePoint));
            }
            else {
                builder.append("\\+");
                builder.append(String.format("%06X", codePoint));
            }
        }
        builder.append("'");
        return builder.toString();
    }
 
Example #30
Source File: CharSequence.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}