Fixes LOXI-59 IPAdddress hould have a factory function for InetAddress
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
index 1c5be86..a9e04fb 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
@@ -1,7 +1,11 @@
package org.projectfloodlight.openflow.types;
+import java.net.InetAddress;
+
import javax.annotation.Nonnull;
+import com.google.common.base.Preconditions;
+
public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> {
public abstract IPVersion getIpVersion();
@@ -66,9 +70,7 @@
*/
@Nonnull
public static IPAddress<?> of(@Nonnull String ip) {
- if (ip == null) {
- throw new NullPointerException("String ip must not be null");
- }
+ Preconditions.checkNotNull(ip, "ip must not be null");
if (ip.indexOf('.') != -1)
return IPv4Address.of(ip);
else if (ip.indexOf(':') != -1)
@@ -77,4 +79,14 @@
throw new IllegalArgumentException("IP Address not well formed: " + ip);
}
+ /**
+ * Factory function for InetAddress values.
+ * @param address the InetAddress you wish to parse into an IPAddress object.
+ * @return the IPAddress object.
+ */
+ @Nonnull
+ public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
+ Preconditions.checkNotNull(address, "address must not be null");
+ return IPAddress.of(address.getHostAddress());
+ }
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
index 25fc943..865df75 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
@@ -1,6 +1,9 @@
package org.projectfloodlight.openflow.types;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.net.UnknownHostException;
import org.junit.Test;
@@ -51,4 +54,14 @@
}
}
+ @Test
+ public void testFromInetAddressException() throws UnknownHostException {
+ try {
+ IPAddress.fromInetAddress(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ }
+
}