Skip to content

Commit 86c2c4a

Browse files
mmerdesrtroilo
authored andcommitted
#470: refactor anonymous implementation to inner class
1 parent 7af02ad commit 86c2c4a

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

oshdb-api/src/main/java/org/heigit/ohsome/oshdb/api/mapreducer/backend/MapReducerJdbc.java

+53-42
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.heigit.ohsome.oshdb.api.mapreducer.backend;
22

3+
import static java.util.Spliterators.spliteratorUnknownSize;
34
import static java.util.stream.Collectors.joining;
45

56
import java.io.IOException;
67
import java.io.ObjectInputStream;
8+
import java.sql.Connection;
79
import java.sql.ResultSet;
810
import java.sql.SQLException;
911
import java.util.Iterator;
1012
import java.util.NoSuchElementException;
1113
import java.util.Optional;
12-
import java.util.Spliterators;
13-
import java.util.stream.Collectors;
1414
import java.util.stream.Stream;
1515
import java.util.stream.StreamSupport;
1616
import javax.annotation.Nonnull;
@@ -82,49 +82,60 @@ protected Stream<GridOSHEntity> getOshCellsStream(CellIdRange cellIdRange) {
8282
pstmt.setLong(2, cellIdRange.getStart().getId());
8383
pstmt.setLong(3, cellIdRange.getEnd().getId());
8484
var oshCellsRawData = pstmt.executeQuery();
85-
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
86-
new Iterator<GridOSHEntity>() {
87-
GridOSHEntity next;
88-
@Override
89-
public boolean hasNext() {
90-
return next != null || (next = getNext()) != null;
91-
}
92-
93-
@Override
94-
public GridOSHEntity next() {
95-
if (!hasNext()) {
96-
throw new NoSuchElementException();
97-
}
98-
var grid = next;
99-
next = null;
100-
return grid;
101-
}
102-
103-
private GridOSHEntity getNext() {
104-
try {
105-
if (!oshCellsRawData.next()) {
106-
try {
107-
oshCellsRawData.close();
108-
} finally {
109-
conn.close();
110-
}
111-
return null;
112-
}
113-
return readOshCellRawData(oshCellsRawData);
114-
} catch (IOException | ClassNotFoundException | SQLException e) {
115-
var exception = new OSHDBException(e);
116-
try {
117-
conn.close();
118-
} catch (Exception e2) {
119-
exception.addSuppressed(e2);
120-
}
121-
throw exception;
122-
}
123-
}
124-
}, 0
85+
return StreamSupport.stream(spliteratorUnknownSize(
86+
new GridOSHEntityIterator(oshCellsRawData, conn), 0
12587
), false);
12688
} catch (SQLException e) {
12789
throw new OSHDBException(e);
12890
}
12991
}
92+
93+
private class GridOSHEntityIterator implements Iterator<GridOSHEntity> {
94+
95+
private final ResultSet oshCellsRawData;
96+
private final Connection conn;
97+
GridOSHEntity next;
98+
99+
public GridOSHEntityIterator(ResultSet oshCellsRawData, Connection conn) {
100+
this.oshCellsRawData = oshCellsRawData;
101+
this.conn = conn;
102+
}
103+
104+
@Override
105+
public boolean hasNext() {
106+
return next != null || (next = getNext()) != null;
107+
}
108+
109+
@Override
110+
public GridOSHEntity next() {
111+
if (!hasNext()) {
112+
throw new NoSuchElementException();
113+
}
114+
var grid = next;
115+
next = null;
116+
return grid;
117+
}
118+
119+
private GridOSHEntity getNext() {
120+
try {
121+
if (!oshCellsRawData.next()) {
122+
try {
123+
oshCellsRawData.close();
124+
} finally {
125+
conn.close();
126+
}
127+
return null;
128+
}
129+
return readOshCellRawData(oshCellsRawData);
130+
} catch (IOException | ClassNotFoundException | SQLException e) {
131+
var exception = new OSHDBException(e);
132+
try {
133+
conn.close();
134+
} catch (Exception e2) {
135+
exception.addSuppressed(e2);
136+
}
137+
throw exception;
138+
}
139+
}
140+
}
130141
}

0 commit comments

Comments
 (0)