Skip to content

Commit acb2459

Browse files
committed
Rename skipAliasedNetworks to includeAliasedNetworks
1 parent 899d6ec commit acb2459

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,38 @@ public final class Networks<T> implements Iterator<DatabaseRecord<T>> {
1818
private final Reader reader;
1919
private final Stack<NetworkNode> nodes;
2020
private NetworkNode lastNode;
21-
private final boolean skipAliasedNetworks;
21+
private final boolean includeAliasedNetworks;
2222
private final ByteBuffer buffer; /* Stores the buffer for Next() calls */
2323
private final Class<T> typeParameterClass;
2424

2525
/**
2626
* Constructs a Networks instance.
2727
* @param reader The reader object.
28-
* @param skipAliasedNetworks The boolean to skip aliased networks.
28+
* @param includeAliasedNetworks The boolean to include aliased networks.
2929
* @param typeParameterClass The type of data returned by the iterator.
3030
* @throws ClosedDatabaseException Exception for a closed database.
3131
*/
32-
Networks(Reader reader, boolean skipAliasedNetworks, Class<T> typeParameterClass)
32+
Networks(Reader reader, boolean includeAliasedNetworks, Class<T> typeParameterClass)
3333
throws ClosedDatabaseException {
34-
this(reader, skipAliasedNetworks, new NetworkNode[]{}, typeParameterClass);
34+
this(reader, includeAliasedNetworks, new NetworkNode[]{}, typeParameterClass);
3535
}
3636

3737
/**
3838
* Constructs a Networks instance.
3939
* @param reader The reader object.
40-
* @param skipAliasedNetworks The boolean to skip aliased networks.
40+
* @param includeAliasedNetworks The boolean to include aliased networks.
4141
* @param nodes The initial nodes array to start Networks iterator with.
4242
* @param typeParameterClass The type of data returned by the iterator.
4343
* @throws ClosedDatabaseException Exception for a closed database.
4444
*/
4545
Networks(
4646
Reader reader,
47-
boolean skipAliasedNetworks,
47+
boolean includeAliasedNetworks,
4848
NetworkNode[] nodes,
4949
Class<T> typeParameterClass)
5050
throws ClosedDatabaseException {
5151
this.reader = reader;
52-
this.skipAliasedNetworks = skipAliasedNetworks;
52+
this.includeAliasedNetworks = includeAliasedNetworks;
5353
this.buffer = reader.getBufferHolder().get();
5454
this.nodes = new Stack<NetworkNode>();
5555
this.typeParameterClass = typeParameterClass;
@@ -59,7 +59,7 @@ public final class Networks<T> implements Iterator<DatabaseRecord<T>> {
5959
}
6060

6161
/**
62-
* Constructs a Networks instance with skipAliasedNetworks set to false by default.
62+
* Constructs a Networks instance with includeAliasedNetworks set to false by default.
6363
* @param reader The reader object.
6464
* @param typeParameterClass The type of data returned by the iterator.
6565
*/
@@ -68,11 +68,9 @@ public final class Networks<T> implements Iterator<DatabaseRecord<T>> {
6868
}
6969

7070
/**
71-
* Returns the next DataRecord. You need to set the class using
72-
* prepareForClass before calling next.
73-
* For example,
74-
* networks.prepareForClass(Map.Class);
75-
* Map test = networks.next();
71+
* Returns the next DataRecord.
72+
* @return The next DataRecord.
73+
* @throws NetworksIterationException An exception when iterating over the networks.
7674
*/
7775
@Override
7876
public DatabaseRecord<T> next() {
@@ -83,11 +81,10 @@ public DatabaseRecord<T> next() {
8381
byte[] ip = this.lastNode.ip;
8482
int prefixLength = this.lastNode.prefix;
8583

86-
// We do this because uses of SkipAliasedNetworks expect the IPv4 networks
87-
// to be returned as IPv4 networks. If we are not skipping aliased
88-
// networks, then the user will get IPv4 networks from the ::FFFF:0:0/96
89-
// network.
90-
if (this.skipAliasedNetworks && isInIpv4Subtree(ip)) {
84+
// We do this because uses of includeAliasedNetworks will get IPv4 networks
85+
// from the ::FFFF:0:0/96. We want to return the IPv4 form of the address
86+
// in that case.
87+
if (!this.includeAliasedNetworks && isInIpv4Subtree(ip)) {
9188
ip = Arrays.copyOfRange(ip, 12, ip.length);
9289
prefixLength -= 96;
9390
}
@@ -117,10 +114,12 @@ private boolean isInIpv4Subtree(byte[] ip) {
117114
return true;
118115
}
119116

120-
/*
121-
* Next prepares the next network for reading with the Network method. It
117+
/**
118+
* hasNext prepares the next network for reading with the Network method. It
122119
* returns true if there is another network to be processed and false if there
123-
* are no more networks or if there is an error.
120+
* are no more networks.
121+
* @return boolean True if there is another network to be processed.
122+
* @throws NetworksIterationException Exception while iterating over the networks.
124123
*/
125124
@Override
126125
public boolean hasNext() {
@@ -131,7 +130,7 @@ public boolean hasNext() {
131130
while (node.pointer != this.reader.getMetadata().getNodeCount()) {
132131
// This skips IPv4 aliases without hardcoding the networks that the writer
133132
// currently aliases.
134-
if (this.skipAliasedNetworks && this.reader.getIpv4Start() != 0
133+
if (!this.includeAliasedNetworks && this.reader.getIpv4Start() != 0
135134
&& node.pointer == this.reader.getIpv4Start()
136135
&& !isInIpv4Subtree(node.ip)) {
137136
break;

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ record = this.readNode(buffer, record, bit);
201201
/**
202202
* Creates a Networks iterator and skips aliased networks.
203203
* Please note that a MaxMind DB may map IPv4 networks into several locations
204-
* in an IPv6 database. This iterator will iterate over all of these locations
205-
* separately. To disable skipAliasedNetworks, which iterates over the IPv4 networks
206-
* only once, use the SkipAliasedNetworks parameter.
204+
* in an IPv6 database. networks() iterates over the canonical locations and
205+
* not the aliases. To include the aliases, you can set includeAliasedNetworks to true.
207206
*
208207
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
209208
* @param typeParameterClass The type of data returned by the iterator.
@@ -214,25 +213,25 @@ record = this.readNode(buffer, record, bit);
214213
*/
215214
public <T> Networks<T> networks(Class<T> typeParameterClass) throws
216215
InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
217-
return this.networks(true, typeParameterClass);
216+
return this.networks(false, typeParameterClass);
218217
}
219218

220219
/**
221220
* Creates a Networks iterator.
222221
* Please note that a MaxMind DB may map IPv4 networks into several locations
223222
* in an IPv6 database. This iterator will iterate over all of these locations
224223
* separately. To set the iteration over the IPv4 networks once, use the
225-
* SkipAliasedNetworks option.
224+
* includeAliasedNetworks option.
226225
*
227226
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
228-
* @param skipAliasedNetworks Enable or disable skipping aliased networks.
227+
* @param includeAliasedNetworks Enable including aliased networks.
229228
* @return Networks The Networks iterator.
230229
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
231230
* @throws ClosedDatabaseException Exception for a closed databased.
232231
* @throws InvalidDatabaseException Exception for an invalid database.
233232
*/
234233
public <T> Networks<T> networks(
235-
boolean skipAliasedNetworks,
234+
boolean includeAliasedNetworks,
236235
Class<T> typeParameterClass) throws
237236
InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
238237
try {
@@ -242,9 +241,9 @@ public <T> Networks<T> networks(
242241
Network ipAllV6 = new Network(ipv6, 0); // Mask 128.
243242

244243
if (this.getMetadata().getIpVersion() == 6) {
245-
return this.networksWithin(ipAllV6, skipAliasedNetworks, typeParameterClass);
244+
return this.networksWithin(ipAllV6, includeAliasedNetworks, typeParameterClass);
246245
}
247-
return this.networksWithin(ipAllV4, skipAliasedNetworks, typeParameterClass);
246+
return this.networksWithin(ipAllV4, includeAliasedNetworks, typeParameterClass);
248247
} catch (UnknownHostException e) {
249248
/* This is returned by getByAddress. This should never happen
250249
as the ipv4 and ipv6 are constants set by us. */
@@ -289,10 +288,10 @@ private int findIpV4StartNode(ByteBuffer buffer)
289288
* Please note that a MaxMind DB may map IPv4 networks into several locations
290289
* in an IPv6 database. This iterator will iterate over all of these locations
291290
* separately. To only iterate over the IPv4 networks once, use the
292-
* SkipAliasedNetworks option.
291+
* includeAliasedNetworks option.
293292
* @param <T> Represents the data type(e.g., Map, HastMap, etc.).
294293
* @param network Specifies the network to be iterated.
295-
* @param skipAliasedNetworks Boolean for skipping aliased networks.
294+
* @param includeAliasedNetworks Boolean for including aliased networks.
296295
* @param typeParameterClass The type of data returned by the iterator.
297296
* @return Networks
298297
* @throws InvalidNetworkException Exception for using an IPv6 network in ipv4-only database.
@@ -301,7 +300,7 @@ private int findIpV4StartNode(ByteBuffer buffer)
301300
*/
302301
public <T> Networks<T> networksWithin(
303302
Network network,
304-
boolean skipAliasedNetworks,
303+
boolean includeAliasedNetworks,
305304
Class<T> typeParameterClass)
306305
throws InvalidNetworkException, ClosedDatabaseException, InvalidDatabaseException {
307306
InetAddress networkAddress = network.getNetworkAddress();
@@ -313,14 +312,14 @@ public <T> Networks<T> networksWithin(
313312
int prefixLength = network.getPrefixLength();
314313

315314
if (this.metadata.getIpVersion() == 6 && ipBytes.length == IPV4_LEN) {
316-
if (skipAliasedNetworks) {
317-
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
318-
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3] };
319-
} else {
315+
if (includeAliasedNetworks) {
320316
// Convert it to the IP address (in 16-byte from) of the IPv4 address.
321317
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
322318
-1, -1, // -1 is for 0xff.
323319
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]};
320+
} else {
321+
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
322+
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3] };
324323
}
325324
prefixLength += 96;
326325
}
@@ -329,7 +328,7 @@ public <T> Networks<T> networksWithin(
329328
int node = traverseResult[0];
330329
int prefix = traverseResult[1];
331330

332-
Networks<T> networks = new Networks<T>(this, skipAliasedNetworks,
331+
Networks<T> networks = new Networks<T>(this, includeAliasedNetworks,
333332
new Networks.NetworkNode[]{new Networks.NetworkNode(ipBytes, prefix, node)},
334333
typeParameterClass);
335334

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ public void testNetworksWithin() throws IOException, InvalidNetworkException{
336336
InetAddress address = InetAddress.getByName(test.network);
337337
Network network = new Network(address, test.prefix);
338338

339-
Networks networks = reader.networksWithin(network, test.skipAliasedNetworks, Map.class);
339+
boolean includeAliasedNetworks = !test.skipAliasedNetworks;
340+
Networks networks = reader.networksWithin(network, includeAliasedNetworks, Map.class);
340341

341342
ArrayList<String> innerIPs = new ArrayList<String>();
342343
while(networks.hasNext()){

0 commit comments

Comments
 (0)