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.nio.charset.StandardCharsets;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.opendaylight.yangtools.concepts.WritableIdentifier;
28 * Type-safe encapsulation of a cluster member name.
30 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
31 interface SerialForm extends Externalizable {
32 @NonNull MemberName name();
34 void setName(@NonNull MemberName name);
40 default void writeExternal(final ObjectOutput out) throws IOException {
41 final var serialized = name().getSerialized();
42 out.writeInt(serialized.length);
43 out.write(serialized);
47 default void readExternal(final ObjectInput in) throws IOException {
48 final var serialized = new byte[in.readInt()];
49 in.readFully(serialized);
50 // TODO: consider caching instances here
51 setName(new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized));
55 @Deprecated(since = "7.0.0", forRemoval = true)
56 private static final class Proxy implements SerialForm {
58 private static final long serialVersionUID = 1L;
60 private MemberName name;
62 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
63 // be able to create instances via reflection.
64 @SuppressWarnings("checkstyle:RedundantModifier")
69 Proxy(final MemberName name) {
70 this.name = requireNonNull(name);
74 public MemberName name() {
75 return verifyNotNull(name);
79 public void setName(final MemberName name) {
80 this.name = requireNonNull(name);
84 public Object readResolve() {
90 private static final long serialVersionUID = 1L;
92 private final @NonNull String name;
94 @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
95 justification = "The array elements are non-volatile but we don't access them.")
96 private volatile byte[] serialized;
98 private MemberName(final String name) {
99 this.name = requireNonNull(name);
102 MemberName(final String name, final byte[] serialized) {
104 this.serialized = verifyNotNull(serialized);
107 public static @NonNull MemberName forName(final String name) {
108 checkArgument(!Strings.isNullOrEmpty(name));
109 // TODO: consider caching instances here
110 return new MemberName(name);
113 public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
114 final byte[] serialized = new byte[in.readInt()];
115 in.readFully(serialized);
116 return new MemberName(new String(serialized, StandardCharsets.UTF_8));
120 public void writeTo(final DataOutput out) throws IOException {
121 final byte[] local = getSerialized();
122 out.writeInt(local.length);
126 public @NonNull String getName() {
130 public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
131 .@NonNull MemberName toYang() {
132 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
137 public int hashCode() {
138 return name.hashCode();
142 public boolean equals(final Object obj) {
143 return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
147 public int compareTo(final MemberName obj) {
148 return this == obj ? 0 : name.compareTo(obj.name);
152 public String toString() {
153 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
156 private byte[] getSerialized() {
157 byte[] local = serialized;
159 local = name.getBytes(StandardCharsets.UTF_8);
166 Object writeReplace() {