Fix FindBugs warnings in cds-access-api and enable enforcement
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / TransactionIdentifier.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.access.concepts;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.io.DataInput;
13 import java.io.DataOutput;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.concepts.WritableIdentifier;
20 import org.opendaylight.yangtools.concepts.WritableObjects;
21
22 /**
23  * Globally-unique identifier of a transaction.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class TransactionIdentifier implements WritableIdentifier {
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31         private LocalHistoryIdentifier historyId;
32         private long transactionId;
33
34         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
35         // be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
42             this.historyId = Preconditions.checkNotNull(historyId);
43             this.transactionId = transactionId;
44         }
45
46         @Override
47         public void writeExternal(final ObjectOutput out) throws IOException {
48             historyId.writeTo(out);
49             WritableObjects.writeLong(out, transactionId);
50         }
51
52         @Override
53         public void readExternal(final ObjectInput in) throws IOException {
54             historyId = LocalHistoryIdentifier.readFrom(in);
55             transactionId = WritableObjects.readLong(in);
56         }
57
58         private Object readResolve() {
59             return new TransactionIdentifier(historyId, transactionId);
60         }
61     }
62
63     private static final long serialVersionUID = 1L;
64     private final LocalHistoryIdentifier historyId;
65     private final long transactionId;
66     private String shortString;
67
68     public TransactionIdentifier(@Nonnull final LocalHistoryIdentifier historyId, final long transactionId) {
69         this.historyId = Preconditions.checkNotNull(historyId);
70         this.transactionId = transactionId;
71     }
72
73     public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
74         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
75         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
76     }
77
78     @Override
79     public void writeTo(final DataOutput out) throws IOException {
80         historyId.writeTo(out);
81         WritableObjects.writeLong(out, transactionId);
82     }
83
84     public LocalHistoryIdentifier getHistoryId() {
85         return historyId;
86     }
87
88     public long getTransactionId() {
89         return transactionId;
90     }
91
92     @Override
93     public int hashCode() {
94         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
95     }
96
97     @Override
98     public boolean equals(final Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (!(obj instanceof TransactionIdentifier)) {
103             return false;
104         }
105
106         final TransactionIdentifier other = (TransactionIdentifier) obj;
107         return transactionId == other.transactionId && historyId.equals(other.historyId);
108     }
109
110     public String toShortString() {
111         if (shortString == null) {
112             String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
113             shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
114                     + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
115                     + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId;
116         }
117
118         return shortString;
119     }
120
121     @Override
122     public String toString() {
123         return toShortString();
124     }
125
126     private Object writeReplace() {
127         return new Proxy(historyId, transactionId);
128     }
129 }