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