Change the underlying type of IntentId

- Change the type from String to long
- Change the argument of the constructor of Intent
- Adapt PacketConnectivityIntent and OpticalConnectivityIntent
  for these change

Change-Id: Iec7154c7d87c3a598fb5b55d886fa9775b3ddef0
diff --git a/src/main/java/net/onrc/onos/api/intent/Intent.java b/src/main/java/net/onrc/onos/api/intent/Intent.java
index 048bdee..be34ba8 100644
--- a/src/main/java/net/onrc/onos/api/intent/Intent.java
+++ b/src/main/java/net/onrc/onos/api/intent/Intent.java
@@ -6,6 +6,8 @@
 import net.onrc.onos.api.batchoperation.IBatchOperationTarget;
 import net.onrc.onos.api.flowmanager.IFlow;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
  * The base class for the connectivity abstraction. It allows applications to
  * specify end hosts, apply some basic filtering to traffic, and constrain
@@ -20,15 +22,15 @@
  * data-plane to satisfy those constraints.
  */
 public abstract class Intent implements IBatchOperationTarget {
-    protected final IntentId id;
+    private final IntentId id;
 
     /**
      * Constructor.
      *
      * @param id ID for this Intent object.
      */
-    public Intent(String id) {
-        this.id = new IntentId(id);
+    protected Intent(IntentId id) {
+        this.id = checkNotNull(id);
     }
 
     /**
diff --git a/src/main/java/net/onrc/onos/api/intent/IntentId.java b/src/main/java/net/onrc/onos/api/intent/IntentId.java
index d488183..4ec3e6a 100644
--- a/src/main/java/net/onrc/onos/api/intent/IntentId.java
+++ b/src/main/java/net/onrc/onos/api/intent/IntentId.java
@@ -2,29 +2,46 @@
 
 import net.onrc.onos.api.batchoperation.BatchOperationTargetId;
 
-public class IntentId extends BatchOperationTargetId {
-    private final String value;
+/**
+ * The class representing intent's ID.
+ *
+ * This class is immutable.
+ */
+public final class IntentId extends BatchOperationTargetId {
+    private final long id;
 
-    public IntentId(String id) {
-        value = id;
-    }
-
-    @Override
-    public String toString() {
-        return value;
+    /**
+     * Constructs the ID corresponding to a given long value.
+     *
+     * In the future, this constructor will not be exposed to avoid misuses.
+     *
+     * @param id the underlay value of this ID.
+     */
+    public IntentId(long id) {
+        this.id = id;
     }
 
     @Override
     public int hashCode() {
-        return value.hashCode();
+        return (int) (id ^ (id >>> 32));
     }
 
     @Override
     public boolean equals(Object obj) {
-        if (obj instanceof IntentId) {
-            IntentId other = (IntentId) obj;
-            return (this.value.equals(other.value));
+        if (obj == this) {
+            return true;
         }
-        return false;
+
+        if (!(obj instanceof IntentId)) {
+            return false;
+        }
+
+        IntentId that = (IntentId) obj;
+        return this.id == that.id;
+    }
+
+    @Override
+    public String toString() {
+        return "0x" + Long.toHexString(id);
     }
 }
diff --git a/src/main/java/net/onrc/onos/api/intent/OpticalConnectivityIntent.java b/src/main/java/net/onrc/onos/api/intent/OpticalConnectivityIntent.java
index 89f6208..e9d6171 100644
--- a/src/main/java/net/onrc/onos/api/intent/OpticalConnectivityIntent.java
+++ b/src/main/java/net/onrc/onos/api/intent/OpticalConnectivityIntent.java
@@ -25,7 +25,7 @@
      * @param srcSwitchPort The source transponder port.
      * @param dstSwitchPort The destination transponder port.
      */
-    public OpticalConnectivityIntent(String id,
+    public OpticalConnectivityIntent(IntentId id,
             SwitchPort srcSwitchPort, SwitchPort dstSwitchPort) {
         super(id);
         this.srcSwitchPort = srcSwitchPort;
diff --git a/src/main/java/net/onrc/onos/api/intent/PacketConnectivityIntent.java b/src/main/java/net/onrc/onos/api/intent/PacketConnectivityIntent.java
index 99660c8..020dd56 100644
--- a/src/main/java/net/onrc/onos/api/intent/PacketConnectivityIntent.java
+++ b/src/main/java/net/onrc/onos/api/intent/PacketConnectivityIntent.java
@@ -42,7 +42,7 @@
      * @param canSetupOpticalFlow The flag whether this intent can create
      *        optical flows if needed.
      */
-    public PacketConnectivityIntent(String id,
+    public PacketConnectivityIntent(IntentId id,
             Collection<SwitchPort> srcSwitchPorts, PacketMatch match,
             Collection<SwitchPort> dstSwitchPorts, boolean canSetupOpticalFlow) {
         super(id);
diff --git a/src/test/java/net/onrc/onos/api/intent/IntentIdTest.java b/src/test/java/net/onrc/onos/api/intent/IntentIdTest.java
new file mode 100644
index 0000000..fb563c3
--- /dev/null
+++ b/src/test/java/net/onrc/onos/api/intent/IntentIdTest.java
@@ -0,0 +1,43 @@
+package net.onrc.onos.api.intent;
+
+import org.junit.Test;
+
+import static net.onrc.onos.core.util.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+/**
+ * This class tests the immutability, equality, and non-equality of {@link IntentId}.
+ */
+public class IntentIdTest {
+    /**
+     * Tests the immutability of {@link IntentId}.
+     */
+    @Test
+    public void intentIdFollowsGuidelineForImmutableObject() {
+        assertThatClassIsImmutable(IntentId.class);
+    }
+
+    /**
+     * Tests equality of {@link IntentId}.
+     */
+    @Test
+    public void testEquality() {
+        IntentId id1 = new IntentId(1L);
+        IntentId id2 = new IntentId(1L);
+
+        assertThat(id1, is(id2));
+    }
+
+    /**
+     * Tests non-equality of {@link IntentId}.
+     */
+    @Test
+    public void testNonEquality() {
+        IntentId id1 = new IntentId(1L);
+        IntentId id2 = new IntentId(2L);
+
+        assertThat(id1, is(not(id2)));
+    }
+}