Skip to content

Commit 1578f18

Browse files
committed
Throw RuntimeException
1 parent 717eb70 commit 1578f18

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

src/main/java/com/maxmind/db/Networks.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
*/
1717
public class Networks<T> implements Iterator<DatabaseRecord<T>> {
1818
private final Reader reader;
19-
private ArrayList<NetworkNode> nodes;
19+
private final ArrayList<NetworkNode> nodes;
2020
private NetworkNode lastNode;
21-
private boolean skipAliasedNetworks;
22-
private Exception err;
23-
private ByteBuffer buffer; /* Stores the buffer for Next() calls */
21+
private final boolean skipAliasedNetworks;
22+
private final ByteBuffer buffer; /* Stores the buffer for Next() calls */
2423
private Class<T> typeParameterClass;
2524

2625
/**
@@ -57,14 +56,6 @@ public class Networks<T> implements Iterator<DatabaseRecord<T>> {
5756
this(reader, false);
5857
}
5958

60-
/**
61-
* Returns if Networks had any errors.
62-
* @return Exception The exception to the Networks iteration.
63-
*/
64-
public Exception getErr() {
65-
return this.err;
66-
}
67-
6859
/**
6960
* Sets the Class for the data type in DataRecord.
7061
* @param cls The class object. ( For example, Map.class )
@@ -82,10 +73,6 @@ public void setDataClass(Class<T> cls) {
8273
*/
8374
@Override
8475
public DatabaseRecord<T> next() {
85-
if (this.err != null) {
86-
return null;
87-
}
88-
8976
try {
9077
T data = this.reader.resolveDataPointer(
9178
this.buffer, this.lastNode.pointer, this.typeParameterClass);
@@ -112,8 +99,7 @@ public DatabaseRecord<T> next() {
11299

113100
return new DatabaseRecord<T>(data, InetAddress.getByAddress(ip), prefixLength);
114101
} catch (IOException e) {
115-
this.err = e;
116-
return null;
102+
throw new NetworksIterationException(e.getMessage());
117103
}
118104
}
119105

@@ -136,9 +122,6 @@ public boolean isInIpv4Subtree(byte[] ip) {
136122
*/
137123
@Override
138124
public boolean hasNext() {
139-
if (this.err != null) {
140-
return false;
141-
}
142125
while (!this.nodes.isEmpty()) {
143126
// Pop the last one.
144127
NetworkNode node = this.nodes.remove(this.nodes.size() - 1);
@@ -160,8 +143,7 @@ public boolean hasNext() {
160143

161144
byte[] ipRight = Arrays.copyOf(node.ip, node.ip.length);
162145
if (ipRight.length <= (node.prefix >> 3)) {
163-
this.err = new InvalidDatabaseException("Invalid search tree");
164-
return false;
146+
throw new NetworksIterationException("Invalid search tree");
165147
}
166148

167149
ipRight[node.prefix >> 3] |= 1 << (7 - (node.prefix % 8));
@@ -173,8 +155,7 @@ public boolean hasNext() {
173155
this.nodes.add(new NetworkNode(ipRight, node.prefix, rightPointer));
174156
node.pointer = this.reader.readNode(this.buffer, node.pointer, 0);
175157
} catch (InvalidDatabaseException e) {
176-
this.err = e;
177-
return false;
158+
throw new NetworksIterationException(e.getMessage());
178159
}
179160
}
180161
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.maxmind.db;
2+
3+
public class NetworksIterationException extends RuntimeException {
4+
NetworksIterationException(String message) {
5+
super(message);
6+
}
7+
}

src/test/java/com/maxmind/db/ReaderTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public void testNetworks() throws IOException, InvalidDatabaseException, Invalid
9191
while(networks.hasNext()) {
9292
DatabaseRecord<Map<String, String>> iteration = networks.next();
9393
Map<String, String> data = iteration.getData();
94-
assertNull(networks.getErr());
9594

9695
InetAddress actualIPInData = InetAddress.getByName(data.get("ip"));
9796

@@ -100,7 +99,6 @@ public void testNetworks() throws IOException, InvalidDatabaseException, Invalid
10099
actualIPInData);
101100
}
102101

103-
assertNull(networks.getErr());
104102
reader.close();
105103
}
106104
}
@@ -113,11 +111,14 @@ public void testNetworksWithInvalidSearchTree() throws IOException, InvalidNetwo
113111

114112
Networks networks = reader.networks(false);
115113
networks.setDataClass(Map.class);
116-
while(networks.hasNext()){
117-
DatabaseRecord iteration = networks.next();
118-
}
119114

120-
assertTrue(networks.getErr() instanceof InvalidDatabaseException);
115+
Exception exception = assertThrows(RuntimeException.class, () -> {
116+
while(networks.hasNext()){
117+
DatabaseRecord iteration = networks.next();
118+
}
119+
});
120+
121+
assertEquals("Invalid search tree", exception.getMessage());
121122
reader.close();
122123
}
123124

@@ -346,7 +347,6 @@ public void testNetworksWithin() throws IOException, InvalidNetworkException{
346347
innerIPs.add(iteration.getNetwork().toString());
347348
}
348349

349-
assertNull(networks.getErr());
350350
assertArrayEquals(test.expected, innerIPs.toArray());
351351

352352
reader.close();
@@ -385,7 +385,6 @@ public void testGeoIPNetworksWithin() throws IOException, InvalidNetworkExceptio
385385
innerIPs.add(iteration.getNetwork().toString());
386386
}
387387

388-
assertNull(networks.getErr());
389388
assertArrayEquals(test.expected, innerIPs.toArray());
390389

391390
reader.close();

0 commit comments

Comments
 (0)