Fix CS 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 java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.nio.charset.StandardCharsets;
22 import org.opendaylight.yangtools.concepts.WritableIdentifier;
23
24 /**
25  * Type-safe encapsulation of a cluster member name.
26  *
27  * @author Robert Varga
28  */
29 @Beta
30 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
31     private static final class Proxy implements Externalizable {
32         private static final long serialVersionUID = 1L;
33         private byte[] serialized;
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             // For Externalizable
40         }
41
42         Proxy(final byte[] serialized) {
43             this.serialized = Preconditions.checkNotNull(serialized);
44         }
45
46         @Override
47         public void writeExternal(final ObjectOutput out) throws IOException {
48             out.writeInt(serialized.length);
49             out.write(serialized);
50         }
51
52         @Override
53         public void readExternal(final ObjectInput in) throws IOException {
54             serialized = new byte[in.readInt()];
55             in.readFully(serialized);
56         }
57
58         private Object readResolve() {
59             // TODO: consider caching instances here
60             return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
61         }
62     }
63
64     private static final long serialVersionUID = 1L;
65     private final String name;
66     private volatile byte[] serialized;
67
68     private MemberName(final String name) {
69         this.name = Preconditions.checkNotNull(name);
70     }
71
72     MemberName(final String name, final byte[] serialized) {
73         this(name);
74         this.serialized = Verify.verifyNotNull(serialized);
75     }
76
77     public static MemberName forName(final String name) {
78         Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
79         // TODO: consider caching instances here
80         return new MemberName(name);
81     }
82
83     public static MemberName readFrom(final DataInput in) throws IOException {
84         final byte[] serialized = new byte[in.readInt()];
85         in.readFully(serialized);
86         return new MemberName(new String(serialized, StandardCharsets.UTF_8));
87     }
88
89     @Override
90     public void writeTo(final DataOutput out) throws IOException {
91         final byte[] local = getSerialized();
92         out.writeInt(local.length);
93         out.write(local);
94     }
95
96     public String getName() {
97         return name;
98     }
99
100     @Override
101     public int hashCode() {
102         return name.hashCode();
103     }
104
105     @Override
106     public boolean equals(final Object obj) {
107         return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
108     }
109
110     @Override
111     public int compareTo(final MemberName obj) {
112         return this == obj ? 0 : name.compareTo(obj.name);
113     }
114
115     @Override
116     public String toString() {
117         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
118     }
119
120     private byte[] getSerialized() {
121         byte[] local = serialized;
122         if (local == null) {
123             local = name.getBytes(StandardCharsets.UTF_8);
124             serialized = local;
125         }
126         return local;
127     }
128
129     Object writeReplace() {
130         return new Proxy(getSerialized());
131     }
132 }