Sprinkle @NonNull annotations
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / MemberName.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 com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.MoreObjects;
16 import com.google.common.base.Strings;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.Externalizable;
21 import java.io.IOException;
22 import java.io.ObjectInput;
23 import java.io.ObjectOutput;
24 import java.nio.charset.StandardCharsets;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.concepts.WritableIdentifier;
27
28 /**
29  * Type-safe encapsulation of a cluster member name.
30  *
31  * @author Robert Varga
32  */
33 @Beta
34 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
35     private static final class Proxy implements Externalizable {
36         private static final long serialVersionUID = 1L;
37         private byte[] serialized;
38
39         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
40         // be able to create instances via reflection.
41         @SuppressWarnings("checkstyle:RedundantModifier")
42         public Proxy() {
43             // For Externalizable
44         }
45
46         Proxy(final byte[] serialized) {
47             this.serialized = requireNonNull(serialized);
48         }
49
50         @Override
51         public void writeExternal(final ObjectOutput out) throws IOException {
52             out.writeInt(serialized.length);
53             out.write(serialized);
54         }
55
56         @Override
57         public void readExternal(final ObjectInput in) throws IOException {
58             serialized = new byte[in.readInt()];
59             in.readFully(serialized);
60         }
61
62         private Object readResolve() {
63             // TODO: consider caching instances here
64             return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
65         }
66     }
67
68     private static final long serialVersionUID = 1L;
69
70     private final @NonNull String name;
71
72     @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
73             justification = "The array elements are non-volatile but we don't access them.")
74     private volatile byte[] serialized;
75
76     private MemberName(final String name) {
77         this.name = requireNonNull(name);
78     }
79
80     MemberName(final String name, final byte[] serialized) {
81         this(name);
82         this.serialized = verifyNotNull(serialized);
83     }
84
85     public static @NonNull MemberName forName(final String name) {
86         checkArgument(!Strings.isNullOrEmpty(name));
87         // TODO: consider caching instances here
88         return new MemberName(name);
89     }
90
91     public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
92         final byte[] serialized = new byte[in.readInt()];
93         in.readFully(serialized);
94         return new MemberName(new String(serialized, StandardCharsets.UTF_8));
95     }
96
97     @Override
98     public void writeTo(final DataOutput out) throws IOException {
99         final byte[] local = getSerialized();
100         out.writeInt(local.length);
101         out.write(local);
102     }
103
104     public @NonNull String getName() {
105         return name;
106     }
107
108     public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
109         .@NonNull MemberName toYang() {
110         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
111                 .MemberName(name);
112     }
113
114     @Override
115     public int hashCode() {
116         return name.hashCode();
117     }
118
119     @Override
120     public boolean equals(final Object obj) {
121         return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
122     }
123
124     @Override
125     public int compareTo(final MemberName obj) {
126         return this == obj ? 0 : name.compareTo(obj.name);
127     }
128
129     @Override
130     public String toString() {
131         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
132     }
133
134     private byte[] getSerialized() {
135         byte[] local = serialized;
136         if (local == null) {
137             local = name.getBytes(StandardCharsets.UTF_8);
138             serialized = local;
139         }
140         return local;
141     }
142
143     Object writeReplace() {
144         return new Proxy(getSerialized());
145     }
146 }