Skip to content

Commit be0e074

Browse files
committed
Remove setDataClass
1 parent 34c911f commit be0e074

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,39 @@ public class Networks<T> implements Iterator<DatabaseRecord<T>> {
2020
private NetworkNode lastNode;
2121
private final boolean skipAliasedNetworks;
2222
private final ByteBuffer buffer; /* Stores the buffer for Next() calls */
23-
private Class<T> typeParameterClass;
23+
private final Class<T> typeParameterClass;
2424

2525
/**
2626
* Constructs a Networks instance.
2727
* @param reader The reader object.
2828
* @param skipAliasedNetworks The boolean to skip aliased networks.
29+
* @param typeParameterClass The type of data returned by the iterator.
2930
* @throws ClosedDatabaseException Exception for a closed database.
3031
*/
31-
Networks(Reader reader, boolean skipAliasedNetworks)
32+
Networks(Reader reader, boolean skipAliasedNetworks, Class<T> typeParameterClass)
3233
throws ClosedDatabaseException {
33-
this(reader, skipAliasedNetworks, new NetworkNode[]{});
34+
this(reader, skipAliasedNetworks, new NetworkNode[]{}, typeParameterClass);
3435
}
3536

3637
/**
3738
* Constructs a Networks instance.
3839
* @param reader The reader object.
3940
* @param skipAliasedNetworks The boolean to skip aliased networks.
4041
* @param nodes The initial nodes array to start Networks iterator with.
42+
* @param typeParameterClass The type of data returned by the iterator.
4143
* @throws ClosedDatabaseException Exception for a closed database.
4244
*/
43-
Networks(Reader reader, boolean skipAliasedNetworks, NetworkNode[] nodes)
45+
Networks(
46+
Reader reader,
47+
boolean skipAliasedNetworks,
48+
NetworkNode[] nodes,
49+
Class<T> typeParameterClass)
4450
throws ClosedDatabaseException {
4551
this.reader = reader;
4652
this.skipAliasedNetworks = skipAliasedNetworks;
4753
this.buffer = reader.getBufferHolder().get();
4854
this.nodes = new Stack<NetworkNode>();
55+
this.typeParameterClass = typeParameterClass;
4956
for (NetworkNode node : nodes) {
5057
this.nodes.push(node);
5158
}
@@ -54,17 +61,10 @@ public class Networks<T> implements Iterator<DatabaseRecord<T>> {
5461
/**
5562
* Constructs a Networks instance with skipAliasedNetworks set to false by default.
5663
* @param reader The reader object.
64+
* @param typeParameterClass The type of data returned by the iterator.
5765
*/
58-
Networks(Reader reader) throws ClosedDatabaseException {
59-
this(reader, false);
60-
}
61-
62-
/**
63-
* Sets the Class for the data type in DataRecord.
64-
* @param cls The class object. ( For example, Map.class )
65-
*/
66-
public void setDataClass(Class<T> cls) {
67-
this.typeParameterClass = cls;
66+
Networks(Reader reader, Class<T> typeParameterClass) throws ClosedDatabaseException {
67+
this(reader, false, typeParameterClass);
6868
}
6969

7070
/**
@@ -101,7 +101,7 @@ public DatabaseRecord<T> next() {
101101

102102
return new DatabaseRecord<T>(data, InetAddress.getByAddress(ip), prefixLength);
103103
} catch (IOException e) {
104-
throw new NetworksIterationException(e.getMessage());
104+
throw new NetworksIterationException(e);
105105
}
106106
}
107107

@@ -156,7 +156,7 @@ public boolean hasNext() {
156156
this.nodes.push(new NetworkNode(ipRight, node.prefix, rightPointer));
157157
node.pointer = this.reader.readNode(this.buffer, node.pointer, 0);
158158
} catch (InvalidDatabaseException e) {
159-
throw new NetworksIterationException(e.getMessage());
159+
throw new NetworksIterationException(e);
160160
}
161161
}
162162
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
package com.maxmind.db;
22

3+
/**
4+
* This class represents an error encountered while iterating over the networks.
5+
* The most likely causes are corrupt data in the database, or a bug in the reader code.
6+
*
7+
* This exception extends RuntimeException because it is thrown by the iterator
8+
* methods in {@link Networks}.
9+
*
10+
* @see Networks
11+
*/
312
public class NetworksIterationException extends RuntimeException {
413
NetworksIterationException(String message) {
514
super(message);
615
}
16+
17+
NetworksIterationException(Throwable cause) {
18+
super(cause);
19+
}
720
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,16 @@ record = this.readNode(buffer, record, bit);
205205
* separately. To disable skipAliasedNetworks, which iterates over the IPv4 networks
206206
* only once, use the SkipAliasedNetworks parameter.
207207
*
208+
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
209+
* @param typeParameterClass The type of data returned by the iterator.
208210
* @return Networks The Networks iterator.
209211
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
210212
* @throws ClosedDatabaseException Exception for a closed databased.
211213
* @throws InvalidDatabaseException Exception for an invalid database.
212214
*/
213-
public Networks networks() throws
215+
public <T> Networks<T> networks(Class<T> typeParameterClass) throws
214216
InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
215-
return this.networks(true);
217+
return this.networks(true, typeParameterClass);
216218
}
217219

218220
/**
@@ -222,13 +224,16 @@ public Networks networks() throws
222224
* separately. To set the iteration over the IPv4 networks once, use the
223225
* SkipAliasedNetworks option.
224226
*
227+
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
225228
* @param skipAliasedNetworks Enable or disable skipping aliased networks.
226229
* @return Networks The Networks iterator.
227230
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
228231
* @throws ClosedDatabaseException Exception for a closed databased.
229232
* @throws InvalidDatabaseException Exception for an invalid database.
230233
*/
231-
public Networks networks(boolean skipAliasedNetworks) throws
234+
public <T> Networks<T> networks(
235+
boolean skipAliasedNetworks,
236+
Class<T> typeParameterClass) throws
232237
InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
233238
try {
234239
InetAddress ipv4 = InetAddress.getByAddress(new byte[4]);
@@ -237,9 +242,9 @@ public Networks networks(boolean skipAliasedNetworks) throws
237242
Network ipAllV6 = new Network(ipv6, 0); // Mask 128.
238243

239244
if (this.getMetadata().getIpVersion() == 6) {
240-
return this.networksWithin(ipAllV6, skipAliasedNetworks);
245+
return this.networksWithin(ipAllV6, skipAliasedNetworks, typeParameterClass);
241246
}
242-
return this.networksWithin(ipAllV4, skipAliasedNetworks);
247+
return this.networksWithin(ipAllV4, skipAliasedNetworks, typeParameterClass);
243248
} catch (UnknownHostException e) {
244249
/* This is returned by getByAddress. This should never happen
245250
as the ipv4 and ipv6 are constants set by us. */
@@ -288,12 +293,16 @@ private int findIpV4StartNode(ByteBuffer buffer)
288293
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
289294
* @param network Specifies the network to be iterated.
290295
* @param skipAliasedNetworks Boolean for skipping aliased networks.
296+
* @param typeParameterClass The type of data returned by the iterator.
291297
* @return Networks
292298
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
293299
* @throws ClosedDatabaseException Exception for a closed databased.
294300
* @throws InvalidDatabaseException Exception for an invalid database.
295301
*/
296-
public <T> Networks<T> networksWithin(Network network, boolean skipAliasedNetworks)
302+
public <T> Networks<T> networksWithin(
303+
Network network,
304+
boolean skipAliasedNetworks,
305+
Class<T> typeParameterClass)
297306
throws InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
298307
InetAddress networkAddress = network.getNetworkAddress();
299308
if (this.metadata.getIpVersion() == 4 && networkAddress instanceof Inet6Address) {
@@ -321,7 +330,8 @@ public <T> Networks<T> networksWithin(Network network, boolean skipAliasedNetwor
321330
int prefix = traverseResult[1];
322331

323332
Networks<T> networks = new Networks<T>(this, skipAliasedNetworks,
324-
new Networks.NetworkNode[]{ new Networks.NetworkNode(ipBytes, prefix, node) });
333+
new Networks.NetworkNode[]{new Networks.NetworkNode(ipBytes, prefix, node)},
334+
typeParameterClass);
325335

326336
return networks;
327337
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public void testNetworks() throws IOException, InvalidDatabaseException, Invalid
8585
File file = getFile("MaxMind-DB-test-ipv" + ipVersion + "-" + recordSize + ".mmdb");
8686

8787
Reader reader = new Reader(file);
88-
Networks networks = reader.networks(false);
89-
networks.setDataClass(Map.class); // This is needed before running any next().
88+
Networks networks = reader.networks(false, Map.class);
9089

9190
while(networks.hasNext()) {
9291
DatabaseRecord<Map<String, String>> iteration = networks.next();
@@ -109,8 +108,7 @@ public void testNetworksWithInvalidSearchTree() throws IOException, InvalidNetwo
109108
File file = getFile("MaxMind-DB-test-broken-search-tree-24.mmdb");
110109
Reader reader = new Reader(file);
111110

112-
Networks networks = reader.networks(false);
113-
networks.setDataClass(Map.class);
111+
Networks networks = reader.networks(false, Map.class);
114112

115113
Exception exception = assertThrows(RuntimeException.class, () -> {
116114
while(networks.hasNext()){
@@ -338,8 +336,7 @@ public void testNetworksWithin() throws IOException, InvalidNetworkException{
338336
InetAddress address = InetAddress.getByName(test.network);
339337
Network network = new Network(address, test.prefix);
340338

341-
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks);
342-
networks.setDataClass(Map.class);
339+
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks, Map.class);
343340

344341
ArrayList<String> innerIPs = new ArrayList<String>();
345342
while(networks.hasNext()){
@@ -376,8 +373,7 @@ public void testGeoIPNetworksWithin() throws IOException, InvalidNetworkExceptio
376373
InetAddress address = InetAddress.getByName(test.network);
377374
Network network = new Network(address, test.prefix);
378375

379-
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks);
380-
networks.setDataClass(Map.class);
376+
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks, Map.class);
381377

382378
ArrayList<String> innerIPs = new ArrayList<String>();
383379
while(networks.hasNext()){

0 commit comments

Comments
 (0)