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 org.opendaylight.controller.cluster.access.concepts.MemberName;
14 public class TransactionIdentifier {
15 protected static final String TX_SEPARATOR = "-txn-";
17 private final MemberName memberName;
18 private final long counter;
19 private final long timestamp;
20 private String stringRepresentation;
22 public TransactionIdentifier(MemberName memberName, long counter) {
23 this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
24 this.counter = counter;
25 this.timestamp = System.currentTimeMillis();
28 public String getChainId() {
32 protected MemberName getMemberName() {
36 protected long getCounter() {
40 protected long getTimestamp() {
44 public static TransactionIdentifier create(MemberName memberName, long counter) {
45 return new TransactionIdentifier(memberName, counter);
49 public boolean equals(Object o) {
53 if (o == null || getClass() != o.getClass()) {
57 TransactionIdentifier that = (TransactionIdentifier) o;
59 if (counter != that.counter) {
63 if (timestamp != that.timestamp) {
67 if (!memberName.equals(that.memberName)) {
75 public int hashCode() {
76 int result = memberName.hashCode();
77 result = 31 * result + (int) (counter ^ (counter >>> 32));
78 result = 31 * result + (int)(timestamp ^ (timestamp >>> 32));
84 public String toString() {
85 if(stringRepresentation == null) {
86 stringRepresentation = new StringBuilder(memberName.getName().length() + TX_SEPARATOR.length() + 21).
87 append(memberName.getName()).append(TX_SEPARATOR).append(counter).append('-').append(timestamp).toString();
90 return stringRepresentation;