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.opendaylight.yangtools.concepts.WritableIdentifier;
28 * Type-safe encapsulation of a cluster member name.
30 * @author Robert Varga
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;
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);
67 private static final long serialVersionUID = 1L;
68 private final String name;
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;
74 private MemberName(final String name) {
75 this.name = requireNonNull(name);
78 MemberName(final String name, final byte[] serialized) {
80 this.serialized = verifyNotNull(serialized);
83 public static MemberName forName(final String name) {
84 checkArgument(!Strings.isNullOrEmpty(name));
85 // TODO: consider caching instances here
86 return new MemberName(name);
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));
96 public void writeTo(final DataOutput out) throws IOException {
97 final byte[] local = getSerialized();
98 out.writeInt(local.length);
102 public String getName() {
107 public int hashCode() {
108 return name.hashCode();
112 public boolean equals(final Object obj) {
113 return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
117 public int compareTo(final MemberName obj) {
118 return this == obj ? 0 : name.compareTo(obj.name);
122 public String toString() {
123 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
126 private byte[] getSerialized() {
127 byte[] local = serialized;
129 local = name.getBytes(StandardCharsets.UTF_8);
135 Object writeReplace() {
136 return new Proxy(getSerialized());