11
11
import java .util .List ;
12
12
import java .util .NoSuchElementException ;
13
13
14
- public class IterableSorter <T > extends SorterBase < T > implements Iterable <T >
14
+ public class IteratingSorter <T > extends SorterBase <T >
15
15
{
16
16
// Set iff sort spilled to disk
17
17
private List <File > _mergerInputs ;
18
18
private DataReader <T > _merger ;
19
- // Set iff sorting completed without cancelation
20
- private Iterator <T > _iterator ;
21
19
22
20
23
- protected IterableSorter (SortConfig config ,
24
- DataReaderFactory <T > readerFactory ,
25
- DataWriterFactory <T > writerFactory ,
26
- Comparator <T > comparator )
21
+ protected IteratingSorter (SortConfig config ,
22
+ DataReaderFactory <T > readerFactory ,
23
+ DataWriterFactory <T > writerFactory ,
24
+ Comparator <T > comparator )
27
25
{
28
26
super (config , readerFactory , writerFactory , comparator );
29
27
}
30
28
31
- protected IterableSorter () {
29
+ protected IteratingSorter () {
32
30
super ();
33
31
}
34
32
35
- protected IterableSorter (SortConfig config ) {
33
+ protected IteratingSorter (SortConfig config ) {
36
34
super (config );
37
35
}
38
36
@@ -44,9 +42,9 @@ protected IterableSorter(SortConfig config) {
44
42
* using {@link DataReaderFactory} and {@link DataWriterFactory} configured
45
43
* for this sorter.
46
44
*
47
- * @return true if sorting complete and output is ready to be written; false if it was cancelled
45
+ * @return Iterator if sorting complete and output is ready to be written; null if it was cancelled
48
46
*/
49
- public boolean sort (DataReader <T > inputReader )
47
+ public Iterator < T > sort (DataReader <T > inputReader )
50
48
throws IOException
51
49
{
52
50
// Clean up any previous sort
@@ -61,10 +59,12 @@ public boolean sort(DataReader<T> inputReader)
61
59
_sortRoundCount = -1 ;
62
60
_currentSortRound = -1 ;
63
61
62
+ Iterator <T > iterator = null ;
64
63
try {
65
64
Object [] items = _readMax (inputReader , buffer , _config .getMaxMemoryUsage (), null );
66
65
if (_checkForCancel ()) {
67
- return false ;
66
+ close ();
67
+ return null ;
68
68
}
69
69
Arrays .sort (items , _rawComparator ());
70
70
T next = inputReader .readNext ();
@@ -76,7 +76,7 @@ public boolean sort(DataReader<T> inputReader)
76
76
inputClosed = true ;
77
77
inputReader .close ();
78
78
_phase = Phase .SORTING ;
79
- _iterator = new CastingIterator <T >(Arrays .asList (items ).iterator ());
79
+ iterator = new CastingIterator <T >(Arrays .asList (items ).iterator ());
80
80
} else { // but if more data than memory-buffer-full, do it right:
81
81
List <File > presorted = new ArrayList <File >();
82
82
presorted .add (_writePresorted (items ));
@@ -86,11 +86,12 @@ public boolean sort(DataReader<T> inputReader)
86
86
inputReader .close ();
87
87
_phase = Phase .SORTING ;
88
88
if (_checkForCancel (presorted )) {
89
- return false ;
89
+ close ();
90
+ return null ;
90
91
}
91
92
_mergerInputs = presorted ;
92
93
_merger = merge (presorted );
93
- _iterator = new MergerIterator <T >(_merger );
94
+ iterator = new MergerIterator <T >(_merger );
94
95
}
95
96
} finally {
96
97
if (!inputClosed ) {
@@ -102,24 +103,11 @@ public boolean sort(DataReader<T> inputReader)
102
103
}
103
104
}
104
105
if (_checkForCancel ()) {
105
- return false ;
106
+ close ();
107
+ return null ;
106
108
}
107
109
_phase = Phase .COMPLETE ;
108
- return true ;
109
- }
110
-
111
- /*
112
- /**********************************************************************
113
- /* Iterable API
114
- /**********************************************************************
115
- */
116
-
117
- @ Override
118
- public Iterator <T > iterator () {
119
- if (_iterator == null ) {
120
- throw new IllegalStateException ("Not yet sorted" );
121
- }
122
- return _iterator ;
110
+ return iterator ;
123
111
}
124
112
125
113
public void close () {
@@ -138,7 +126,6 @@ public void close() {
138
126
}
139
127
_mergerInputs = null ;
140
128
_merger = null ;
141
- _iterator = null ;
142
129
}
143
130
144
131
0 commit comments