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