bd16e4b28ec67c611eb573ac3205a18e126b8f66
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / FrontendIdentifier.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 java.util.Objects;
18 import org.opendaylight.yangtools.concepts.Identifier;
19
20 /**
21  * A cluster-wide unique identifier of a frontend type located at a cluster member.
22  *
23  * @author Robert Varga
24  */
25 @Beta
26 public final class FrontendIdentifier<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 MemberName memberName;
30         private T clientType;
31
32         public Proxy() {
33             // Needed for Externalizable
34         }
35
36         Proxy(final MemberName memberName, final T clientType) {
37             this.memberName = Preconditions.checkNotNull(memberName);
38             this.clientType = Preconditions.checkNotNull(clientType);
39         }
40
41         @Override
42         public void writeExternal(ObjectOutput out) throws IOException {
43             out.writeObject(memberName);
44             out.writeObject(clientType);
45         }
46
47         @SuppressWarnings("unchecked")
48         @Override
49         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
50             memberName = (MemberName) in.readObject();
51             clientType = (T) in.readObject();
52         }
53
54         private Object readResolve() {
55             return new FrontendIdentifier<>(memberName, clientType);
56         }
57     }
58
59     private static final long serialVersionUID = 1L;
60     private final MemberName memberName;
61     private final T clientType;
62
63     FrontendIdentifier(final MemberName memberName, final T clientType) {
64         this.clientType = Preconditions.checkNotNull(clientType);
65         this.memberName = Preconditions.checkNotNull(memberName);
66     }
67
68     public static <T extends FrontendType> FrontendIdentifier<T> create(MemberName memberName, final T clientType) {
69         return new FrontendIdentifier<>(memberName, clientType);
70     }
71
72     public T getClientType() {
73         return clientType;
74     }
75
76     public MemberName getMemberName() {
77         return memberName;
78     }
79
80     @Override
81     public int hashCode() {
82         return Objects.hash(memberName, clientType);
83     }
84
85     @Override
86     public boolean equals(final Object o) {
87         if (this == o) {
88             return true;
89         }
90         if (!(o instanceof FrontendIdentifier)) {
91             return false;
92         }
93
94         final FrontendIdentifier<?> other = (FrontendIdentifier<?>) o;
95         return memberName.equals(other.memberName) && clientType.equals(other.clientType);
96     }
97
98     @Override
99     public String toString() {
100         return MoreObjects.toStringHelper(FrontendIdentifier.class).add("member", memberName)
101                 .add("clientType", clientType).toString();
102     }
103
104     private Object writeReplace() {
105         return new Proxy<>(memberName, clientType);
106     }
107 }