Skip to content

Commit 6b0f14e

Browse files
committed
Provides Iterator but is not Iterable
1 parent 694cfb1 commit 6b0f14e

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

src/main/java/com/fasterxml/sort/IterableSorter.java renamed to src/main/java/com/fasterxml/sort/IteratingSorter.java

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@
1111
import java.util.List;
1212
import java.util.NoSuchElementException;
1313

14-
public class IterableSorter<T> extends SorterBase<T> implements Iterable<T>
14+
public class IteratingSorter<T> extends SorterBase<T>
1515
{
1616
// Set iff sort spilled to disk
1717
private List<File> _mergerInputs;
1818
private DataReader<T> _merger;
19-
// Set iff sorting completed without cancelation
20-
private Iterator<T> _iterator;
2119

2220

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)
2725
{
2826
super(config, readerFactory, writerFactory, comparator);
2927
}
3028

31-
protected IterableSorter() {
29+
protected IteratingSorter() {
3230
super();
3331
}
3432

35-
protected IterableSorter(SortConfig config) {
33+
protected IteratingSorter(SortConfig config) {
3634
super(config);
3735
}
3836

@@ -44,9 +42,9 @@ protected IterableSorter(SortConfig config) {
4442
* using {@link DataReaderFactory} and {@link DataWriterFactory} configured
4543
* for this sorter.
4644
*
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
4846
*/
49-
public boolean sort(DataReader<T> inputReader)
47+
public Iterator<T> sort(DataReader<T> inputReader)
5048
throws IOException
5149
{
5250
// Clean up any previous sort
@@ -61,10 +59,12 @@ public boolean sort(DataReader<T> inputReader)
6159
_sortRoundCount = -1;
6260
_currentSortRound = -1;
6361

62+
Iterator<T> iterator = null;
6463
try {
6564
Object[] items = _readMax(inputReader, buffer, _config.getMaxMemoryUsage(), null);
6665
if (_checkForCancel()) {
67-
return false;
66+
close();
67+
return null;
6868
}
6969
Arrays.sort(items, _rawComparator());
7070
T next = inputReader.readNext();
@@ -76,7 +76,7 @@ public boolean sort(DataReader<T> inputReader)
7676
inputClosed = true;
7777
inputReader.close();
7878
_phase = Phase.SORTING;
79-
_iterator = new CastingIterator<T>(Arrays.asList(items).iterator());
79+
iterator = new CastingIterator<T>(Arrays.asList(items).iterator());
8080
} else { // but if more data than memory-buffer-full, do it right:
8181
List<File> presorted = new ArrayList<File>();
8282
presorted.add(_writePresorted(items));
@@ -86,11 +86,12 @@ public boolean sort(DataReader<T> inputReader)
8686
inputReader.close();
8787
_phase = Phase.SORTING;
8888
if (_checkForCancel(presorted)) {
89-
return false;
89+
close();
90+
return null;
9091
}
9192
_mergerInputs = presorted;
9293
_merger = merge(presorted);
93-
_iterator = new MergerIterator<T>(_merger);
94+
iterator = new MergerIterator<T>(_merger);
9495
}
9596
} finally {
9697
if (!inputClosed) {
@@ -102,24 +103,11 @@ public boolean sort(DataReader<T> inputReader)
102103
}
103104
}
104105
if (_checkForCancel()) {
105-
return false;
106+
close();
107+
return null;
106108
}
107109
_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;
123111
}
124112

125113
public void close() {
@@ -138,7 +126,6 @@ public void close() {
138126
}
139127
_mergerInputs = null;
140128
_merger = null;
141-
_iterator = null;
142129
}
143130

144131

src/main/java/com/fasterxml/sort/Sorter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Since the cost of creating new instances is trivial, there is usually
1414
* no benefit from reusing instances, other than possible convenience.
1515
*/
16-
public class Sorter<T> extends IterableSorter<T>
16+
public class Sorter<T> extends IteratingSorter<T>
1717
{
1818
/**
1919
* @param config Configuration for the sorter
@@ -70,10 +70,10 @@ public void sort(InputStream source, OutputStream destination)
7070
public boolean sort(DataReader<T> inputReader, DataWriter<T> resultWriter)
7171
throws IOException
7272
{
73-
if(!super.sort(inputReader)) {
73+
Iterator<T> it = super.sort(inputReader);
74+
if(it == null) {
7475
return false;
7576
}
76-
Iterator<T> it = super.iterator();
7777
try {
7878
while(it.hasNext()) {
7979
T value = it.next();

0 commit comments

Comments
 (0)