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.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.yangtools.concepts.Identifier;
20 * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
21 * of a particular frontend.
23 * @author Robert Varga
26 public final class ClientIdentifier<T extends FrontendType> implements Identifier {
27 private static final class Proxy<T extends FrontendType> implements Externalizable {
28 private static final long serialVersionUID = 1L;
29 private FrontendIdentifier<T> frontendId;
30 private long generation;
33 // Needed for Externalizable
36 Proxy(final FrontendIdentifier<T> frontendId, final long generation) {
37 this.frontendId = Preconditions.checkNotNull(frontendId);
38 this.generation = generation;
42 public void writeExternal(ObjectOutput out) throws IOException {
43 out.writeObject(frontendId);
44 out.writeLong(generation);
47 @SuppressWarnings("unchecked")
49 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
50 frontendId = (FrontendIdentifier<T>) in.readObject();
51 generation = in.readLong();
54 private Object readResolve() {
55 return new ClientIdentifier<>(frontendId, generation);
59 private static final long serialVersionUID = 1L;
60 private final FrontendIdentifier<T> frontendId;
61 private final long generation;
63 ClientIdentifier(final FrontendIdentifier<T> frontendId, final long generation) {
64 this.frontendId = Preconditions.checkNotNull(frontendId);
65 this.generation = generation;
68 public static <T extends FrontendType> ClientIdentifier<T> create(final FrontendIdentifier<T> frontendId,
69 final long generation) {
70 return new ClientIdentifier<>(frontendId, generation);
73 public FrontendIdentifier<T> getFrontendId() {
77 public long getGeneration() {
82 public int hashCode() {
83 return frontendId.hashCode() * 31 + Long.hashCode(generation);
87 public boolean equals(final Object o) {
91 if (!(o instanceof ClientIdentifier)) {
95 final ClientIdentifier<?> other = (ClientIdentifier<?>) o;
96 return generation == other.generation && frontendId.equals(other.frontendId);
100 public String toString() {
101 return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
102 .add("generation", Long.toUnsignedString(generation)).toString();
105 private Object writeReplace() {
106 return new Proxy<>(frontendId, generation);