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.base.MoreObjects;
15 import com.google.common.base.Strings;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.DataInput;
18 import java.io.DataOutput;
19 import java.io.Externalizable;
20 import java.io.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectOutput;
23 import java.io.Serial;
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 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
32 private static final class Proxy implements Externalizable {
34 private static final long serialVersionUID = 1L;
36 private byte[] serialized;
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")
45 Proxy(final byte[] serialized) {
46 this.serialized = requireNonNull(serialized);
50 public void writeExternal(final ObjectOutput out) throws IOException {
51 out.writeInt(serialized.length);
52 out.write(serialized);
56 public void readExternal(final ObjectInput in) throws IOException {
57 serialized = new byte[in.readInt()];
58 in.readFully(serialized);
61 private Object readResolve() {
62 // TODO: consider caching instances here
63 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());