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.Identifier;
22 * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
23 * of a particular frontend.
25 * @author Robert Varga
28 public final class ClientIdentifier implements Identifier, WritableObject {
29 private static final class Proxy implements Externalizable {
30 private static final long serialVersionUID = 1L;
31 private FrontendIdentifier frontendId;
32 private long generation;
35 // Needed for Externalizable
38 Proxy(final FrontendIdentifier frontendId, final long generation) {
39 this.frontendId = Preconditions.checkNotNull(frontendId);
40 this.generation = generation;
44 public void writeExternal(final ObjectOutput out) throws IOException {
45 frontendId.writeTo(out);
46 WritableObjects.writeLong(out, generation);
50 public void readExternal(final ObjectInput in) throws IOException {
51 frontendId = FrontendIdentifier.readFrom(in);
52 generation = WritableObjects.readLong(in);
55 private Object readResolve() {
56 return new ClientIdentifier(frontendId, generation);
60 private static final long serialVersionUID = 1L;
61 private final FrontendIdentifier frontendId;
62 private final long generation;
64 ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
65 this.frontendId = Preconditions.checkNotNull(frontendId);
66 this.generation = generation;
69 public static ClientIdentifier create(final FrontendIdentifier frontendId,
70 final long generation) {
71 return new ClientIdentifier(frontendId, generation);
74 public static ClientIdentifier readFrom(final DataInput in) throws IOException {
75 final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
76 return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
80 public void writeTo(final DataOutput out) throws IOException {
81 frontendId.writeTo(out);
82 WritableObjects.writeLong(out, generation);
85 public FrontendIdentifier getFrontendId() {
89 public long getGeneration() {
94 public int hashCode() {
95 return frontendId.hashCode() * 31 + Long.hashCode(generation);
99 public boolean equals(final Object o) {
103 if (!(o instanceof ClientIdentifier)) {
107 final ClientIdentifier other = (ClientIdentifier) o;
108 return generation == other.generation && frontendId.equals(other.frontendId);
112 public String toString() {
113 return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
114 .add("generation", Long.toUnsignedString(generation)).toString();
117 private Object writeReplace() {
118 return new Proxy(frontendId, generation);