b2016d806c4c7d93f8ba12cadc20f2cb75d2349e
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / ClientIdentifier.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
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;
18
19 /**
20  * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
21  * of a particular frontend.
22  *
23  * @author Robert Varga
24  */
25 @Beta
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;
31
32         public Proxy() {
33             // Needed for Externalizable
34         }
35
36         Proxy(final FrontendIdentifier<T> frontendId, final long generation) {
37             this.frontendId = Preconditions.checkNotNull(frontendId);
38             this.generation = generation;
39         }
40
41         @Override
42         public void writeExternal(ObjectOutput out) throws IOException {
43             out.writeObject(frontendId);
44             out.writeLong(generation);
45         }
46
47         @SuppressWarnings("unchecked")
48         @Override
49         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
50             frontendId = (FrontendIdentifier<T>) in.readObject();
51             generation = in.readLong();
52         }
53
54         private Object readResolve() {
55             return new ClientIdentifier<>(frontendId, generation);
56         }
57     }
58
59     private static final long serialVersionUID = 1L;
60     private final FrontendIdentifier<T> frontendId;
61     private final long generation;
62
63     ClientIdentifier(final FrontendIdentifier<T> frontendId, final long generation) {
64         this.frontendId = Preconditions.checkNotNull(frontendId);
65         this.generation = generation;
66     }
67
68     public static <T extends FrontendType> ClientIdentifier<T> create(final FrontendIdentifier<T> frontendId,
69             final long generation) {
70         return new ClientIdentifier<>(frontendId, generation);
71     }
72
73     public FrontendIdentifier<T> getFrontendId() {
74         return frontendId;
75     }
76
77     public long getGeneration() {
78         return generation;
79     }
80
81     @Override
82     public int hashCode() {
83         return frontendId.hashCode() * 31 + Long.hashCode(generation);
84     }
85
86     @Override
87     public boolean equals(final Object o) {
88         if (this == o) {
89             return true;
90         }
91         if (!(o instanceof ClientIdentifier)) {
92             return false;
93         }
94
95         final ClientIdentifier<?> other = (ClientIdentifier<?>) o;
96         return generation == other.generation && frontendId.equals(other.frontendId);
97     }
98
99     @Override
100     public String toString() {
101         return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
102                 .add("generation", Long.toUnsignedString(generation)).toString();
103     }
104
105     private Object writeReplace() {
106         return new Proxy<>(frontendId, generation);
107     }
108 }