2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.identifiers;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
14 public class TransactionIdentifier {
15 private static final String TX_SEPARATOR = "-txn-";
17 private final String memberName;
18 private final long counter;
19 private String stringRepresentation;
21 public TransactionIdentifier(String memberName, long counter) {
22 this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
23 this.counter = counter;
26 public String getChainId() {
30 public static TransactionIdentifier create(String memberName, long counter, String chainId) {
31 if (Strings.isNullOrEmpty(chainId)) {
32 return new TransactionIdentifier(memberName, counter);
34 return new ChainedTransactionIdentifier(memberName, counter, chainId);
39 public boolean equals(Object o) {
43 if (o == null || getClass() != o.getClass()) {
47 TransactionIdentifier that = (TransactionIdentifier) o;
49 if (counter != that.counter) {
52 if (!memberName.equals(that.memberName)) {
60 public int hashCode() {
61 int result = memberName.hashCode();
62 result = 31 * result + (int) (counter ^ (counter >>> 32));
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();
73 return stringRepresentation;