8a1123da520f9ff74d7a504d6839e0ec0d07be20
[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     public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
107         .MemberName toYang() {
108         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
109                 .MemberName(name);
110     }
111
112     @Override
113     public int hashCode() {
114         return name.hashCode();
115     }
116
117     @Override
118     public boolean equals(final Object obj) {
119         return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
120     }
121
122     @Override
123     public int compareTo(final MemberName obj) {
124         return this == obj ? 0 : name.compareTo(obj.name);
125     }
126
127     @Override
128     public String toString() {
129         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
130     }
131
132     private byte[] getSerialized() {
133         byte[] local = serialized;
134         if (local == null) {
135             local = name.getBytes(StandardCharsets.UTF_8);
136             serialized = local;
137         }
138         return local;
139     }
140
141     Object writeReplace() {
142         return new Proxy(getSerialized());
143     }
144 }