2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
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;
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.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.concepts.WritableIdentifier;
29 * Type-safe encapsulation of a cluster member name.
31 * @author Robert Varga
34 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
35 private static final class Proxy implements Externalizable {
36 private static final long serialVersionUID = 1L;
37 private byte[] serialized;
39 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
40 // be able to create instances via reflection.
41 @SuppressWarnings("checkstyle:RedundantModifier")
46 Proxy(final byte[] serialized) {
47 this.serialized = requireNonNull(serialized);
51 public void writeExternal(final ObjectOutput out) throws IOException {
52 out.writeInt(serialized.length);
53 out.write(serialized);
57 public void readExternal(final ObjectInput in) throws IOException {
58 serialized = new byte[in.readInt()];
59 in.readFully(serialized);
62 private Object readResolve() {
63 // TODO: consider caching instances here
64 return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
68 private static final long serialVersionUID = 1L;
70 private final @NonNull String name;
72 @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
73 justification = "The array elements are non-volatile but we don't access them.")
74 private volatile byte[] serialized;
76 private MemberName(final String name) {
77 this.name = requireNonNull(name);
80 MemberName(final String name, final byte[] serialized) {
82 this.serialized = verifyNotNull(serialized);
85 public static @NonNull MemberName forName(final String name) {
86 checkArgument(!Strings.isNullOrEmpty(name));
87 // TODO: consider caching instances here
88 return new MemberName(name);
91 public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
92 final byte[] serialized = new byte[in.readInt()];
93 in.readFully(serialized);
94 return new MemberName(new String(serialized, StandardCharsets.UTF_8));
98 public void writeTo(final DataOutput out) throws IOException {
99 final byte[] local = getSerialized();
100 out.writeInt(local.length);
104 public @NonNull String getName() {
108 public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
109 .@NonNull MemberName toYang() {
110 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
115 public int hashCode() {
116 return name.hashCode();
120 public boolean equals(final Object obj) {
121 return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
125 public int compareTo(final MemberName obj) {
126 return this == obj ? 0 : name.compareTo(obj.name);
130 public String toString() {
131 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
134 private byte[] getSerialized() {
135 byte[] local = serialized;
137 local = name.getBytes(StandardCharsets.UTF_8);
143 Object writeReplace() {
144 return new Proxy(getSerialized());