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;
36 // Needed for Externalizable
39 Proxy(final FrontendIdentifier frontendId, final long generation) {
40 this.frontendId = Preconditions.checkNotNull(frontendId);
41 this.generation = generation;
45 public void writeExternal(final ObjectOutput out) throws IOException {
46 frontendId.writeTo(out);
47 WritableObjects.writeLong(out, generation);
51 public void readExternal(final ObjectInput in) throws IOException {
52 frontendId = FrontendIdentifier.readFrom(in);
53 generation = WritableObjects.readLong(in);
56 private Object readResolve() {
57 return new ClientIdentifier(frontendId, generation);
61 private static final long serialVersionUID = 1L;
62 private final FrontendIdentifier frontendId;
63 private final long generation;
65 ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
66 this.frontendId = Preconditions.checkNotNull(frontendId);
67 this.generation = generation;
70 public static ClientIdentifier create(final FrontendIdentifier frontendId,
71 final long generation) {
72 return new ClientIdentifier(frontendId, generation);
75 public static ClientIdentifier readFrom(final DataInput in) throws IOException {
76 final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
77 return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
81 public void writeTo(final DataOutput out) throws IOException {
82 frontendId.writeTo(out);
83 WritableObjects.writeLong(out, generation);
86 public FrontendIdentifier getFrontendId() {
90 public long getGeneration() {
95 public int hashCode() {
96 return frontendId.hashCode() * 31 + Long.hashCode(generation);
100 public boolean equals(final Object o) {
104 if (!(o instanceof ClientIdentifier)) {
108 final ClientIdentifier other = (ClientIdentifier) o;
109 return generation == other.generation && frontendId.equals(other.frontendId);
113 public String toString() {
114 return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
115 .add("generation", Long.toUnsignedString(generation)).toString();
118 private Object writeReplace() {
119 return new Proxy(frontendId, generation);