Sprinkle @NonNull annotations
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / MemberName.java
index ceec55555fc22b71000c39939825293ad5722f0e..3cc2b0c6398ec59bde76877648e40bc7e4e71c27 100644 (file)
@@ -7,17 +7,23 @@
  */
 package org.opendaylight.controller.cluster.access.concepts;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
-import com.google.common.base.Verify;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 import java.nio.charset.StandardCharsets;
-import org.opendaylight.yangtools.concepts.Identifier;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.concepts.WritableIdentifier;
 
 /**
  * Type-safe encapsulation of a cluster member name.
@@ -25,73 +31,99 @@ import org.opendaylight.yangtools.concepts.Identifier;
  * @author Robert Varga
  */
 @Beta
-public final class MemberName implements Comparable<MemberName>, Identifier {
+public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
     private static final class Proxy implements Externalizable {
         private static final long serialVersionUID = 1L;
         private byte[] serialized;
 
+        // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
+        // be able to create instances via reflection.
+        @SuppressWarnings("checkstyle:RedundantModifier")
         public Proxy() {
             // For Externalizable
         }
 
-        Proxy(final String name) {
-            serialized = name.getBytes(StandardCharsets.UTF_8);
+        Proxy(final byte[] serialized) {
+            this.serialized = requireNonNull(serialized);
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeInt(serialized.length);
             out.write(serialized);
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException {
             serialized = new byte[in.readInt()];
             in.readFully(serialized);
         }
 
         private Object readResolve() {
             // TODO: consider caching instances here
-            return new MemberName(new String(serialized, StandardCharsets.UTF_8), this);
+            return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
         }
     }
 
     private static final long serialVersionUID = 1L;
-    private final String name;
-    private volatile Proxy proxy;
 
-    MemberName(final String name) {
-        this.name = Preconditions.checkNotNull(name);
+    private final @NonNull String name;
+
+    @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
+            justification = "The array elements are non-volatile but we don't access them.")
+    private volatile byte[] serialized;
+
+    private MemberName(final String name) {
+        this.name = requireNonNull(name);
     }
 
-    MemberName(final String name, final Proxy proxy) {
-        this.name = Preconditions.checkNotNull(name);
-        this.proxy = Verify.verifyNotNull(proxy);
+    MemberName(final String name, final byte[] serialized) {
+        this(name);
+        this.serialized = verifyNotNull(serialized);
     }
 
-    public static MemberName forName(final String name) {
-        Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
+    public static @NonNull MemberName forName(final String name) {
+        checkArgument(!Strings.isNullOrEmpty(name));
         // TODO: consider caching instances here
         return new MemberName(name);
     }
 
-    public String getName() {
+    public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
+        final byte[] serialized = new byte[in.readInt()];
+        in.readFully(serialized);
+        return new MemberName(new String(serialized, StandardCharsets.UTF_8));
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        final byte[] local = getSerialized();
+        out.writeInt(local.length);
+        out.write(local);
+    }
+
+    public @NonNull String getName() {
         return name;
     }
 
+    public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
+        .@NonNull MemberName toYang() {
+        return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
+                .MemberName(name);
+    }
+
     @Override
     public int hashCode() {
         return name.hashCode();
     }
 
     @Override
-    public boolean equals(final Object o) {
-        return this == o || (o instanceof MemberName && name.equals(((MemberName)o).name));
+    public boolean equals(final Object obj) {
+        return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
     }
 
     @Override
-    public int compareTo(final MemberName o) {
-        return this == o ? 0 : name.compareTo(o.name);
+    public int compareTo(final MemberName obj) {
+        return this == obj ? 0 : name.compareTo(obj.name);
     }
 
     @Override
@@ -99,14 +131,16 @@ public final class MemberName implements Comparable<MemberName>, Identifier {
         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
     }
 
-    Object writeReplace() {
-        Proxy ret = proxy;
-        if (ret == null) {
-            // We do not really care if multiple threads race here
-            ret = new Proxy(name);
-            proxy = ret;
+    private byte[] getSerialized() {
+        byte[] local = serialized;
+        if (local == null) {
+            local = name.getBytes(StandardCharsets.UTF_8);
+            serialized = local;
         }
+        return local;
+    }
 
-        return ret;
+    Object writeReplace() {
+        return new Proxy(getSerialized());
     }
 }