Sprinkle @NonNull annotations
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
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 java.util.Objects;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.concepts.WritableIdentifier;
22
23 /**
24  * A cluster-wide unique identifier of a frontend type located at a cluster member.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public final class FrontendIdentifier implements WritableIdentifier {
30     private static final class Proxy implements Externalizable {
31         private static final long serialVersionUID = 1L;
32         private MemberName memberName;
33         private FrontendType clientType;
34
35         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
36         // be able to create instances via reflection.
37         @SuppressWarnings("checkstyle:RedundantModifier")
38         public Proxy() {
39             // Needed for Externalizable
40         }
41
42         Proxy(final MemberName memberName, final FrontendType clientType) {
43             this.memberName = requireNonNull(memberName);
44             this.clientType = requireNonNull(clientType);
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             memberName.writeTo(out);
50             clientType.writeTo(out);
51         }
52
53         @Override
54         public void readExternal(final ObjectInput in) throws IOException {
55             memberName = MemberName.readFrom(in);
56             clientType = FrontendType.readFrom(in);
57         }
58
59         private Object readResolve() {
60             return new FrontendIdentifier(memberName, clientType);
61         }
62     }
63
64     private static final long serialVersionUID = 1L;
65     private final MemberName memberName;
66     private final FrontendType clientType;
67
68     FrontendIdentifier(final MemberName memberName, final FrontendType clientType) {
69         this.clientType = requireNonNull(clientType);
70         this.memberName = requireNonNull(memberName);
71     }
72
73     public static @NonNull FrontendIdentifier create(final MemberName memberName, final FrontendType clientType) {
74         return new FrontendIdentifier(memberName, clientType);
75     }
76
77     public static @NonNull FrontendIdentifier readFrom(final DataInput in) throws IOException {
78         final MemberName memberName = MemberName.readFrom(in);
79         final FrontendType clientType = FrontendType.readFrom(in);
80         return new FrontendIdentifier(memberName, clientType);
81     }
82
83     @Override
84     public void writeTo(final DataOutput out) throws IOException {
85         memberName.writeTo(out);
86         clientType.writeTo(out);
87     }
88
89     public @NonNull FrontendType getClientType() {
90         return clientType;
91     }
92
93     public @NonNull MemberName getMemberName() {
94         return memberName;
95     }
96
97     @Override
98     public int hashCode() {
99         return Objects.hash(memberName, clientType);
100     }
101
102     @Override
103     public boolean equals(final Object obj) {
104         if (this == obj) {
105             return true;
106         }
107         if (!(obj instanceof FrontendIdentifier)) {
108             return false;
109         }
110
111         final FrontendIdentifier other = (FrontendIdentifier) obj;
112         return memberName.equals(other.memberName) && clientType.equals(other.clientType);
113     }
114
115     public @NonNull String toPersistentId() {
116         return memberName.getName() + "-frontend-" + clientType.getName();
117     }
118
119     @Override
120     public String toString() {
121         return toPersistentId();
122     }
123
124     private Object writeReplace() {
125         return new Proxy(memberName, clientType);
126     }
127 }