Remove use of {String,UUID}Identifier
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / identifiers / TransactionIdentifier.java
index ba2e27c69f96f55192030b5e622e660197d19847..1e8620359a674357d3a9c5eaceb683dc877927d3 100644 (file)
@@ -9,19 +9,40 @@
 package org.opendaylight.controller.cluster.datastore.identifiers;
 
 import com.google.common.base.Preconditions;
+import org.opendaylight.controller.cluster.access.concepts.MemberName;
 
 public class TransactionIdentifier {
-    private final String memberName;
-    private final long counter;
+    protected static final String TX_SEPARATOR = "-txn-";
 
+    private final MemberName memberName;
+    private final long counter;
+    private final long timestamp;
+    private String stringRepresentation;
 
-    public TransactionIdentifier(String memberName, long counter) {
+    public TransactionIdentifier(MemberName memberName, long counter) {
         this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
         this.counter = counter;
+        this.timestamp = System.currentTimeMillis();
+    }
+
+    public String getChainId() {
+        return "";
+    }
+
+    protected MemberName getMemberName() {
+        return memberName;
     }
 
-    public static Builder builder(){
-        return new Builder();
+    protected long getCounter() {
+        return counter;
+    }
+
+    protected long getTimestamp() {
+        return timestamp;
+    }
+
+    public static TransactionIdentifier create(MemberName memberName, long counter) {
+        return new TransactionIdentifier(memberName, counter);
     }
 
     @Override
@@ -38,6 +59,11 @@ public class TransactionIdentifier {
         if (counter != that.counter) {
             return false;
         }
+
+        if (timestamp != that.timestamp) {
+            return false;
+        }
+
         if (!memberName.equals(that.memberName)) {
             return false;
         }
@@ -49,32 +75,19 @@ public class TransactionIdentifier {
     public int hashCode() {
         int result = memberName.hashCode();
         result = 31 * result + (int) (counter ^ (counter >>> 32));
+        result = 31 * result + (int)(timestamp ^ (timestamp >>> 32));
         return result;
     }
 
-    @Override public String toString() {
-        final StringBuilder sb =
-            new StringBuilder();
-        sb.append(memberName).append("-txn-").append(counter);
-        return sb.toString();
-    }
-
-    public static class Builder {
-        private String memberName;
-        private long counter;
 
-        public TransactionIdentifier build(){
-            return new TransactionIdentifier(memberName, counter);
-        }
-
-        public Builder memberName(String memberName){
-            this.memberName = memberName;
-            return this;
+    @Override
+    public String toString() {
+        if(stringRepresentation == null) {
+            stringRepresentation = new StringBuilder(memberName.getName().length() + TX_SEPARATOR.length() + 21).
+                append(memberName.getName()).append(TX_SEPARATOR).append(counter).append('-').append(timestamp).toString();
         }
 
-        public Builder counter(long counter){
-            this.counter = counter;
-            return this;
-        }
+        return stringRepresentation;
     }
+
 }