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 static java.util.Objects.requireNonNull;
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;
24 * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
25 * of a particular frontend.
27 * @author Robert Varga
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;
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")
40 // Needed for Externalizable
43 Proxy(final FrontendIdentifier frontendId, final long generation) {
44 this.frontendId = requireNonNull(frontendId);
45 this.generation = generation;
49 public void writeExternal(final ObjectOutput out) throws IOException {
50 frontendId.writeTo(out);
51 WritableObjects.writeLong(out, generation);
55 public void readExternal(final ObjectInput in) throws IOException {
56 frontendId = FrontendIdentifier.readFrom(in);
57 generation = WritableObjects.readLong(in);
60 private Object readResolve() {
61 return new ClientIdentifier(frontendId, generation);
65 private static final long serialVersionUID = 1L;
66 private final FrontendIdentifier frontendId;
67 private final long generation;
69 ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
70 this.frontendId = requireNonNull(frontendId);
71 this.generation = generation;
74 public static ClientIdentifier create(final FrontendIdentifier frontendId,
75 final long generation) {
76 return new ClientIdentifier(frontendId, generation);
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));
85 public void writeTo(final DataOutput out) throws IOException {
86 frontendId.writeTo(out);
87 WritableObjects.writeLong(out, generation);
90 public FrontendIdentifier getFrontendId() {
94 public long getGeneration() {
99 public int hashCode() {
100 return frontendId.hashCode() * 31 + Long.hashCode(generation);
104 public boolean equals(final Object obj) {
108 if (!(obj instanceof ClientIdentifier)) {
112 final ClientIdentifier other = (ClientIdentifier) obj;
113 return generation == other.generation && frontendId.equals(other.frontendId);
117 public String toString() {
118 return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
119 .add("generation", Long.toUnsignedString(generation)).toString();
122 private Object writeReplace() {
123 return new Proxy(frontendId, generation);