2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
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;
23 * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
24 * of a particular frontend.
26 * @author Robert Varga
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;
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")
39 // Needed for Externalizable
42 Proxy(final FrontendIdentifier frontendId, final long generation) {
43 this.frontendId = Preconditions.checkNotNull(frontendId);
44 this.generation = generation;
48 public void writeExternal(final ObjectOutput out) throws IOException {
49 frontendId.writeTo(out);
50 WritableObjects.writeLong(out, generation);
54 public void readExternal(final ObjectInput in) throws IOException {
55 frontendId = FrontendIdentifier.readFrom(in);
56 generation = WritableObjects.readLong(in);
59 private Object readResolve() {
60 return new ClientIdentifier(frontendId, generation);
64 private static final long serialVersionUID = 1L;
65 private final FrontendIdentifier frontendId;
66 private final long generation;
68 ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
69 this.frontendId = Preconditions.checkNotNull(frontendId);
70 this.generation = generation;
73 public static ClientIdentifier create(final FrontendIdentifier frontendId,
74 final long generation) {
75 return new ClientIdentifier(frontendId, generation);
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));
84 public void writeTo(final DataOutput out) throws IOException {
85 frontendId.writeTo(out);
86 WritableObjects.writeLong(out, generation);
89 public FrontendIdentifier getFrontendId() {
93 public long getGeneration() {
98 public int hashCode() {
99 return frontendId.hashCode() * 31 + Long.hashCode(generation);
103 public boolean equals(final Object obj) {
107 if (!(obj instanceof ClientIdentifier)) {
111 final ClientIdentifier other = (ClientIdentifier) obj;
112 return generation == other.generation && frontendId.equals(other.frontendId);
116 public String toString() {
117 return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
118 .add("generation", Long.toUnsignedString(generation)).toString();
121 private Object writeReplace() {
122 return new Proxy(frontendId, generation);