6023e555288b58e18f6eab4e215615ffdd7254f0
[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
13 public class TransactionIdentifier {
14     protected static final String TX_SEPARATOR = "-txn-";
15
16     private final String memberName;
17     private final long counter;
18     private final long timestamp;
19     private String stringRepresentation;
20
21     public TransactionIdentifier(String memberName, long counter) {
22         this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
23         this.counter = counter;
24         this.timestamp = System.currentTimeMillis();
25     }
26
27     public String getChainId() {
28         return "";
29     }
30
31     protected String getMemberName() {
32         return memberName;
33     }
34
35     protected long getCounter() {
36         return counter;
37     }
38
39     protected long getTimestamp() {
40         return timestamp;
41     }
42
43     public static TransactionIdentifier create(String memberName, long counter) {
44         return new TransactionIdentifier(memberName, counter);
45     }
46
47     @Override
48     public boolean equals(Object o) {
49         if (this == o) {
50             return true;
51         }
52         if (o == null || getClass() != o.getClass()) {
53             return false;
54         }
55
56         TransactionIdentifier that = (TransactionIdentifier) o;
57
58         if (counter != that.counter) {
59             return false;
60         }
61
62         if (timestamp != that.timestamp) {
63             return false;
64         }
65
66         if (!memberName.equals(that.memberName)) {
67             return false;
68         }
69
70         return true;
71     }
72
73     @Override
74     public int hashCode() {
75         int result = memberName.hashCode();
76         result = 31 * result + (int) (counter ^ (counter >>> 32));
77         result = 31 * result + (int)(timestamp ^ (timestamp >>> 32));
78         return result;
79     }
80
81
82     @Override
83     public String toString() {
84         if(stringRepresentation == null) {
85             stringRepresentation = new StringBuilder(memberName.length() + TX_SEPARATOR.length() + 21).
86                 append(memberName).append(TX_SEPARATOR).append(counter).append('-').append(timestamp).toString();
87         }
88
89         return stringRepresentation;
90     }
91
92 }