8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.lang.reflect.*;
29 import static java.util.ArraysParallelSortHelpers.*;
30
31 /**
32 * This class contains various methods for manipulating arrays (such as
33 * sorting and searching). This class also contains a static factory
34 * that allows arrays to be viewed as lists.
35 *
36 * <p>The methods in this class all throw a {@code NullPointerException},
37 * if the specified array reference is null, except where noted.
38 *
39 * <p>The documentation for the methods contained in this class includes
40 * briefs description of the <i>implementations</i>. Such descriptions should
41 * be regarded as <i>implementation notes</i>, rather than parts of the
42 * <i>specification</i>. Implementors should feel free to substitute other
43 * algorithms, so long as the specification itself is adhered to. (For
44 * example, the algorithm used by {@code sort(Object[])} does not have to be
45 * a MergeSort, but it does have to be <i>stable</i>.)
46 *
47 * <p>This class is a member of the
48 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
4289 else if (eClass == boolean[].class)
4290 buf.append(toString((boolean[]) element));
4291 else { // element is an array of object references
4292 if (dejaVu.contains(element))
4293 buf.append("[...]");
4294 else
4295 deepToString((Object[])element, buf, dejaVu);
4296 }
4297 } else { // element is non-null and not an array
4298 buf.append(element.toString());
4299 }
4300 }
4301 if (i == iMax)
4302 break;
4303 buf.append(", ");
4304 }
4305 buf.append(']');
4306 dejaVu.remove(a);
4307 }
4308
4309 /**
4310 * Creates a {@link Spliterator} covering all of the specified array.
4311 *
4312 * <p>The spliterator reports {@link Spliterator#SIZED},
4313 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4314 * {@link Spliterator#IMMUTABLE}.
4315 *
4316 * @param <T> Type of elements
4317 * @param array The array, assumed to be unmodified during use
4318 * @return A spliterator from the array
4319 * @throws NullPointerException if the specified array is {@code null}
4320 * @since 1.8
4321 */
4322 public static <T> Spliterator<T> spliterator(T[] array) {
4323 return Spliterators.spliterator(array,
4324 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4325 }
4326
4327 /**
4328 * Creates a {@link Spliterator} covering the specified range of the
4329 * specified array.
4330 *
4331 * <p>The spliterator reports {@link Spliterator#SIZED},
4332 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4333 * {@link Spliterator#IMMUTABLE}.
4334 *
4335 * @param <T> Type of elements
4336 * @param array The array, assumed to be unmodified during use
4337 * @param fromIndex The least index (inclusive) to cover
4338 * @param toIndex One past the greatest index to cover
4339 * @return A spliterator from the array
4340 * @throws NullPointerException if the specified array is {@code null}
4341 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4342 * {@code toIndex} is less than {@code fromIndex}, or
4343 * {@code toIndex} is greater than the array size
4344 * @since 1.8
4345 */
4346 public static <T> Spliterator<T> spliterator(T[] array, int fromIndex, int toIndex) {
4347 return Spliterators.spliterator(array, fromIndex, toIndex,
4348 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4349 }
4350
4351 /**
4352 * Creates a {@link Spliterator.OfInt} covering all of the specified array.
4353 *
4354 * <p>The spliterator reports {@link Spliterator#SIZED},
4355 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4356 * {@link Spliterator#IMMUTABLE}.
4357 *
4358 * @param array The array, assumed to be unmodified during use
4359 * @return A spliterator from the array
4360 * @throws NullPointerException if the specified array is {@code null}
4361 * @since 1.8
4362 */
4363 public static Spliterator.OfInt spliterator(int[] array) {
4364 return Spliterators.spliterator(array,
4365 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4366 }
4367
4368 /**
4369 * Creates a {@link Spliterator.OfInt} covering the specified range of the
4370 * specified array.
4371 *
4372 * <p>The spliterator reports {@link Spliterator#SIZED},
4373 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4374 * {@link Spliterator#IMMUTABLE}.
4375 *
4376 * @param array The array, assumed to be unmodified during use
4377 * @param fromIndex The least index (inclusive) to cover
4378 * @param toIndex One past the greatest index to cover
4379 * @return A spliterator from the array
4380 * @throws NullPointerException if the specified array is {@code null}
4381 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4382 * {@code toIndex} is less than {@code fromIndex}, or
4383 * {@code toIndex} is greater than the array size
4384 * @since 1.8
4385 */
4386 public static Spliterator.OfInt spliterator(int[] array, int fromIndex, int toIndex) {
4387 return Spliterators.spliterator(array, fromIndex, toIndex,
4388 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4389 }
4390
4391 /**
4392 * Creates a {@link Spliterator.OfLong} covering all of the specified array.
4393 *
4394 * <p>The spliterator reports {@link Spliterator#SIZED},
4395 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4396 * {@link Spliterator#IMMUTABLE}.
4397 *
4398 * @param array The array, assumed to be unmodified during use
4399 * @return A spliterator from the array
4400 * @throws NullPointerException if the specified array is {@code null}
4401 * @since 1.8
4402 */
4403 public static Spliterator.OfLong spliterator(long[] array) {
4404 return Spliterators.spliterator(array,
4405 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4406 }
4407
4408 /**
4409 * Creates a {@link Spliterator.OfLong} covering the specified range of the
4410 * specified array.
4411 *
4412 * <p>The spliterator reports {@link Spliterator#SIZED},
4413 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4414 * {@link Spliterator#IMMUTABLE}.
4415 *
4416 * @param array The array, assumed to be unmodified during use
4417 * @param fromIndex The least index (inclusive) to cover
4418 * @param toIndex One past the greatest index to cover
4419 * @return A spliterator from the array
4420 * @throws NullPointerException if the specified array is {@code null}
4421 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4422 * {@code toIndex} is less than {@code fromIndex}, or
4423 * {@code toIndex} is greater than the array size
4424 * @since 1.8
4425 */
4426 public static Spliterator.OfLong spliterator(long[] array, int fromIndex, int toIndex) {
4427 return Spliterators.spliterator(array, fromIndex, toIndex,
4428 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4429 }
4430
4431 /**
4432 * Creates a {@link Spliterator.OfDouble} covering all of the specified
4433 * array.
4434 *
4435 * <p>The spliterator reports {@link Spliterator#SIZED},
4436 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4437 * {@link Spliterator#IMMUTABLE}.
4438 *
4439 * @param array The array, assumed to be unmodified during use
4440 * @return A spliterator from the array
4441 * @throws NullPointerException if the specified array is {@code null}
4442 * @since 1.8
4443 */
4444 public static Spliterator.OfDouble spliterator(double[] array) {
4445 return Spliterators.spliterator(array,
4446 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4447 }
4448
4449 /**
4450 * Creates a {@link Spliterator.OfDouble} covering the specified range of
4451 * the specified array.
4452 *
4453 * <p>The spliterator reports {@link Spliterator#SIZED},
4454 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4455 * {@link Spliterator#IMMUTABLE}.
4456 *
4457 * @param array The array, assumed to be unmodified during use
4458 * @param fromIndex The least index (inclusive) to cover
4459 * @param toIndex One past the greatest index to cover
4460 * @return A spliterator from the array
4461 * @throws NullPointerException if the specified array is {@code null}
4462 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4463 * {@code toIndex} is less than {@code fromIndex}, or
4464 * {@code toIndex} is greater than the array size
4465 * @since 1.8
4466 */
4467 public static Spliterator.OfDouble spliterator(double[] array, int fromIndex, int toIndex) {
4468 return Spliterators.spliterator(array, fromIndex, toIndex,
4469 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4470 }
4471 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.lang.reflect.Array;
29 import java.util.concurrent.ForkJoinPool;
30 import java.util.function.BinaryOperator;
31 import java.util.function.DoubleBinaryOperator;
32 import java.util.function.IntBinaryOperator;
33 import java.util.function.IntFunction;
34 import java.util.function.IntToDoubleFunction;
35 import java.util.function.IntToLongFunction;
36 import java.util.function.IntUnaryOperator;
37 import java.util.function.LongBinaryOperator;
38 import java.util.stream.DoubleStream;
39 import java.util.stream.IntStream;
40 import java.util.stream.LongStream;
41 import java.util.stream.Stream;
42 import java.util.stream.StreamSupport;
43 import static java.util.ArraysParallelSortHelpers.*;
44
45 /**
46 * This class contains various methods for manipulating arrays (such as
47 * sorting and searching). This class also contains a static factory
48 * that allows arrays to be viewed as lists.
49 *
50 * <p>The methods in this class all throw a {@code NullPointerException},
51 * if the specified array reference is null, except where noted.
52 *
53 * <p>The documentation for the methods contained in this class includes
54 * briefs description of the <i>implementations</i>. Such descriptions should
55 * be regarded as <i>implementation notes</i>, rather than parts of the
56 * <i>specification</i>. Implementors should feel free to substitute other
57 * algorithms, so long as the specification itself is adhered to. (For
58 * example, the algorithm used by {@code sort(Object[])} does not have to be
59 * a MergeSort, but it does have to be <i>stable</i>.)
60 *
61 * <p>This class is a member of the
62 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
4303 else if (eClass == boolean[].class)
4304 buf.append(toString((boolean[]) element));
4305 else { // element is an array of object references
4306 if (dejaVu.contains(element))
4307 buf.append("[...]");
4308 else
4309 deepToString((Object[])element, buf, dejaVu);
4310 }
4311 } else { // element is non-null and not an array
4312 buf.append(element.toString());
4313 }
4314 }
4315 if (i == iMax)
4316 break;
4317 buf.append(", ");
4318 }
4319 buf.append(']');
4320 dejaVu.remove(a);
4321 }
4322
4323
4324 /**
4325 * Set all elements of the specified array, using the provided
4326 * generator function to compute each element.
4327 *
4328 * <p>If the generator function throws an exception, it is relayed to
4329 * the caller and the array is left in an indeterminate state.
4330 *
4331 * @param array Array to be initialized
4332 * @param generator Function accepting an index and producing the desired
4333 * value for that position
4334 * @param <T> Type of elements of the array
4335 * @throw NullPointerException if the generator is null
4336 */
4337 public static<T> void setAll(T[] array, IntFunction<? extends T> generator) {
4338 Objects.requireNonNull(generator);
4339 for (int i=0; i<array.length; i++)
4340 array[i] = generator.apply(i);
4341 }
4342
4343 /**
4344 * Set all elements of the specified array, in parallel, using the
4345 * provided generator function to compute each element.
4346 *
4347 * <p>If the generator function throws an exception, an unchecked exception
4348 * is thrown from {@code parallelSetAll} and the array is left in an
4349 * indeterminate state.
4350 *
4351 * @param array Array to be initialized
4352 * @param generator Function accepting an index and producing the desired
4353 * value for that position
4354 * @param <T> Type of elements of the array
4355 * @throw NullPointerException if the generator is null
4356 */
4357 public static<T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
4358 Objects.requireNonNull(generator);
4359 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
4360 }
4361
4362 /**
4363 * Set all elements of the specified array, using the provided
4364 * generator function to compute each element.
4365 *
4366 * <p>If the generator function throws an exception, it is relayed to
4367 * the caller and the array is left in an indeterminate state.
4368 *
4369 * @param array Array to be initialized
4370 * @param generator Function accepting an index and producing the desired
4371 * value for that position
4372 * @throw NullPointerException if the generator is null
4373 */
4374 public static void setAll(int[] array, IntUnaryOperator generator) {
4375 Objects.requireNonNull(generator);
4376 for (int i=0; i<array.length; i++)
4377 array[i] = generator.applyAsInt(i);
4378 }
4379
4380 /**
4381 * Set all elements of the specified array, in parallel, using the
4382 * provided generator function to compute each element.
4383 *
4384 * <p>If the generator function throws an exception, an unchecked exception
4385 * is thrown from {@code parallelSetAll} and the array is left in an
4386 * indeterminate state.
4387 *
4388 * @param array Array to be initialized
4389 * @param generator Function accepting an index and producing the desired
4390 * value for that position
4391 * @throw NullPointerException if the generator is null
4392 */
4393 public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
4394 Objects.requireNonNull(generator);
4395 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
4396 }
4397
4398 /**
4399 * Set all elements of the specified array, using the provided
4400 * generator function to compute each element.
4401 *
4402 * <p>If the generator function throws an exception, it is relayed to
4403 * the caller and the array is left in an indeterminate state.
4404 *
4405 * @param array Array to be initialized
4406 * @param generator Function accepting an index and producing the desired
4407 * value for that position
4408 * @throw NullPointerException if the generator is null
4409 */
4410 public static void setAll(long[] array, IntToLongFunction generator) {
4411 Objects.requireNonNull(generator);
4412 for (int i=0; i<array.length; i++)
4413 array[i] = generator.applyAsLong(i);
4414 }
4415
4416 /**
4417 * Set all elements of the specified array, in parallel, using the
4418 * provided generator function to compute each element.
4419 *
4420 * <p>If the generator function throws an exception, an unchecked exception
4421 * is thrown from {@code parallelSetAll} and the array is left in an
4422 * indeterminate state.
4423 *
4424 * @param array Array to be initialized
4425 * @param generator Function accepting an index and producing the desired
4426 * value for that position
4427 * @throw NullPointerException if the generator is null
4428 */
4429 public static void parallelSetAll(long[] array, IntToLongFunction generator) {
4430 Objects.requireNonNull(generator);
4431 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
4432 }
4433
4434 /**
4435 * Set all elements of the specified array, using the provided
4436 * generator function to compute each element.
4437 *
4438 * <p>If the generator function throws an exception, it is relayed to
4439 * the caller and the array is left in an indeterminate state.
4440 *
4441 * @param array Array to be initialized
4442 * @param generator Function accepting an index and producing the desired
4443 * value for that position
4444 * @throw NullPointerException if the generator is null
4445 */
4446 public static void setAll(double[] array, IntToDoubleFunction generator) {
4447 Objects.requireNonNull(generator);
4448 for (int i=0; i<array.length; i++)
4449 array[i] = generator.applyAsDouble(i);
4450 }
4451
4452 /**
4453 * Set all elements of the specified array, in parallel, using the
4454 * provided generator function to compute each element.
4455 *
4456 * <p>If the generator function throws an exception, an unchecked exception
4457 * is thrown from {@code parallelSetAll} and the array is left in an
4458 * indeterminate state.
4459 *
4460 * @param array Array to be initialized
4461 * @param generator Function accepting an index and producing the desired
4462 * value for that position
4463 * @throw NullPointerException if the generator is null
4464 */
4465 public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
4466 Objects.requireNonNull(generator);
4467 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
4468 }
4469
4470 /**
4471 * Returns a {@link Spliterator} covering all of the specified array.
4472 *
4473 * <p>The spliterator reports {@link Spliterator#SIZED},
4474 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4475 * {@link Spliterator#IMMUTABLE}.
4476 *
4477 * @param <T> Type of elements
4478 * @param array The array, assumed to be unmodified during use
4479 * @return the spliterator for the array elements
4480 * @throws NullPointerException if the specified array is {@code null}
4481 * @since 1.8
4482 */
4483 public static <T> Spliterator<T> spliterator(T[] array) {
4484 return Spliterators.spliterator(array,
4485 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4486 }
4487
4488 /**
4489 * Returns a {@link Spliterator} covering the specified range of the
4490 * specified array.
4491 *
4492 * <p>The spliterator reports {@link Spliterator#SIZED},
4493 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4494 * {@link Spliterator#IMMUTABLE}.
4495 *
4496 * @param <T> Type of elements
4497 * @param array The array, assumed to be unmodified during use
4498 * @param fromIndex The least index (inclusive) to cover
4499 * @param toIndex One past the greatest index to cover
4500 * @return the spliterator for the array elements
4501 * @throws NullPointerException if the specified array is {@code null}
4502 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4503 * {@code toIndex} is less than {@code fromIndex}, or
4504 * {@code toIndex} is greater than the array size
4505 * @since 1.8
4506 */
4507 public static <T> Spliterator<T> spliterator(T[] array, int fromIndex, int toIndex) {
4508 return Spliterators.spliterator(array, fromIndex, toIndex,
4509 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4510 }
4511
4512 /**
4513 * Returns a {@link Spliterator.OfInt} covering all of the specified array.
4514 *
4515 * <p>The spliterator reports {@link Spliterator#SIZED},
4516 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4517 * {@link Spliterator#IMMUTABLE}.
4518 *
4519 * @param array The array, assumed to be unmodified during use
4520 * @return the spliterator for the array elements
4521 * @throws NullPointerException if the specified array is {@code null}
4522 * @since 1.8
4523 */
4524 public static Spliterator.OfInt spliterator(int[] array) {
4525 return Spliterators.spliterator(array,
4526 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4527 }
4528
4529 /**
4530 * Returns a {@link Spliterator.OfInt} covering the specified range of the
4531 * specified array.
4532 *
4533 * <p>The spliterator reports {@link Spliterator#SIZED},
4534 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4535 * {@link Spliterator#IMMUTABLE}.
4536 *
4537 * @param array The array, assumed to be unmodified during use
4538 * @param fromIndex The least index (inclusive) to cover
4539 * @param toIndex One past the greatest index to cover
4540 * @return the spliterator for the array elements
4541 * @throws NullPointerException if the specified array is {@code null}
4542 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4543 * {@code toIndex} is less than {@code fromIndex}, or
4544 * {@code toIndex} is greater than the array size
4545 * @since 1.8
4546 */
4547 public static Spliterator.OfInt spliterator(int[] array, int fromIndex, int toIndex) {
4548 return Spliterators.spliterator(array, fromIndex, toIndex,
4549 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4550 }
4551
4552 /**
4553 * Returns a {@link Spliterator.OfLong} covering all of the specified array.
4554 *
4555 * <p>The spliterator reports {@link Spliterator#SIZED},
4556 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4557 * {@link Spliterator#IMMUTABLE}.
4558 *
4559 * @param array The array, assumed to be unmodified during use
4560 * @return the spliterator for the array elements
4561 * @throws NullPointerException if the specified array is {@code null}
4562 * @since 1.8
4563 */
4564 public static Spliterator.OfLong spliterator(long[] array) {
4565 return Spliterators.spliterator(array,
4566 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4567 }
4568
4569 /**
4570 * Returns a {@link Spliterator.OfLong} covering the specified range of the
4571 * specified array.
4572 *
4573 * <p>The spliterator reports {@link Spliterator#SIZED},
4574 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4575 * {@link Spliterator#IMMUTABLE}.
4576 *
4577 * @param array The array, assumed to be unmodified during use
4578 * @param fromIndex The least index (inclusive) to cover
4579 * @param toIndex One past the greatest index to cover
4580 * @return the spliterator for the array elements
4581 * @throws NullPointerException if the specified array is {@code null}
4582 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4583 * {@code toIndex} is less than {@code fromIndex}, or
4584 * {@code toIndex} is greater than the array size
4585 * @since 1.8
4586 */
4587 public static Spliterator.OfLong spliterator(long[] array, int fromIndex, int toIndex) {
4588 return Spliterators.spliterator(array, fromIndex, toIndex,
4589 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4590 }
4591
4592 /**
4593 * Returns a {@link Spliterator.OfDouble} covering all of the specified
4594 * array.
4595 *
4596 * <p>The spliterator reports {@link Spliterator#SIZED},
4597 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4598 * {@link Spliterator#IMMUTABLE}.
4599 *
4600 * @param array The array, assumed to be unmodified during use
4601 * @return the spliterator for the array elements
4602 * @throws NullPointerException if the specified array is {@code null}
4603 * @since 1.8
4604 */
4605 public static Spliterator.OfDouble spliterator(double[] array) {
4606 return Spliterators.spliterator(array,
4607 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4608 }
4609
4610 /**
4611 * Returns a {@link Spliterator.OfDouble} covering the specified range of
4612 * the specified array.
4613 *
4614 * <p>The spliterator reports {@link Spliterator#SIZED},
4615 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
4616 * {@link Spliterator#IMMUTABLE}.
4617 *
4618 * @param array The array, assumed to be unmodified during use
4619 * @param fromIndex The least index (inclusive) to cover
4620 * @param toIndex One past the greatest index to cover
4621 * @return the spliterator for the array elements
4622 * @throws NullPointerException if the specified array is {@code null}
4623 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4624 * {@code toIndex} is less than {@code fromIndex}, or
4625 * {@code toIndex} is greater than the array size
4626 * @since 1.8
4627 */
4628 public static Spliterator.OfDouble spliterator(double[] array, int fromIndex, int toIndex) {
4629 return Spliterators.spliterator(array, fromIndex, toIndex,
4630 Spliterator.ORDERED | Spliterator.IMMUTABLE);
4631 }
4632
4633 /**
4634 * Returns a sequential {@link Stream} with the specified array as its
4635 * source.
4636 *
4637 * @param <T> The type of the array elements
4638 * @param array The array, assumed to be unmodified during use
4639 * @return A {@code Stream} from the array
4640 * @throws NullPointerException if the specified array is {@code null}
4641 * @since 1.8
4642 */
4643 public static <T> Stream<T> stream(T[] array) {
4644 return stream(array, 0, array.length);
4645 }
4646
4647 /**
4648 * Returns a sequential {@link Stream} with specified range of the
4649 * specified array as its source.
4650 *
4651 * @param <T> The type of the array elements
4652 * @param array The array, assumed to be unmodified during use
4653 * @param fromIndex The index of the first element (inclusive) to be
4654 * encountered
4655 * @param toIndex One past the index of the last element to be encountered
4656 * @return A {@code Stream} from the array
4657 * @throws NullPointerException if the specified array is {@code null}
4658 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4659 * {@code toIndex} is less than {@code fromIndex}, or
4660 * {@code toIndex} is greater than the array size
4661 * @since 1.8
4662 */
4663 public static <T> Stream<T> stream(T[] array, int fromIndex, int toIndex) {
4664 return StreamSupport.stream(spliterator(array, fromIndex, toIndex));
4665 }
4666
4667 /**
4668 * Returns a sequential {@link IntStream} with the specified array as its
4669 * source.
4670 *
4671 * @param array The array, assumed to be unmodified during use
4672 * @return An {@code IntStream} from the array
4673 * @throws NullPointerException if the specified array is {@code null}
4674 * @since 1.8
4675 */
4676 public static IntStream stream(int[] array) {
4677 return stream(array, 0, array.length);
4678 }
4679
4680 /**
4681 * Returns a sequential {@link IntStream} with specified range of the
4682 * specified array as its source.
4683 *
4684 * @param array The array, assumed to be unmodified during use
4685 * @param fromIndex The index of the first element (inclusive) to be
4686 * encountered
4687 * @param toIndex One past the index of the last element to be encountered
4688 * @return An {@code IntStream} from the array
4689 * @throws NullPointerException if the specified array is {@code null}
4690 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4691 * {@code toIndex} is less than {@code fromIndex}, or
4692 * {@code toIndex} is greater than the array size
4693 * @since 1.8
4694 */
4695 public static IntStream stream(int[] array, int fromIndex, int toIndex) {
4696 return StreamSupport.intStream(spliterator(array, fromIndex, toIndex));
4697 }
4698
4699 /**
4700 * Returns a sequential {@link LongStream} with the specified array as its
4701 * source.
4702 *
4703 * @param array The array, assumed to be unmodified during use
4704 * @return A {@code LongStream} from the array
4705 * @throws NullPointerException if the specified array is {@code null}
4706 * @since 1.8
4707 */
4708 public static LongStream stream(long[] array) {
4709 return stream(array, 0, array.length);
4710 }
4711
4712 /**
4713 * Returns a sequential {@link LongStream} with specified range of the
4714 * specified array as its source.
4715 *
4716 * @param array The array, assumed to be unmodified during use
4717 * @param fromIndex The index of the first element (inclusive) to be
4718 * encountered
4719 * @param toIndex One past the index of the last element to be encountered
4720 * @return A {@code LongStream} from the array
4721 * @throws NullPointerException if the specified array is {@code null}
4722 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4723 * {@code toIndex} is less than {@code fromIndex}, or
4724 * {@code toIndex} is greater than the array size
4725 * @since 1.8
4726 */
4727 public static LongStream stream(long[] array, int fromIndex, int toIndex) {
4728 return StreamSupport.longStream(spliterator(array, fromIndex, toIndex));
4729 }
4730
4731 /**
4732 * Returns a sequential {@link DoubleStream} with the specified array as its
4733 * source.
4734 *
4735 * @param array The array, assumed to be unmodified during use
4736 * @return A {@code DoubleStream} from the array
4737 * @throws NullPointerException if the specified array is {@code null}
4738 * @since 1.8
4739 */
4740 public static DoubleStream stream(double[] array) {
4741 return stream(array, 0, array.length);
4742 }
4743
4744 /**
4745 * Returns a sequential {@link DoubleStream} with specified range of the
4746 * specified array as its source.
4747 *
4748 * @param array The array, assumed to be unmodified during use
4749 * @param fromIndex The index of the first element (inclusive) to be
4750 * encountered
4751 * @param toIndex One past the index of the last element to be encountered
4752 * @return A {@code DoubleStream} from the array
4753 * @throws NullPointerException if the specified array is {@code null}
4754 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
4755 * {@code toIndex} is less than {@code fromIndex}, or
4756 * {@code toIndex} is greater than the array size
4757 * @since 1.8
4758 */
4759 public static DoubleStream stream(double[] array, int fromIndex, int toIndex) {
4760 return StreamSupport.doubleStream(spliterator(array, fromIndex, toIndex));
4761 }
4762 }
|