Sprinkle @NonNull annotations
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / LocalHistoryIdentifier.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.concepts.WritableIdentifier;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * Globally-unique identifier of a local history. This identifier is assigned on the frontend and is composed of
25  * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
26  * - an unsigned long, which uniquely identifies the history on the backend
27  * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
28  *
29  * @author Robert Varga
30  */
31 public final class LocalHistoryIdentifier implements WritableIdentifier {
32     /*
33      * Implementation note: cookie is currently required only for module-based sharding, which is implemented as part
34      *                      of normal DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence
35      *                      we may end up not needing cookie at all.
36      *
37      *                      We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we
38      *                      end up not needing the cookie at all, we can switch to writeLong() and use zero flags for
39      *                      compatibility.
40      */
41     private static final class Proxy implements Externalizable {
42         private static final long serialVersionUID = 1L;
43         private ClientIdentifier clientId;
44         private long historyId;
45         private long cookie;
46
47         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
48         // be able to create instances via reflection.
49         @SuppressWarnings("checkstyle:RedundantModifier")
50         public Proxy() {
51             // For Externalizable
52         }
53
54         Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
55             clientId = requireNonNull(frontendId);
56             this.historyId = historyId;
57             this.cookie = cookie;
58         }
59
60         @Override
61         public void writeExternal(final ObjectOutput out) throws IOException {
62             clientId.writeTo(out);
63             WritableObjects.writeLongs(out, historyId, cookie);
64         }
65
66         @Override
67         public void readExternal(final ObjectInput in) throws IOException {
68             clientId = ClientIdentifier.readFrom(in);
69
70             final byte header = WritableObjects.readLongHeader(in);
71             historyId = WritableObjects.readFirstLong(in, header);
72             cookie = WritableObjects.readSecondLong(in, header);
73         }
74
75         private Object readResolve() {
76             return new LocalHistoryIdentifier(clientId, historyId, cookie);
77         }
78     }
79
80     private static final long serialVersionUID = 1L;
81     private final @NonNull ClientIdentifier clientId;
82     private final long historyId;
83     private final long cookie;
84
85     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
86         this(frontendId, historyId, 0);
87     }
88
89     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
90         clientId = requireNonNull(frontendId);
91         this.historyId = historyId;
92         this.cookie = cookie;
93     }
94
95     public static @NonNull LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
96         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
97
98         final byte header = WritableObjects.readLongHeader(in);
99         return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
100             WritableObjects.readSecondLong(in, header));
101     }
102
103     @Override
104     public void writeTo(final DataOutput out) throws IOException {
105         clientId.writeTo(out);
106         WritableObjects.writeLongs(out, historyId, cookie);
107     }
108
109     public @NonNull ClientIdentifier getClientId() {
110         return clientId;
111     }
112
113     public long getHistoryId() {
114         return historyId;
115     }
116
117     public long getCookie() {
118         return cookie;
119     }
120
121     @Override
122     public int hashCode() {
123         int ret = clientId.hashCode();
124         ret = 31 * ret + Long.hashCode(historyId);
125         ret = 31 * ret + Long.hashCode(cookie);
126         return ret;
127     }
128
129     @Override
130     public boolean equals(final Object obj) {
131         if (this == obj) {
132             return true;
133         }
134         if (!(obj instanceof LocalHistoryIdentifier)) {
135             return false;
136         }
137
138         final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
139         return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
140     }
141
142     @Override
143     public String toString() {
144         return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
145                 .add("history", Long.toUnsignedString(historyId, 16))
146                 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
147     }
148
149     private Object writeReplace() {
150         return new Proxy(clientId, historyId, cookie);
151     }
152 }