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