BUG-5280: optimize identifier serialization format
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / ClientIdentifier.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.MoreObjects;
12 import com.google.common.base.Preconditions;
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.opendaylight.yangtools.concepts.Identifier;
20
21 /**
22  * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
23  * of a particular frontend.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class ClientIdentifier implements Identifier, WritableObject {
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31         private FrontendIdentifier frontendId;
32         private long generation;
33
34         public Proxy() {
35             // Needed for Externalizable
36         }
37
38         Proxy(final FrontendIdentifier frontendId, final long generation) {
39             this.frontendId = Preconditions.checkNotNull(frontendId);
40             this.generation = generation;
41         }
42
43         @Override
44         public void writeExternal(final ObjectOutput out) throws IOException {
45             frontendId.writeTo(out);
46             WritableObjects.writeLong(out, generation);
47         }
48
49         @Override
50         public void readExternal(final ObjectInput in) throws IOException {
51             frontendId = FrontendIdentifier.readFrom(in);
52             generation = WritableObjects.readLong(in);
53         }
54
55         private Object readResolve() {
56             return new ClientIdentifier(frontendId, generation);
57         }
58     }
59
60     private static final long serialVersionUID = 1L;
61     private final FrontendIdentifier frontendId;
62     private final long generation;
63
64     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
65         this.frontendId = Preconditions.checkNotNull(frontendId);
66         this.generation = generation;
67     }
68
69     public static ClientIdentifier create(final FrontendIdentifier frontendId,
70             final long generation) {
71         return new ClientIdentifier(frontendId, generation);
72     }
73
74     public static ClientIdentifier readFrom(final DataInput in) throws IOException {
75         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
76         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
77     }
78
79     @Override
80     public void writeTo(final DataOutput out) throws IOException {
81         frontendId.writeTo(out);
82         WritableObjects.writeLong(out, generation);
83     }
84
85     public FrontendIdentifier getFrontendId() {
86         return frontendId;
87     }
88
89     public long getGeneration() {
90         return generation;
91     }
92
93     @Override
94     public int hashCode() {
95         return frontendId.hashCode() * 31 + Long.hashCode(generation);
96     }
97
98     @Override
99     public boolean equals(final Object o) {
100         if (this == o) {
101             return true;
102         }
103         if (!(o instanceof ClientIdentifier)) {
104             return false;
105         }
106
107         final ClientIdentifier other = (ClientIdentifier) o;
108         return generation == other.generation && frontendId.equals(other.frontendId);
109     }
110
111     @Override
112     public String toString() {
113         return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
114                 .add("generation", Long.toUnsignedString(generation)).toString();
115     }
116
117     private Object writeReplace() {
118         return new Proxy(frontendId, generation);
119     }
120 }