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