CDS: Introduce ChainedTransactionIdentifier
[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 com.google.common.base.Strings;
13
14 public class TransactionIdentifier {
15     private static final String TX_SEPARATOR = "-txn-";
16
17     private final String memberName;
18     private final long counter;
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     }
25
26     public String getChainId() {
27         return "";
28     }
29
30     public static TransactionIdentifier create(String memberName, long counter, String chainId) {
31         if (Strings.isNullOrEmpty(chainId)) {
32             return new TransactionIdentifier(memberName, counter);
33         } else {
34             return new ChainedTransactionIdentifier(memberName, counter, chainId);
35         }
36     }
37
38     @Override
39     public boolean equals(Object o) {
40         if (this == o) {
41             return true;
42         }
43         if (o == null || getClass() != o.getClass()) {
44             return false;
45         }
46
47         TransactionIdentifier that = (TransactionIdentifier) o;
48
49         if (counter != that.counter) {
50             return false;
51         }
52         if (!memberName.equals(that.memberName)) {
53             return false;
54         }
55
56         return true;
57     }
58
59     @Override
60     public int hashCode() {
61         int result = memberName.hashCode();
62         result = 31 * result + (int) (counter ^ (counter >>> 32));
63         return result;
64     }
65
66     @Override
67     public String toString() {
68         if(stringRepresentation == null) {
69             stringRepresentation = new StringBuilder(memberName.length() + TX_SEPARATOR.length() + 10).
70                 append(memberName).append(TX_SEPARATOR).append(counter).toString();
71         }
72
73         return stringRepresentation;
74     }
75 }