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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.base.Verify;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.DataInput;
17 import java.io.DataOutput;
18 import java.io.Externalizable;
19 import java.io.IOException;
20 import java.io.ObjectInput;
21 import java.io.ObjectOutput;
22 import java.nio.charset.StandardCharsets;
23 import org.opendaylight.yangtools.concepts.WritableIdentifier;
26 * Type-safe encapsulation of a cluster member name.
28 * @author Robert Varga
31 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
32 private static final class Proxy implements Externalizable {
33 private static final long serialVersionUID = 1L;
34 private byte[] serialized;
36 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
37 // be able to create instances via reflection.
38 @SuppressWarnings("checkstyle:RedundantModifier")
43 Proxy(final byte[] serialized) {
44 this.serialized = Preconditions.checkNotNull(serialized);
48 public void writeExternal(final ObjectOutput out) throws IOException {
49 out.writeInt(serialized.length);
50 out.write(serialized);
54 public void readExternal(final ObjectInput in) throws IOException {
55 serialized = new byte[in.readInt()];
56 in.readFully(serialized);
59 private Object readResolve() {
60 // TODO: consider caching instances here
61 return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
65 private static final long serialVersionUID = 1L;
66 private final String name;
68 @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
69 justification = "The array elements are non-volatile but we don't access them.")
70 private volatile byte[] serialized;
72 private MemberName(final String name) {
73 this.name = Preconditions.checkNotNull(name);
76 MemberName(final String name, final byte[] serialized) {
78 this.serialized = Verify.verifyNotNull(serialized);
81 public static MemberName forName(final String name) {
82 Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
83 // TODO: consider caching instances here
84 return new MemberName(name);
87 public static MemberName readFrom(final DataInput in) throws IOException {
88 final byte[] serialized = new byte[in.readInt()];
89 in.readFully(serialized);
90 return new MemberName(new String(serialized, StandardCharsets.UTF_8));
94 public void writeTo(final DataOutput out) throws IOException {
95 final byte[] local = getSerialized();
96 out.writeInt(local.length);
100 public String getName() {
105 public int hashCode() {
106 return name.hashCode();
110 public boolean equals(final Object obj) {
111 return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
115 public int compareTo(final MemberName obj) {
116 return this == obj ? 0 : name.compareTo(obj.name);
120 public String toString() {
121 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
124 private byte[] getSerialized() {
125 byte[] local = serialized;
127 local = name.getBytes(StandardCharsets.UTF_8);
133 Object writeReplace() {
134 return new Proxy(getSerialized());