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.IOException;
20 import java.nio.charset.StandardCharsets;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.concepts.WritableIdentifier;
25 * Type-safe encapsulation of a cluster member name.
27 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
29 private static final long serialVersionUID = 1L;
31 private final @NonNull String name;
33 @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
34 justification = "The array elements are non-volatile but we don't access them.")
35 private volatile byte[] serialized;
37 private MemberName(final String name) {
38 this.name = requireNonNull(name);
41 MemberName(final String name, final byte[] serialized) {
43 this.serialized = verifyNotNull(serialized);
46 public static @NonNull MemberName forName(final String name) {
47 checkArgument(!Strings.isNullOrEmpty(name));
48 // TODO: consider caching instances here
49 return new MemberName(name);
52 public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
53 final byte[] serialized = new byte[in.readInt()];
54 in.readFully(serialized);
55 return new MemberName(new String(serialized, StandardCharsets.UTF_8));
59 public void writeTo(final DataOutput out) throws IOException {
60 final byte[] local = getSerialized();
61 out.writeInt(local.length);
65 public @NonNull String getName() {
69 public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
70 .@NonNull MemberName toYang() {
71 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
76 public int hashCode() {
77 return name.hashCode();
81 public boolean equals(final Object obj) {
82 return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
86 public int compareTo(final MemberName obj) {
87 return this == obj ? 0 : name.compareTo(obj.name);
91 public String toString() {
92 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
95 private byte[] getSerialized() {
96 byte[] local = serialized;
98 local = name.getBytes(StandardCharsets.UTF_8);
105 Object writeReplace() {
106 return new MN(getSerialized());