-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Iterators.java
873 lines (798 loc) · 27 KB
/
Iterators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.helpers.collection;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.neo4j.collection.RawIterator;
import org.neo4j.function.Predicates;
import org.neo4j.function.ThrowingFunction;
import org.neo4j.graphdb.Resource;
import org.neo4j.graphdb.ResourceIterable;
import org.neo4j.graphdb.ResourceIterator;
import static java.util.Collections.emptyIterator;
/**
* Contains common functionality regarding {@link Iterator}s and
* {@link Iterable}s.
*/
public final class Iterators
{
private Iterators()
{
throw new AssertionError( "no instance" );
}
/**
* Returns the given iterator's first element or {@code null} if no
* element found.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @return the first element in the {@code iterator}, or {@code null} if no
* element found.
*/
public static <T> T firstOrNull( Iterator<T> iterator )
{
return iterator.hasNext() ? iterator.next() : null;
}
/**
* Returns the given iterator's first element. If no element is found a
* {@link NoSuchElementException} is thrown.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @return the first element in the {@code iterator}.
* @throws NoSuchElementException if no element found.
*/
public static <T> T first( Iterator<T> iterator )
{
return assertNotNull( iterator, firstOrNull( iterator ) );
}
/**
* Returns the given iterator's last element or {@code null} if no
* element found.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @return the last element in the {@code iterator}, or {@code null} if no
* element found.
*/
public static <T> T lastOrNull( Iterator<T> iterator )
{
T result = null;
while ( iterator.hasNext() )
{
result = iterator.next();
}
return result;
}
/**
* Returns the given iterator's last element. If no element is found a
* {@link NoSuchElementException} is thrown.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @return the last element in the {@code iterator}.
* @throws NoSuchElementException if no element found.
*/
public static <T> T last( Iterator<T> iterator )
{
return assertNotNull( iterator, lastOrNull( iterator ) );
}
/**
* Returns the given iterator's single element or {@code null} if no
* element found. If there is more than one element in the iterator a
* {@link NoSuchElementException} will be thrown.
* <p>
* If the {@code iterator} implements {@link Resource} it will be {@link Resource#close() closed}
* in a {@code finally} block after the single item has been retrieved, or failed to be retrieved.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @return the single element in {@code iterator}, or {@code null} if no
* element found.
* @throws NoSuchElementException if more than one element was found.
*/
public static <T> T singleOrNull( Iterator<T> iterator )
{
return single( iterator, null );
}
/**
* Returns the given iterator's single element. If there are no elements
* or more than one element in the iterator a {@link NoSuchElementException}
* will be thrown.
* <p>
* If the {@code iterator} implements {@link Resource} it will be {@link Resource#close() closed}
* in a {@code finally} block after the single item has been retrieved, or failed to be retrieved.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @return the single element in the {@code iterator}.
* @throws NoSuchElementException if there isn't exactly one element.
*/
public static <T> T single( Iterator<T> iterator )
{
return assertNotNull( iterator, singleOrNull( iterator ) );
}
/**
* Returns the iterator's n:th item from the end of the iteration.
* If the iterator has got less than n-1 items in it
* {@link NoSuchElementException} is thrown.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @param n the n:th item from the end to get.
* @return the iterator's n:th item from the end of the iteration.
* @throws NoSuchElementException if the iterator contains less than n-1 items.
*/
public static <T> T fromEnd( Iterator<T> iterator, int n )
{
return assertNotNull( iterator, fromEndOrNull( iterator, n ) );
}
/**
* Returns the iterator's n:th item from the end of the iteration.
* If the iterator has got less than n-1 items in it {@code null} is returned.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @param n the n:th item from the end to get.
* @return the iterator's n:th item from the end of the iteration,
* or {@code null} if the iterator doesn't contain that many items.
*/
public static <T> T fromEndOrNull( Iterator<T> iterator, int n )
{
Deque<T> trail = new ArrayDeque<>( n );
while ( iterator.hasNext() )
{
if ( trail.size() > n )
{
trail.removeLast();
}
trail.addFirst( iterator.next() );
}
return trail.size() == n + 1 ? trail.getLast() : null;
}
/**
* Iterates over the full iterators, and checks equality for each item in them. Note that this
* will consume the iterators.
*
* @param first the first iterator
* @param other the other iterator
* @return {@code true} if all items are equal; otherwise
*/
public static boolean iteratorsEqual( Iterator<?> first, Iterator<?> other )
{
while ( true )
{
if ( first.hasNext() && other.hasNext() )
{
if ( !first.next().equals( other.next() ) )
{
return false;
}
}
else
{
return first.hasNext() == other.hasNext();
}
}
}
private static <T> T assertNotNull( Iterator<T> iterator, T result )
{
if ( result == null )
{
throw new NoSuchElementException( "No element found in " + iterator );
}
return result;
}
/**
* Returns the given iterator's single element or {@code itemIfNone} if no
* element found. If there is more than one element in the iterator a
* {@link NoSuchElementException} will be thrown.
* <p>
* If the {@code iterator} implements {@link Resource} it will be {@link Resource#close() closed}
* in a {@code finally} block after the single item has been retrieved, or failed to be retrieved.
*
* @param <T> the type of elements in {@code iterator}.
* @param iterator the {@link Iterator} to get elements from.
* @param itemIfNone item to use if none is found
* @return the single element in {@code iterator}, or {@code itemIfNone} if no
* element found.
* @throws NoSuchElementException if more than one element was found.
*/
public static <T> T single( Iterator<T> iterator, T itemIfNone )
{
try
{
T result = iterator.hasNext() ? iterator.next() : itemIfNone;
if ( iterator.hasNext() )
{
throw new NoSuchElementException( "More than one element in " + iterator + ". First element is '"
+ result + "' and the second element is '" + iterator.next() + "'" );
}
return result;
}
finally
{
if ( iterator instanceof Resource )
{
((Resource) iterator).close();
}
}
}
/**
* Adds all the items in {@code iterator} to {@code collection}.
*
* @param <C> the type of {@link Collection} to add to items to.
* @param <T> the type of items in the collection and iterator.
* @param iterator the {@link Iterator} to grab the items from.
* @param collection the {@link Collection} to add the items to.
* @return the {@code collection} which was passed in, now filled
* with the items from {@code iterator}.
*/
public static <C extends Collection<T>, T> C addToCollection( Iterator<T> iterator,
C collection )
{
while ( iterator.hasNext() )
{
collection.add( iterator.next() );
}
return collection;
}
/**
* Adds all the items in {@code iterator} to {@code collection}.
*
* @param <C> the type of {@link Collection} to add to items to.
* @param <T> the type of items in the collection and iterator.
* @param iterator the {@link Iterator} to grab the items from.
* @param collection the {@link Collection} to add the items to.
* @return the {@code collection} which was passed in, now filled
* with the items from {@code iterator}.
*/
public static <C extends Collection<T>, T> C addToCollectionUnique( Iterator<T> iterator,
C collection )
{
while ( iterator.hasNext() )
{
addUnique( collection, iterator.next() );
}
return collection;
}
private static <T, C extends Collection<T>> void addUnique( C collection, T item )
{
if ( !collection.add( item ) )
{
throw new IllegalStateException( "Encountered an already added item:" + item +
" when adding items uniquely to a collection:" + collection );
}
}
/**
* Adds all the items in {@code iterator} to {@code collection}.
*
* @param <C> the type of {@link Collection} to add to items to.
* @param <T> the type of items in the collection and iterator.
* @param iterable the {@link Iterator} to grab the items from.
* @param collection the {@link Collection} to add the items to.
* @return the {@code collection} which was passed in, now filled
* with the items from {@code iterator}.
*/
public static <C extends Collection<T>, T> C addToCollectionUnique( Iterable<T> iterable,
C collection )
{
return addToCollectionUnique( iterable.iterator(), collection );
}
/**
* Convenience method for looping over an {@link Iterator}. Converts the
* {@link Iterator} to an {@link Iterable} by wrapping it in an
* {@link Iterable} that returns the {@link Iterator}. It breaks the
* contract of {@link Iterable} in that it returns the supplied iterator
* instance for each call to {@code iterator()} on the returned
* {@link Iterable} instance. This method exists to make it easy to use an
* {@link Iterator} in a for-loop.
*
* @param <T> the type of items in the iterator.
* @param iterator the iterator to expose as an {@link Iterable}.
* @return the supplied iterator posing as an {@link Iterable}.
*/
public static <T> Iterable<T> loop( final Iterator<T> iterator )
{
return () -> iterator;
}
/**
* Exposes {@code iterator} as an {@link Iterable}. It breaks the contract
* of {@link Iterable} in that it returns the supplied iterator instance for
* each call to {@code iterator()} on the returned {@link Iterable}
* instance. This method mostly exists to make it easy to use an
* {@link Iterator} in a for-loop.
*
* @param <T> the type of items in the iterator.
* @param iterator the iterator to expose as an {@link Iterable}.
* @return the supplied iterator posing as an {@link Iterable}.
*/
//@Deprecated * @deprecated use {@link #loop(Iterator) the loop method} instead.
public static <T> Iterable<T> asIterable( final Iterator<T> iterator )
{
return loop( iterator );
}
public static <T> long count( Iterator<T> iterator )
{
return count( iterator, Predicates.alwaysTrue() );
}
/**
* Counts the number of filtered in the {@code iterator} by looping
* through it.
*
* @param <T> the type of items in the iterator.
* @param iterator the {@link Iterator} to count items in.
* @param filter the filter to test items against
* @return the number of filtered items found in {@code iterator}.
*/
public static <T> long count( Iterator<T> iterator, Predicate<T> filter )
{
long result = 0;
while ( iterator.hasNext() )
{
if ( filter.test( iterator.next() ) )
{
result++;
}
}
return result;
}
public static <T> Collection<T> asCollection( Iterator<T> iterable )
{
return addToCollection( iterable, new ArrayList<>() );
}
public static <T> List<T> asList( Iterator<T> iterator )
{
return addToCollection( iterator, new ArrayList<>() );
}
public static <T, EX extends Exception> List<T> asList( RawIterator<T,EX> iterator ) throws EX
{
List<T> out = new ArrayList<>();
while ( iterator.hasNext() )
{
out.add( iterator.next() );
}
return out;
}
public static <T> Set<T> asSet( Iterator<T> iterator )
{
return addToCollection( iterator, new HashSet<>() );
}
/**
* Creates a {@link Set} from an array of items.an
*
* @param items the items to add to the set.
* @param <T> the type of the items
* @return the {@link Set} containing the items.
*/
@SafeVarargs
public static <T> Set<T> asSet( T... items )
{
return new HashSet<>( Arrays.asList( items ) );
}
/**
* Alias for asSet()
*
* @param items the items to add to the set.
* @param <T> the type of the items
* @return the {@link Set} containing the items.
*/
@SafeVarargs
public static <T> Set<T> set( T... items )
{
return asSet( items );
}
/**
* Creates a {@link Set} from an array of items.
*
* @param items the items to add to the set.
* @param <T> the type of the items
* @return the {@link Set} containing the items.
*/
@SafeVarargs
public static <T> Set<T> asUniqueSet( T... items )
{
HashSet<T> set = new HashSet<>();
for ( T item : items )
{
addUnique( set, item );
}
return set;
}
/**
* Creates a {@link Set} from an array of items.
*
* @param items the items to add to the set.
* @param <T> the type of the items
* @return the {@link Set} containing the items.
*/
public static <T> Set<T> asUniqueSet( Iterator<T> items )
{
HashSet<T> set = new HashSet<>();
while ( items.hasNext() )
{
addUnique( set, items.next() );
}
return set;
}
public static Iterator<Long> asIterator( final long... array )
{
return new PrefetchingIterator<Long>()
{
private int index;
@Override
protected Long fetchNextOrNull()
{
try
{
return index < array.length ? array[index] : null;
}
finally
{
index++;
}
}
};
}
public static Iterator<Integer> asIterator( final int... array )
{
return new PrefetchingIterator<Integer>()
{
private int index;
@Override
protected Integer fetchNextOrNull()
{
try
{
return index < array.length ? array[index] : null;
}
finally
{
index++;
}
}
};
}
@SafeVarargs
public static <T> Iterator<T> asIterator( final int maxItems, final T... array )
{
return new PrefetchingIterator<T>()
{
private int index;
@Override
protected T fetchNextOrNull()
{
try
{
return index < array.length && index < maxItems ? array[index] : null;
}
finally
{
index++;
}
}
};
}
public static <T> Iterator<T> iterator( final T item )
{
if ( item == null )
{
return emptyIterator();
}
return new Iterator<T>()
{
T myItem = item;
@Override
public boolean hasNext()
{
return myItem != null;
}
@Override
public T next()
{
if ( !hasNext() )
{
throw new NoSuchElementException();
}
T toReturn = myItem;
myItem = null;
return toReturn;
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
@SafeVarargs
public static <T> Iterator<T> iterator( T... items )
{
return asIterator( items.length, items );
}
@SafeVarargs
public static <T> Iterator<T> iterator( int maxItems, T... items )
{
return asIterator( maxItems, items );
}
public static <T> Iterator<T> appendTo( Iterator<T> iterator, T... appended )
{
return new Iterator<T>()
{
private int index;
@Override
public boolean hasNext()
{
return iterator.hasNext() || index < appended.length;
}
@Override
public T next()
{
if ( iterator.hasNext() )
{
return iterator.next();
}
else if ( index < appended.length )
{
return appended[index++];
}
else
{
throw new NoSuchElementException();
}
}
};
}
public static <T> Iterator<T> prependTo( Iterator<T> iterator, T... prepended )
{
return new Iterator<T>()
{
private int index;
@Override
public boolean hasNext()
{
return index < prepended.length || iterator.hasNext();
}
@Override
public T next()
{
if ( index < prepended.length )
{
return prepended[index++];
}
else if ( iterator.hasNext() )
{
return iterator.next();
}
else
{
throw new NoSuchElementException();
}
}
};
}
@SuppressWarnings( "unchecked" )
public static <T> ResourceIterator<T> emptyResourceIterator()
{
return (ResourceIterator<T>) EmptyResourceIterator.EMPTY_RESOURCE_ITERATOR;
}
public static <T> boolean contains( Iterator<T> iterator, T item )
{
try
{
for ( T element : loop( iterator ) )
{
if ( item == null ? element == null : item.equals( element ) )
{
return true;
}
}
return false;
}
finally
{
if ( iterator instanceof ResourceIterator<?> )
{
((ResourceIterator<?>) iterator).close();
}
}
}
public static <T> ResourceIterator<T> asResourceIterator( final Iterator<T> iterator )
{
if ( iterator instanceof ResourceIterator<?> )
{
return (ResourceIterator<T>) iterator;
}
return new WrappingResourceIterator<>( iterator );
}
public static <T> ResourceIterator<T> resourceIterator( final Iterator<T> iterator, final Resource resource )
{
return new PrefetchingResourceIterator<T>()
{
@Override
public void close()
{
resource.close();
}
@Override
protected T fetchNextOrNull()
{
return iterator.hasNext() ? iterator.next() : null;
}
};
}
@SafeVarargs
public static <T> T[] array( T... items )
{
return items;
}
public static <X> Iterator<X> filter( Predicate<? super X> specification, Iterator<X> i )
{
return new FilterIterable.FilterIterator<>( i, specification );
}
public static <FROM, TO> Iterator<TO> map( Function<? super FROM,? extends TO> function, Iterator<FROM> from )
{
return new MapIterable.MapIterator<>( from, function );
}
public static <FROM, TO, EX extends Exception> RawIterator<TO,EX> map(
ThrowingFunction<? super FROM,? extends TO,EX> function, RawIterator<FROM,EX> from )
{
return new RawMapIterator<>( from, function );
}
public static <T, EX extends Exception> RawIterator<T,EX> asRawIterator( Iterator<T> iter )
{
return new RawIterator<T,EX>()
{
@Override
public boolean hasNext()
{
return iter.hasNext();
}
@Override
public T next()
{
return iter.next();
}
};
}
public static <T, EX extends Exception> RawIterator<T,EX> asRawIterator( Stream<T> stream )
{
return asRawIterator( stream.iterator() );
}
public static <FROM, TO> Iterator<TO> flatMap( Function<? super FROM,? extends Iterator<TO>> function,
Iterator<FROM> from )
{
return new CombiningIterator<>( map( function, from ) );
}
@SafeVarargs
@SuppressWarnings( "unchecked" )
public static <T> Iterator<T> concat( Iterator<? extends T>... iterators )
{
return concat( Arrays.asList( (Iterator<T>[]) iterators ).iterator() );
}
public static <T> ResourceIterator<T> concatResourceIterators( Iterator<ResourceIterator<T>> iterators )
{
return new CombiningResourceIterator<>( iterators );
}
public static <T> Iterator<T> concat( Iterator<Iterator<T>> iterators )
{
return new CombiningIterator<>( iterators );
}
public static <T> ResourceIterable<T> asResourceIterable( final ResourceIterator<T> it )
{
return () -> it;
}
public static String join( String joinString, Iterator<?> iter )
{
StringBuilder sb = new StringBuilder();
while ( iter.hasNext() )
{
sb.append( iter.next().toString() );
if ( iter.hasNext() )
{
sb.append( joinString );
}
}
return sb.toString();
}
public static <T> PrefetchingIterator<T> prefetching( Iterator<T> iterator )
{
return iterator instanceof PrefetchingIterator ? (PrefetchingIterator<T>) iterator :
new PrefetchingIterator<T>()
{
@Override
protected T fetchNextOrNull()
{
return iterator.hasNext() ? iterator.next() : null;
}
};
}
/**
* Create a stream from the given iterator.
*
* <b>Note:</b> returned stream needs to be closed via {@link Stream#close()} if the given iterator implements
* {@link Resource}.
*
* @param iterator the iterator to convert to stream
* @param <T> the type of elements in the given iterator
* @return stream over the iterator elements
* @throws NullPointerException when the given stream is {@code null}
*/
public static <T> Stream<T> stream( Iterator<T> iterator )
{
return stream( iterator, 0 );
}
/**
* Create a stream from the given iterator with given characteristics.
*
* <b>Note:</b> returned stream needs to be closed via {@link Stream#close()} if the given iterator implements
* {@link Resource}.
*
* @param iterator the iterator to convert to stream
* @param characteristics the logical OR of characteristics for the underlying {@link Spliterator}
* @param <T> the type of elements in the given iterator
* @return stream over the iterator elements
* @throws NullPointerException when the given iterator is {@code null}
*/
public static <T> Stream<T> stream( Iterator<T> iterator, int characteristics )
{
Objects.requireNonNull( iterator );
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize( iterator, characteristics );
Stream<T> stream = StreamSupport.stream( spliterator, false );
if ( iterator instanceof Resource )
{
return stream.onClose( ((Resource) iterator)::close );
}
return stream;
}
private static class EmptyResourceIterator<E> implements ResourceIterator<E>
{
private static final ResourceIterator<Object> EMPTY_RESOURCE_ITERATOR = new Iterators.EmptyResourceIterator<>();
@Override
public void close()
{
}
@Override
public boolean hasNext()
{
return false;
}
@Override
public E next()
{
throw new NoSuchElementException();
}
}
}