@@ -90,7 +90,12 @@ void preconditionCheck() {
90
90
91
91
assume (elementData .end >= 0 );
92
92
// we can create a stream for an array using Stream.of
93
- assume (elementData .end <= HARD_MAX_ARRAY_SIZE );
93
+ assumeOrExecuteConcretely (elementData .end <= HARD_MAX_ARRAY_SIZE );
94
+
95
+ // As real primitive streams contain primitives, we cannot accept nulls.
96
+ for (int i = 0 ; i < elementData .end ; i ++) {
97
+ assume (elementData .get (i ) != null );
98
+ }
94
99
95
100
visit (this );
96
101
}
@@ -290,17 +295,13 @@ public DoubleStream skip(long n) {
290
295
}
291
296
292
297
int curSize = elementData .end ;
293
- if (n > curSize ) {
298
+ if (n >= curSize ) {
294
299
return new UtDoubleStream ();
295
300
}
296
301
297
- // n is 1 ...Integer.MAX_VALUE here
302
+ // n is 0 ...( Integer.MAX_VALUE - 1) here
298
303
int newSize = (int ) (curSize - n );
299
304
300
- if (newSize == 0 ) {
301
- return new UtDoubleStream ();
302
- }
303
-
304
305
Double [] elements = elementData .toCastedArray ((int ) n , newSize );
305
306
306
307
return new UtDoubleStream (elements , newSize );
@@ -353,17 +354,13 @@ public OptionalDouble reduce(DoubleBinaryOperator op) {
353
354
return OptionalDouble .empty ();
354
355
}
355
356
356
- Double result = null ;
357
- for (int i = 0 ; i < size ; i ++) {
357
+ double result = elementData . get ( 0 ) ;
358
+ for (int i = 1 ; i < size ; i ++) {
358
359
double element = elementData .get (i );
359
- if (result == null ) {
360
- result = element ;
361
- } else {
362
- result = op .applyAsDouble (result , element );
363
- }
360
+ result = op .applyAsDouble (result , element );
364
361
}
365
362
366
- return result == null ? OptionalDouble . empty () : OptionalDouble .of (result );
363
+ return OptionalDouble .of (result );
367
364
}
368
365
369
366
@ Override
@@ -391,16 +388,38 @@ public double sum() {
391
388
}
392
389
393
390
double sum = 0 ;
391
+ boolean anyNaN = false ;
392
+ boolean anyPositiveInfinity = false ;
393
+ boolean anyNegativeInfinity = false ;
394
394
395
395
for (int i = 0 ; i < size ; i ++) {
396
396
double element = elementData .get (i );
397
397
sum += element ;
398
+
399
+ anyNaN |= Double .isNaN (element );
400
+ anyPositiveInfinity |= element == Double .POSITIVE_INFINITY ;
401
+ anyNegativeInfinity |= element == Double .NEGATIVE_INFINITY ;
402
+ }
403
+
404
+ if (anyNaN ) {
405
+ return Double .NaN ;
406
+ }
407
+
408
+ if (anyPositiveInfinity && anyNegativeInfinity ) {
409
+ return Double .NaN ;
410
+ }
411
+
412
+ if (anyPositiveInfinity && sum == Double .NEGATIVE_INFINITY ) {
413
+ return Double .NaN ;
414
+ }
415
+
416
+ if (anyNegativeInfinity && sum == Double .POSITIVE_INFINITY ) {
417
+ return Double .NaN ;
398
418
}
399
419
400
420
return sum ;
401
421
}
402
422
403
- @ SuppressWarnings ("ManualMinMaxCalculation" )
404
423
@ Override
405
424
public OptionalDouble min () {
406
425
preconditionCheckWithClosingStream ();
@@ -413,13 +432,12 @@ public OptionalDouble min() {
413
432
double min = elementData .get (0 );
414
433
for (int i = 1 ; i < size ; i ++) {
415
434
final double element = elementData .get (i );
416
- min = (element < min ) ? element : min ;
435
+ min = Math . min (element , min );
417
436
}
418
437
419
438
return OptionalDouble .of (min );
420
439
}
421
440
422
- @ SuppressWarnings ("ManualMinMaxCalculation" )
423
441
@ Override
424
442
public OptionalDouble max () {
425
443
preconditionCheckWithClosingStream ();
@@ -432,7 +450,7 @@ public OptionalDouble max() {
432
450
double max = elementData .get (0 );
433
451
for (int i = 1 ; i < size ; i ++) {
434
452
final double element = elementData .get (i );
435
- max = (element > max ) ? element : max ;
453
+ max = Math . max (element , max );
436
454
}
437
455
438
456
return OptionalDouble .of (max );
@@ -629,7 +647,7 @@ public void close() {
629
647
closeHandlers .get (i ).run ();
630
648
}
631
649
632
- // clear handlers
650
+ // clear handlers (we do not need to manually clear all elements)
633
651
closeHandlers .end = 0 ;
634
652
}
635
653
0 commit comments