Skip to content

Commit

Permalink
Remove setDataClass
Browse files Browse the repository at this point in the history
  • Loading branch information
faktas2 committed Nov 7, 2023
1 parent 34c911f commit be0e074
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
32 changes: 16 additions & 16 deletions src/main/java/com/maxmind/db/Networks.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,39 @@ public class Networks<T> implements Iterator<DatabaseRecord<T>> {
private NetworkNode lastNode;
private final boolean skipAliasedNetworks;
private final ByteBuffer buffer; /* Stores the buffer for Next() calls */
private Class<T> typeParameterClass;
private final Class<T> typeParameterClass;

/**
* Constructs a Networks instance.
* @param reader The reader object.
* @param skipAliasedNetworks The boolean to skip aliased networks.
* @param typeParameterClass The type of data returned by the iterator.
* @throws ClosedDatabaseException Exception for a closed database.
*/
Networks(Reader reader, boolean skipAliasedNetworks)
Networks(Reader reader, boolean skipAliasedNetworks, Class<T> typeParameterClass)
throws ClosedDatabaseException {
this(reader, skipAliasedNetworks, new NetworkNode[]{});
this(reader, skipAliasedNetworks, new NetworkNode[]{}, typeParameterClass);
}

/**
* Constructs a Networks instance.
* @param reader The reader object.
* @param skipAliasedNetworks The boolean to skip aliased networks.
* @param nodes The initial nodes array to start Networks iterator with.
* @param typeParameterClass The type of data returned by the iterator.
* @throws ClosedDatabaseException Exception for a closed database.
*/
Networks(Reader reader, boolean skipAliasedNetworks, NetworkNode[] nodes)
Networks(
Reader reader,
boolean skipAliasedNetworks,
NetworkNode[] nodes,
Class<T> typeParameterClass)
throws ClosedDatabaseException {
this.reader = reader;
this.skipAliasedNetworks = skipAliasedNetworks;
this.buffer = reader.getBufferHolder().get();
this.nodes = new Stack<NetworkNode>();
this.typeParameterClass = typeParameterClass;
for (NetworkNode node : nodes) {
this.nodes.push(node);
}
Expand All @@ -54,17 +61,10 @@ public class Networks<T> implements Iterator<DatabaseRecord<T>> {
/**
* Constructs a Networks instance with skipAliasedNetworks set to false by default.
* @param reader The reader object.
* @param typeParameterClass The type of data returned by the iterator.
*/
Networks(Reader reader) throws ClosedDatabaseException {
this(reader, false);
}

/**
* Sets the Class for the data type in DataRecord.
* @param cls The class object. ( For example, Map.class )
*/
public void setDataClass(Class<T> cls) {
this.typeParameterClass = cls;
Networks(Reader reader, Class<T> typeParameterClass) throws ClosedDatabaseException {
this(reader, false, typeParameterClass);
}

/**
Expand Down Expand Up @@ -101,7 +101,7 @@ public DatabaseRecord<T> next() {

return new DatabaseRecord<T>(data, InetAddress.getByAddress(ip), prefixLength);
} catch (IOException e) {
throw new NetworksIterationException(e.getMessage());
throw new NetworksIterationException(e);
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public boolean hasNext() {
this.nodes.push(new NetworkNode(ipRight, node.prefix, rightPointer));
node.pointer = this.reader.readNode(this.buffer, node.pointer, 0);
} catch (InvalidDatabaseException e) {
throw new NetworksIterationException(e.getMessage());
throw new NetworksIterationException(e);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/maxmind/db/NetworksIterationException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.maxmind.db;

/**
* This class represents an error encountered while iterating over the networks.
* The most likely causes are corrupt data in the database, or a bug in the reader code.
*
* This exception extends RuntimeException because it is thrown by the iterator
* methods in {@link Networks}.
*
* @see Networks
*/
public class NetworksIterationException extends RuntimeException {
NetworksIterationException(String message) {
super(message);
}

NetworksIterationException(Throwable cause) {
super(cause);
}
}
24 changes: 17 additions & 7 deletions src/main/java/com/maxmind/db/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ record = this.readNode(buffer, record, bit);
* separately. To disable skipAliasedNetworks, which iterates over the IPv4 networks
* only once, use the SkipAliasedNetworks parameter.
*
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
* @param typeParameterClass The type of data returned by the iterator.
* @return Networks The Networks iterator.
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
* @throws ClosedDatabaseException Exception for a closed databased.
* @throws InvalidDatabaseException Exception for an invalid database.
*/
public Networks networks() throws
public <T> Networks<T> networks(Class<T> typeParameterClass) throws
InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
return this.networks(true);
return this.networks(true, typeParameterClass);
}

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

if (this.getMetadata().getIpVersion() == 6) {
return this.networksWithin(ipAllV6, skipAliasedNetworks);
return this.networksWithin(ipAllV6, skipAliasedNetworks, typeParameterClass);
}
return this.networksWithin(ipAllV4, skipAliasedNetworks);
return this.networksWithin(ipAllV4, skipAliasedNetworks, typeParameterClass);
} catch (UnknownHostException e) {
/* This is returned by getByAddress. This should never happen
as the ipv4 and ipv6 are constants set by us. */
Expand Down Expand Up @@ -288,12 +293,16 @@ private int findIpV4StartNode(ByteBuffer buffer)
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
* @param network Specifies the network to be iterated.
* @param skipAliasedNetworks Boolean for skipping aliased networks.
* @param typeParameterClass The type of data returned by the iterator.
* @return Networks
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
* @throws ClosedDatabaseException Exception for a closed databased.
* @throws InvalidDatabaseException Exception for an invalid database.
*/
public <T> Networks<T> networksWithin(Network network, boolean skipAliasedNetworks)
public <T> Networks<T> networksWithin(
Network network,
boolean skipAliasedNetworks,
Class<T> typeParameterClass)
throws InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
InetAddress networkAddress = network.getNetworkAddress();
if (this.metadata.getIpVersion() == 4 && networkAddress instanceof Inet6Address) {
Expand Down Expand Up @@ -321,7 +330,8 @@ public <T> Networks<T> networksWithin(Network network, boolean skipAliasedNetwor
int prefix = traverseResult[1];

Networks<T> networks = new Networks<T>(this, skipAliasedNetworks,
new Networks.NetworkNode[]{ new Networks.NetworkNode(ipBytes, prefix, node) });
new Networks.NetworkNode[]{new Networks.NetworkNode(ipBytes, prefix, node)},
typeParameterClass);

return networks;
}
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/com/maxmind/db/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public void testNetworks() throws IOException, InvalidDatabaseException, Invalid
File file = getFile("MaxMind-DB-test-ipv" + ipVersion + "-" + recordSize + ".mmdb");

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

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

Networks networks = reader.networks(false);
networks.setDataClass(Map.class);
Networks networks = reader.networks(false, Map.class);

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

Networks networks = reader.networksWithin(network, test.skipAliasedNetworks);
networks.setDataClass(Map.class);
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks, Map.class);

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

Networks networks = reader.networksWithin(network, test.skipAliasedNetworks);
networks.setDataClass(Map.class);
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks, Map.class);

ArrayList<String> innerIPs = new ArrayList<String>();
while(networks.hasNext()){
Expand Down

0 comments on commit be0e074

Please sign in to comment.