32637a578e2d2af08c79f160bade7a9e2faf62aa
[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     private static final String TX_SEPARATOR = "-txn-";
15
16     private final String memberName;
17     private final long counter;
18     private String stringRepresentation;
19
20     public TransactionIdentifier(String memberName, long counter) {
21         this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
22         this.counter = counter;
23     }
24
25     @Override
26     public boolean equals(Object o) {
27         if (this == o) {
28             return true;
29         }
30         if (o == null || getClass() != o.getClass()) {
31             return false;
32         }
33
34         TransactionIdentifier that = (TransactionIdentifier) o;
35
36         if (counter != that.counter) {
37             return false;
38         }
39         if (!memberName.equals(that.memberName)) {
40             return false;
41         }
42
43         return true;
44     }
45
46     @Override
47     public int hashCode() {
48         int result = memberName.hashCode();
49         result = 31 * result + (int) (counter ^ (counter >>> 32));
50         return result;
51     }
52
53     @Override
54     public String toString() {
55         if(stringRepresentation == null) {
56             stringRepresentation = new StringBuilder(memberName.length() + TX_SEPARATOR.length() + 10).
57                 append(memberName).append(TX_SEPARATOR).append(counter).toString();
58         }
59
60         return stringRepresentation;
61     }
62 }