BUG-5280: use MemberName instead of String
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / identifiers / TransactionIdentifier.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.identifiers;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.cluster.access.concepts.MemberName;
13
14 public class TransactionIdentifier {
15     protected static final String TX_SEPARATOR = "-txn-";
16
17     private final MemberName memberName;
18     private final long counter;
19     private final long timestamp;
20     private String stringRepresentation;
21
22     public TransactionIdentifier(MemberName memberName, long counter) {
23         this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
24         this.counter = counter;
25         this.timestamp = System.currentTimeMillis();
26     }
27
28     public String getChainId() {
29         return "";
30     }
31
32     protected MemberName getMemberName() {
33         return memberName;
34     }
35
36     protected long getCounter() {
37         return counter;
38     }
39
40     protected long getTimestamp() {
41         return timestamp;
42     }
43
44     public static TransactionIdentifier create(MemberName memberName, long counter) {
45         return new TransactionIdentifier(memberName, counter);
46     }
47
48     @Override
49     public boolean equals(Object o) {
50         if (this == o) {
51             return true;
52         }
53         if (o == null || getClass() != o.getClass()) {
54             return false;
55         }
56
57         TransactionIdentifier that = (TransactionIdentifier) o;
58
59         if (counter != that.counter) {
60             return false;
61         }
62
63         if (timestamp != that.timestamp) {
64             return false;
65         }
66
67         if (!memberName.equals(that.memberName)) {
68             return false;
69         }
70
71         return true;
72     }
73
74     @Override
75     public int hashCode() {
76         int result = memberName.hashCode();
77         result = 31 * result + (int) (counter ^ (counter >>> 32));
78         result = 31 * result + (int)(timestamp ^ (timestamp >>> 32));
79         return result;
80     }
81
82
83     @Override
84     public String toString() {
85         if(stringRepresentation == null) {
86             stringRepresentation = new StringBuilder(memberName.getName().length() + TX_SEPARATOR.length() + 21).
87                 append(memberName.getName()).append(TX_SEPARATOR).append(counter).append('-').append(timestamp).toString();
88         }
89
90         return stringRepresentation;
91     }
92
93 }