--- old/src/share/classes/java/util/Arrays.java 2013-04-23 11:32:19.032938726 -0700 +++ new/src/share/classes/java/util/Arrays.java 2013-04-23 11:32:18.884938727 -0700 @@ -25,7 +25,21 @@ package java.util; -import java.lang.reflect.*; +import java.lang.reflect.Array; +import java.util.concurrent.ForkJoinPool; +import java.util.function.BinaryOperator; +import java.util.function.DoubleBinaryOperator; +import java.util.function.IntBinaryOperator; +import java.util.function.IntFunction; +import java.util.function.IntToDoubleFunction; +import java.util.function.IntToLongFunction; +import java.util.function.IntUnaryOperator; +import java.util.function.LongBinaryOperator; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import static java.util.ArraysParallelSortHelpers.*; /** @@ -4306,8 +4320,155 @@ dejaVu.remove(a); } + + /** + * Set all elements of the specified array, using the provided + * generator function to compute each element. + * + *

If the generator function throws an exception, it is relayed to + * the caller and the array is left in an indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @param Type of elements of the array + * @throw NullPointerException if the generator is null + */ + public static void setAll(T[] array, IntFunction generator) { + Objects.requireNonNull(generator); + for (int i=0; iIf the generator function throws an exception, an unchecked exception + * is thrown from {@code parallelSetAll} and the array is left in an + * indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @param Type of elements of the array + * @throw NullPointerException if the generator is null + */ + public static void parallelSetAll(T[] array, IntFunction generator) { + Objects.requireNonNull(generator); + IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); }); + } + + /** + * Set all elements of the specified array, using the provided + * generator function to compute each element. + * + *

If the generator function throws an exception, it is relayed to + * the caller and the array is left in an indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @throw NullPointerException if the generator is null + */ + public static void setAll(int[] array, IntUnaryOperator generator) { + Objects.requireNonNull(generator); + for (int i=0; iIf the generator function throws an exception, an unchecked exception + * is thrown from {@code parallelSetAll} and the array is left in an + * indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @throw NullPointerException if the generator is null + */ + public static void parallelSetAll(int[] array, IntUnaryOperator generator) { + Objects.requireNonNull(generator); + IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); }); + } + + /** + * Set all elements of the specified array, using the provided + * generator function to compute each element. + * + *

If the generator function throws an exception, it is relayed to + * the caller and the array is left in an indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @throw NullPointerException if the generator is null + */ + public static void setAll(long[] array, IntToLongFunction generator) { + Objects.requireNonNull(generator); + for (int i=0; iIf the generator function throws an exception, an unchecked exception + * is thrown from {@code parallelSetAll} and the array is left in an + * indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @throw NullPointerException if the generator is null + */ + public static void parallelSetAll(long[] array, IntToLongFunction generator) { + Objects.requireNonNull(generator); + IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); }); + } + + /** + * Set all elements of the specified array, using the provided + * generator function to compute each element. + * + *

If the generator function throws an exception, it is relayed to + * the caller and the array is left in an indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @throw NullPointerException if the generator is null + */ + public static void setAll(double[] array, IntToDoubleFunction generator) { + Objects.requireNonNull(generator); + for (int i=0; iIf the generator function throws an exception, an unchecked exception + * is thrown from {@code parallelSetAll} and the array is left in an + * indeterminate state. + * + * @param array Array to be initialized + * @param generator Function accepting an index and producing the desired + * value for that position + * @throw NullPointerException if the generator is null + */ + public static void parallelSetAll(double[] array, IntToDoubleFunction generator) { + Objects.requireNonNull(generator); + IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); }); + } + + /** + * Returns a {@link Spliterator} covering all of the specified array. * *

The spliterator reports {@link Spliterator#SIZED}, * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and @@ -4315,7 +4476,7 @@ * * @param Type of elements * @param array The array, assumed to be unmodified during use - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @since 1.8 */ @@ -4325,7 +4486,7 @@ } /** - * Creates a {@link Spliterator} covering the specified range of the + * Returns a {@link Spliterator} covering the specified range of the * specified array. * *

The spliterator reports {@link Spliterator#SIZED}, @@ -4336,7 +4497,7 @@ * @param array The array, assumed to be unmodified during use * @param fromIndex The least index (inclusive) to cover * @param toIndex One past the greatest index to cover - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, * {@code toIndex} is less than {@code fromIndex}, or @@ -4349,14 +4510,14 @@ } /** - * Creates a {@link Spliterator.OfInt} covering all of the specified array. + * Returns a {@link Spliterator.OfInt} covering all of the specified array. * *

The spliterator reports {@link Spliterator#SIZED}, * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and * {@link Spliterator#IMMUTABLE}. * * @param array The array, assumed to be unmodified during use - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @since 1.8 */ @@ -4366,7 +4527,7 @@ } /** - * Creates a {@link Spliterator.OfInt} covering the specified range of the + * Returns a {@link Spliterator.OfInt} covering the specified range of the * specified array. * *

The spliterator reports {@link Spliterator#SIZED}, @@ -4376,7 +4537,7 @@ * @param array The array, assumed to be unmodified during use * @param fromIndex The least index (inclusive) to cover * @param toIndex One past the greatest index to cover - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, * {@code toIndex} is less than {@code fromIndex}, or @@ -4389,14 +4550,14 @@ } /** - * Creates a {@link Spliterator.OfLong} covering all of the specified array. + * Returns a {@link Spliterator.OfLong} covering all of the specified array. * *

The spliterator reports {@link Spliterator#SIZED}, * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and * {@link Spliterator#IMMUTABLE}. * * @param array The array, assumed to be unmodified during use - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @since 1.8 */ @@ -4406,7 +4567,7 @@ } /** - * Creates a {@link Spliterator.OfLong} covering the specified range of the + * Returns a {@link Spliterator.OfLong} covering the specified range of the * specified array. * *

The spliterator reports {@link Spliterator#SIZED}, @@ -4416,7 +4577,7 @@ * @param array The array, assumed to be unmodified during use * @param fromIndex The least index (inclusive) to cover * @param toIndex One past the greatest index to cover - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, * {@code toIndex} is less than {@code fromIndex}, or @@ -4429,7 +4590,7 @@ } /** - * Creates a {@link Spliterator.OfDouble} covering all of the specified + * Returns a {@link Spliterator.OfDouble} covering all of the specified * array. * *

The spliterator reports {@link Spliterator#SIZED}, @@ -4437,7 +4598,7 @@ * {@link Spliterator#IMMUTABLE}. * * @param array The array, assumed to be unmodified during use - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @since 1.8 */ @@ -4447,7 +4608,7 @@ } /** - * Creates a {@link Spliterator.OfDouble} covering the specified range of + * Returns a {@link Spliterator.OfDouble} covering the specified range of * the specified array. * *

The spliterator reports {@link Spliterator#SIZED}, @@ -4457,7 +4618,7 @@ * @param array The array, assumed to be unmodified during use * @param fromIndex The least index (inclusive) to cover * @param toIndex One past the greatest index to cover - * @return A spliterator from the array + * @return the spliterator for the array elements * @throws NullPointerException if the specified array is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, * {@code toIndex} is less than {@code fromIndex}, or @@ -4468,4 +4629,134 @@ return Spliterators.spliterator(array, fromIndex, toIndex, Spliterator.ORDERED | Spliterator.IMMUTABLE); } + + /** + * Returns a sequential {@link Stream} with the specified array as its + * source. + * + * @param The type of the array elements + * @param array The array, assumed to be unmodified during use + * @return A {@code Stream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @since 1.8 + */ + public static Stream stream(T[] array) { + return stream(array, 0, array.length); + } + + /** + * Returns a sequential {@link Stream} with specified range of the + * specified array as its source. + * + * @param The type of the array elements + * @param array The array, assumed to be unmodified during use + * @param fromIndex The index of the first element (inclusive) to be + * encountered + * @param toIndex One past the index of the last element to be encountered + * @return A {@code Stream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, + * {@code toIndex} is less than {@code fromIndex}, or + * {@code toIndex} is greater than the array size + * @since 1.8 + */ + public static Stream stream(T[] array, int fromIndex, int toIndex) { + return StreamSupport.stream(spliterator(array, fromIndex, toIndex)); + } + + /** + * Returns a sequential {@link IntStream} with the specified array as its + * source. + * + * @param array The array, assumed to be unmodified during use + * @return An {@code IntStream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @since 1.8 + */ + public static IntStream stream(int[] array) { + return stream(array, 0, array.length); + } + + /** + * Returns a sequential {@link IntStream} with specified range of the + * specified array as its source. + * + * @param array The array, assumed to be unmodified during use + * @param fromIndex The index of the first element (inclusive) to be + * encountered + * @param toIndex One past the index of the last element to be encountered + * @return An {@code IntStream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, + * {@code toIndex} is less than {@code fromIndex}, or + * {@code toIndex} is greater than the array size + * @since 1.8 + */ + public static IntStream stream(int[] array, int fromIndex, int toIndex) { + return StreamSupport.intStream(spliterator(array, fromIndex, toIndex)); + } + + /** + * Returns a sequential {@link LongStream} with the specified array as its + * source. + * + * @param array The array, assumed to be unmodified during use + * @return A {@code LongStream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @since 1.8 + */ + public static LongStream stream(long[] array) { + return stream(array, 0, array.length); + } + + /** + * Returns a sequential {@link LongStream} with specified range of the + * specified array as its source. + * + * @param array The array, assumed to be unmodified during use + * @param fromIndex The index of the first element (inclusive) to be + * encountered + * @param toIndex One past the index of the last element to be encountered + * @return A {@code LongStream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, + * {@code toIndex} is less than {@code fromIndex}, or + * {@code toIndex} is greater than the array size + * @since 1.8 + */ + public static LongStream stream(long[] array, int fromIndex, int toIndex) { + return StreamSupport.longStream(spliterator(array, fromIndex, toIndex)); + } + + /** + * Returns a sequential {@link DoubleStream} with the specified array as its + * source. + * + * @param array The array, assumed to be unmodified during use + * @return A {@code DoubleStream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @since 1.8 + */ + public static DoubleStream stream(double[] array) { + return stream(array, 0, array.length); + } + + /** + * Returns a sequential {@link DoubleStream} with specified range of the + * specified array as its source. + * + * @param array The array, assumed to be unmodified during use + * @param fromIndex The index of the first element (inclusive) to be + * encountered + * @param toIndex One past the index of the last element to be encountered + * @return A {@code DoubleStream} from the array + * @throws NullPointerException if the specified array is {@code null} + * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative, + * {@code toIndex} is less than {@code fromIndex}, or + * {@code toIndex} is greater than the array size + * @since 1.8 + */ + public static DoubleStream stream(double[] array, int fromIndex, int toIndex) { + return StreamSupport.doubleStream(spliterator(array, fromIndex, toIndex)); + } }