e97626b7ebcb607d1780795d1c70fd4da41cc411
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import java.io.DataInput;
15 import java.io.DataOutput;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
20 import org.opendaylight.yangtools.concepts.WritableIdentifier;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
25  * of a particular frontend.
26  *
27  * @author Robert Varga
28  */
29 @Beta
30 public final class ClientIdentifier implements WritableIdentifier {
31     private static final class Proxy implements Externalizable {
32         private static final long serialVersionUID = 1L;
33         private FrontendIdentifier frontendId;
34         private long generation;
35
36         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
37         // be able to create instances via reflection.
38         @SuppressWarnings("checkstyle:RedundantModifier")
39         public Proxy() {
40             // Needed for Externalizable
41         }
42
43         Proxy(final FrontendIdentifier frontendId, final long generation) {
44             this.frontendId = requireNonNull(frontendId);
45             this.generation = generation;
46         }
47
48         @Override
49         public void writeExternal(final ObjectOutput out) throws IOException {
50             frontendId.writeTo(out);
51             WritableObjects.writeLong(out, generation);
52         }
53
54         @Override
55         public void readExternal(final ObjectInput in) throws IOException {
56             frontendId = FrontendIdentifier.readFrom(in);
57             generation = WritableObjects.readLong(in);
58         }
59
60         private Object readResolve() {
61             return new ClientIdentifier(frontendId, generation);
62         }
63     }
64
65     private static final long serialVersionUID = 1L;
66     private final FrontendIdentifier frontendId;
67     private final long generation;
68
69     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
70         this.frontendId = requireNonNull(frontendId);
71         this.generation = generation;
72     }
73
74     public static ClientIdentifier create(final FrontendIdentifier frontendId,
75             final long generation) {
76         return new ClientIdentifier(frontendId, generation);
77     }
78
79     public static ClientIdentifier readFrom(final DataInput in) throws IOException {
80         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
81         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
82     }
83
84     @Override
85     public void writeTo(final DataOutput out) throws IOException {
86         frontendId.writeTo(out);
87         WritableObjects.writeLong(out, generation);
88     }
89
90     public FrontendIdentifier getFrontendId() {
91         return frontendId;
92     }
93
94     public long getGeneration() {
95         return generation;
96     }
97
98     @Override
99     public int hashCode() {
100         return frontendId.hashCode() * 31 + Long.hashCode(generation);
101     }
102
103     @Override
104     public boolean equals(final Object obj) {
105         if (this == obj) {
106             return true;
107         }
108         if (!(obj instanceof ClientIdentifier)) {
109             return false;
110         }
111
112         final ClientIdentifier other = (ClientIdentifier) obj;
113         return generation == other.generation && frontendId.equals(other.frontendId);
114     }
115
116     @Override
117     public String toString() {
118         return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
119                 .add("generation", Long.toUnsignedString(generation)).toString();
120     }
121
122     private Object writeReplace() {
123         return new Proxy(frontendId, generation);
124     }
125 }