This is an automated email from the ASF dual-hosted git repository.

adrabble pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git


The following commit(s) were added to refs/heads/main by this push:
     new c02a6e9c Support single IPv4 address to clean up code (#528)
c02a6e9c is described below

commit c02a6e9cfea15e4f23a7a42a69eee18936fdd483
Author: Antoine Drabble <[email protected]>
AuthorDate: Wed Nov 16 11:36:43 2022 +0100

    Support single IPv4 address to clean up code (#528)
    
    * Support single IPv4 address to clean up code
    
    * Remove empty lines
    
    * assertArrayEquals takes expected value as first parameter
    
    * Hide Ipv4 constructor from a UniqueIpResource
---
 .../iploc/data/{Ipv4Range.java => Ipv4.java}       | 57 ++++++++++------------
 .../org/apache/baremaps/iploc/data/Ipv4Range.java  | 53 ++++----------------
 .../java/org/apache/baremaps/iploc/IpLocTest.java  |  5 +-
 .../org/apache/baremaps/iploc/Ipv4RangeTest.java   | 24 ++++-----
 .../java/org/apache/baremaps/iploc/Ipv4Test.java   | 38 +++++++++++++++
 5 files changed, 88 insertions(+), 89 deletions(-)

diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java 
b/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4.java
similarity index 65%
copy from 
baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java
copy to baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4.java
index 89077e84..a97db07e 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4.java
@@ -17,37 +17,41 @@ package org.apache.baremaps.iploc.data;
 import com.google.common.primitives.Bytes;
 import java.math.BigInteger;
 import java.util.Arrays;
-import net.ripe.ipresource.IpResourceRange;
 import net.ripe.ipresource.Ipv4Address;
+import net.ripe.ipresource.UniqueIpResource;
 
 /**
- * Represents an IP range from a start IP to an end IP stored into longs 
because Java and Sqlite do
- * not have unsigned 32 bits integers
+ * Represents an IPv4
  */
-public class Ipv4Range {
-  private final byte[] start;
-  private final byte[] end;
+public class Ipv4 {
+
+  private final byte[] ip;
 
   /**
    * Constructs an Ipv4 range from a String
    *
-   * @param range the range in format "192.168.0.1 - 192.168.0.255" or 
"192.168.0.1/24"
+   * @param ip the ip in format "192.168.0.1"
+   */
+  public Ipv4(String ip) {
+    this(UniqueIpResource.parse(ip));
+  }
+
+  /**
+   * Construct an Ipv4 from a UniqueIpResource
+   *
+   * @param uniqueIpResource
    */
-  public Ipv4Range(String range) {
-    IpResourceRange ipResourceRange = IpResourceRange.parse(range);
-    this.start = 
forceIpOn4Bytes(ipResourceRange.getStart().getValue().toByteArray());
-    this.end = 
forceIpOn4Bytes(ipResourceRange.getEnd().getValue().toByteArray());
+  protected Ipv4(UniqueIpResource uniqueIpResource) {
+    this.ip = forceIpOn4Bytes(uniqueIpResource.getValue().toByteArray());
   }
 
   /**
-   * Constructs an Ipv4 range from a start and end ips
+   * Constructs an Ipv4 range from an ip
    *
-   * @param start
-   * @param end
+   * @param ip
    */
-  public Ipv4Range(byte[] start, byte[] end) {
-    this.start = start;
-    this.end = end;
+  public Ipv4(byte[] ip) {
+    this.ip = ip;
   }
 
   /**
@@ -82,25 +86,16 @@ public class Ipv4Range {
   }
 
   /**
-   * Returns the first IP in the range
-   *
-   * @return the first IP in the range
-   */
-  public byte[] getStart() {
-    return this.start;
-  }
-
-  /**
-   * Returns the last IP in the range
+   * Returns the IP
    *
-   * @return the last IP in the range
+   * @return the IP
    */
-  public byte[] getEnd() {
-    return this.end;
+  public byte[] getIp() {
+    return this.ip;
   }
 
   @Override
   public String toString() {
-    return "Ipv4Range{" + "start=" + format(start) + ", end=" + format(end) + 
'}';
+    return "Ipv4{" + "value=" + format(ip) + '}';
   }
 }
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java 
b/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java
index 89077e84..0f2c5287 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/iploc/data/Ipv4Range.java
@@ -14,19 +14,15 @@ package org.apache.baremaps.iploc.data;
 
 
 
-import com.google.common.primitives.Bytes;
-import java.math.BigInteger;
-import java.util.Arrays;
 import net.ripe.ipresource.IpResourceRange;
-import net.ripe.ipresource.Ipv4Address;
 
 /**
  * Represents an IP range from a start IP to an end IP stored into longs 
because Java and Sqlite do
  * not have unsigned 32 bits integers
  */
 public class Ipv4Range {
-  private final byte[] start;
-  private final byte[] end;
+  private final Ipv4 start;
+  private final Ipv4 end;
 
   /**
    * Constructs an Ipv4 range from a String
@@ -35,8 +31,8 @@ public class Ipv4Range {
    */
   public Ipv4Range(String range) {
     IpResourceRange ipResourceRange = IpResourceRange.parse(range);
-    this.start = 
forceIpOn4Bytes(ipResourceRange.getStart().getValue().toByteArray());
-    this.end = 
forceIpOn4Bytes(ipResourceRange.getEnd().getValue().toByteArray());
+    this.start = new Ipv4(ipResourceRange.getStart());
+    this.end = new Ipv4(ipResourceRange.getEnd());
   }
 
   /**
@@ -46,39 +42,8 @@ public class Ipv4Range {
    * @param end
    */
   public Ipv4Range(byte[] start, byte[] end) {
-    this.start = start;
-    this.end = end;
-  }
-
-  /**
-   * Force an IP on 4 bytes. Because the RIPE uses BigInteger as a means to 
store IPs and
-   * BigIntegers toByteArray() method returns byte arrays of variable size, we 
need to force it onto
-   * 4 bytes. It might be less than 4 bytes when the first part of the address 
if 0.x.x.x. It might
-   * be more than 4 bytes when the first bit in the address is 1 because 
BigInteger will prepend a
-   * zero byte to prevent negative value in the two's-complement binary 
representation
-   *
-   * @param bytes
-   * @return
-   */
-  private static byte[] forceIpOn4Bytes(byte[] bytes) {
-    if (bytes.length > 4) {
-      return Arrays.copyOfRange(bytes, bytes.length - 4, bytes.length);
-    } else if (bytes.length < 4) {
-      return Bytes.concat(new byte[4 - bytes.length], bytes);
-    }
-    return bytes;
-  }
-
-  /**
-   * Format an IP from a byte array into a string We need to prepend a byte of 
value 0 to the byte
-   * array so that the MSB is 0 in the two's-complement binary representation
-   *
-   * @param ip the IP in a byte array
-   * @return teh formatted IP
-   */
-  public static String format(byte[] ip) {
-    byte[] zero = new byte[] {0};
-    return new Ipv4Address(new BigInteger(Bytes.concat(zero, 
ip)).longValue()).toString();
+    this.start = new Ipv4(start);
+    this.end = new Ipv4(end);
   }
 
   /**
@@ -87,7 +52,7 @@ public class Ipv4Range {
    * @return the first IP in the range
    */
   public byte[] getStart() {
-    return this.start;
+    return this.start.getIp();
   }
 
   /**
@@ -96,11 +61,11 @@ public class Ipv4Range {
    * @return the last IP in the range
    */
   public byte[] getEnd() {
-    return this.end;
+    return this.end.getIp();
   }
 
   @Override
   public String toString() {
-    return "Ipv4Range{" + "start=" + format(start) + ", end=" + format(end) + 
'}';
+    return "Ipv4Range{" + "start=" + start.toString() + ", end=" + 
end.toString() + '}';
   }
 }
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/iploc/IpLocTest.java 
b/baremaps-core/src/test/java/org/apache/baremaps/iploc/IpLocTest.java
index 0cde1e83..a38fe88c 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/iploc/IpLocTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/iploc/IpLocTest.java
@@ -26,6 +26,7 @@ import org.apache.baremaps.collection.utils.FileUtils;
 import org.apache.baremaps.geocoder.Geocoder;
 import org.apache.baremaps.geocoder.geonames.GeonamesGeocoder;
 import org.apache.baremaps.iploc.data.InetnumLocation;
+import org.apache.baremaps.iploc.data.Ipv4;
 import org.apache.baremaps.iploc.data.Ipv4Range;
 import org.apache.baremaps.iploc.data.Location;
 import org.apache.baremaps.iploc.database.InetnumLocationDao;
@@ -92,7 +93,7 @@ class IpLocTest {
   void findByIpWithZeroes() {
     ipLoc.insertNicObjects(nicObjects.stream());
     List<InetnumLocation> inetnumLocations =
-        inetnumLocationDao.findByIp(new Ipv4Range("0.0.0.5/32").getStart());
+        inetnumLocationDao.findByIp(new Ipv4("0.0.0.5").getIp());
     assertEquals(4, inetnumLocations.size());
   }
 
@@ -100,7 +101,7 @@ class IpLocTest {
   void findByIp() {
     ipLoc.insertNicObjects(nicObjects.stream());
     List<InetnumLocation> inetnumLocations =
-        inetnumLocationDao.findByIp(new 
Ipv4Range("255.22.22.2/32").getStart());
+        inetnumLocationDao.findByIp(new Ipv4("255.22.22.2").getIp());
     assertEquals(1, inetnumLocations.size());
   }
 
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4RangeTest.java 
b/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4RangeTest.java
index 3c5c4d4d..a0132cf3 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4RangeTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4RangeTest.java
@@ -26,32 +26,32 @@ class Ipv4RangeTest {
   @Test
   void testRange() {
     Ipv4Range ipv4Range = new Ipv4Range("0.0.0.0 - 0.0.0.255");
-    assertArrayEquals(ipv4Range.getStart(), new byte[] {0x0, 0x0, 0x0, 0x0});
-    assertArrayEquals(ipv4Range.getEnd(), new byte[] {0x0, 0x0, 0x0, (byte) 
0xFF});
+    assertArrayEquals(new byte[] {0x0, 0x0, 0x0, 0x0}, ipv4Range.getStart());
+    assertArrayEquals(new byte[] {0x0, 0x0, 0x0, (byte) 0xFF}, 
ipv4Range.getEnd());
   }
 
   @Test
   void testRangeWithMask() {
     Ipv4Range ipv4Range = new Ipv4Range("0.0.0.0/24");
-    assertArrayEquals(ipv4Range.getStart(), new byte[] {0x0, 0x0, 0x0, 0x0});
-    assertArrayEquals(ipv4Range.getEnd(), new byte[] {0x0, 0x0, 0x0, (byte) 
0xFF});
+    assertArrayEquals(new byte[] {0x0, 0x0, 0x0, 0x0}, ipv4Range.getStart());
+    assertArrayEquals(new byte[] {0x0, 0x0, 0x0, (byte) 0xFF}, 
ipv4Range.getEnd());
   }
 
   @Test
   void testRangeMaxValue() {
     Ipv4Range ipv4Range = new Ipv4Range("255.255.255.0 - 255.255.255.255");
-    assertArrayEquals(ipv4Range.getStart(),
-        new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x0});
-    assertArrayEquals(ipv4Range.getEnd(),
-        new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF});
+    assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x0},
+        ipv4Range.getStart());
+    assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
(byte) 0xFF},
+        ipv4Range.getEnd());
   }
 
   @Test
   void testRangeWithMaxMask() {
     Ipv4Range ipv4Range = new Ipv4Range("255.255.255.0/24");
-    assertArrayEquals(ipv4Range.getStart(),
-        new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x0});
-    assertArrayEquals(ipv4Range.getEnd(),
-        new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF});
+    assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x0},
+        ipv4Range.getStart());
+    assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
(byte) 0xFF},
+        ipv4Range.getEnd());
   }
 }
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4Test.java 
b/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4Test.java
new file mode 100644
index 00000000..2f5ddceb
--- /dev/null
+++ b/baremaps-core/src/test/java/org/apache/baremaps/iploc/Ipv4Test.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+
+package org.apache.baremaps.iploc;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import org.apache.baremaps.iploc.data.Ipv4;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test the IP class which can generate 32 bits byte arrays representing IPv4 
addresses from a
+ * string
+ */
+class Ipv4Test {
+
+  @Test
+  void testRange() {
+    Ipv4 ipv4Range = new Ipv4("0.0.0.0");
+    assertArrayEquals(new byte[] {0x0, 0x0, 0x0, 0x0}, ipv4Range.getIp());
+  }
+
+  @Test
+  void testRangeMaxValue() {
+    Ipv4 ipv4Range = new Ipv4("255.255.255.255");
+    assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
(byte) 0xFF},
+        ipv4Range.getIp());
+  }
+}

Reply via email to