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