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;
13 public class TransactionIdentifier {
14 protected static final String TX_SEPARATOR = "-txn-";
16 private final String memberName;
17 private final long counter;
18 private final long timestamp;
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;
24 this.timestamp = System.currentTimeMillis();
27 public String getChainId() {
31 protected String getMemberName() {
35 protected long getCounter() {
39 protected long getTimestamp() {
43 public static TransactionIdentifier create(String memberName, long counter) {
44 return new TransactionIdentifier(memberName, counter);
48 public boolean equals(Object o) {
52 if (o == null || getClass() != o.getClass()) {
56 TransactionIdentifier that = (TransactionIdentifier) o;
58 if (counter != that.counter) {
62 if (timestamp != that.timestamp) {
66 if (!memberName.equals(that.memberName)) {
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));
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();
89 return stringRepresentation;