BUG-5280: remove WritableObjects
[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 com.google.common.base.MoreObjects;
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 org.opendaylight.yangtools.concepts.WritableIdentifier;
19 import org.opendaylight.yangtools.concepts.WritableObjects;
20
21 /**
22  * Globally-unique identifier of a local history.
23  *
24  * @author Robert Varga
25  */
26 public final class LocalHistoryIdentifier implements WritableIdentifier {
27     private static final class Proxy implements Externalizable {
28         private static final long serialVersionUID = 1L;
29         private ClientIdentifier clientId;
30         private long historyId;
31
32         public Proxy() {
33             // For Externalizable
34         }
35
36         Proxy(final ClientIdentifier frontendId, final long historyId) {
37             this.clientId = Preconditions.checkNotNull(frontendId);
38             this.historyId = historyId;
39         }
40
41         @Override
42         public void writeExternal(final ObjectOutput out) throws IOException {
43             clientId.writeTo(out);
44             WritableObjects.writeLong(out, historyId);
45         }
46
47         @Override
48         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
49             clientId = ClientIdentifier.readFrom(in);
50             historyId = WritableObjects.readLong(in);
51         }
52
53         private Object readResolve() {
54             return new LocalHistoryIdentifier(clientId, historyId);
55         }
56     }
57
58     private static final long serialVersionUID = 1L;
59     private final ClientIdentifier clientId;
60     private final long historyId;
61
62     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
63         this.clientId = Preconditions.checkNotNull(frontendId);
64         this.historyId = historyId;
65     }
66
67     public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
68         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
69         return new LocalHistoryIdentifier(clientId, WritableObjects.readLong(in));
70     }
71
72     @Override
73     public void writeTo(final DataOutput out) throws IOException {
74         clientId.writeTo(out);
75         WritableObjects.writeLong(out, historyId);
76     }
77
78     public ClientIdentifier getClientId() {
79         return clientId;
80     }
81
82     public long getHistoryId() {
83         return historyId;
84     }
85
86     @Override
87     public int hashCode() {
88         return clientId.hashCode() * 31 + Long.hashCode(historyId);
89     }
90
91     @Override
92     public boolean equals(final Object o) {
93         if (this == o) {
94             return true;
95         }
96         if (!(o instanceof LocalHistoryIdentifier)) {
97             return false;
98         }
99
100         final LocalHistoryIdentifier other = (LocalHistoryIdentifier) o;
101         return historyId == other.historyId && clientId.equals(other.clientId);
102     }
103
104     @Override
105     public String toString() {
106         return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
107                 .add("history", Long.toUnsignedString(historyId)).toString();
108     }
109
110     private Object writeReplace() {
111         return new Proxy(clientId, historyId);
112     }
113 }