2 * Copyright (c) 2016 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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.concepts.Identifier;
21 * Globally-unique identifier of a transaction.
23 * @param <T> Frontend type
25 * @author Robert Varga
28 public final class TransactionIdentifier<T extends FrontendType> implements Identifier {
29 private static final class Proxy<T extends FrontendType> implements Externalizable {
30 private static final long serialVersionUID = 1L;
31 private LocalHistoryIdentifier<T> historyId;
32 private long transactionId;
38 Proxy(final LocalHistoryIdentifier<T> historyId, final long transactionId) {
39 this.historyId = Preconditions.checkNotNull(historyId);
40 this.transactionId = transactionId;
44 public void writeExternal(final ObjectOutput out) throws IOException {
45 out.writeObject(historyId);
46 out.writeLong(transactionId);
49 @SuppressWarnings("unchecked")
51 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
52 historyId = (LocalHistoryIdentifier<T>) in.readObject();
53 transactionId = in.readLong();
56 private Object readResolve() {
57 return new TransactionIdentifier<>(historyId, transactionId);
61 private static final long serialVersionUID = 1L;
62 private final LocalHistoryIdentifier<T> historyId;
63 private final long transactionId;
65 public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier<T> historyId, final long transactionId) {
66 this.historyId = Preconditions.checkNotNull(historyId);
67 this.transactionId = transactionId;
70 public LocalHistoryIdentifier<T> getHistoryId() {
74 public long getTransactionId() {
79 public int hashCode() {
80 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
84 public boolean equals(final Object o) {
88 if (!(o instanceof TransactionIdentifier)) {
92 final TransactionIdentifier<?> other = (TransactionIdentifier<?>) o;
93 return transactionId == other.transactionId && historyId.equals(other.historyId);
97 public String toString() {
98 return MoreObjects.toStringHelper(TransactionIdentifier.class).add("history", historyId)
99 .add("transaction", transactionId).toString();
102 private Object writeReplace() {
103 return new Proxy<>(historyId, transactionId);