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 java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.nio.charset.StandardCharsets;
22 import org.opendaylight.yangtools.concepts.WritableIdentifier;
25 * Type-safe encapsulation of a cluster member name.
27 * @author Robert Varga
30 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
31 private static final class Proxy implements Externalizable {
32 private static final long serialVersionUID = 1L;
33 private byte[] serialized;
35 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
36 // be able to create instances via reflection.
37 @SuppressWarnings("checkstyle:RedundantModifier")
42 Proxy(final byte[] serialized) {
43 this.serialized = Preconditions.checkNotNull(serialized);
47 public void writeExternal(final ObjectOutput out) throws IOException {
48 out.writeInt(serialized.length);
49 out.write(serialized);
53 public void readExternal(final ObjectInput in) throws IOException {
54 serialized = new byte[in.readInt()];
55 in.readFully(serialized);
58 private Object readResolve() {
59 // TODO: consider caching instances here
60 return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
64 private static final long serialVersionUID = 1L;
65 private final String name;
66 private volatile byte[] serialized;
68 private MemberName(final String name) {
69 this.name = Preconditions.checkNotNull(name);
72 MemberName(final String name, final byte[] serialized) {
74 this.serialized = Verify.verifyNotNull(serialized);
77 public static MemberName forName(final String name) {
78 Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
79 // TODO: consider caching instances here
80 return new MemberName(name);
83 public static MemberName readFrom(final DataInput in) throws IOException {
84 final byte[] serialized = new byte[in.readInt()];
85 in.readFully(serialized);
86 return new MemberName(new String(serialized, StandardCharsets.UTF_8));
90 public void writeTo(final DataOutput out) throws IOException {
91 final byte[] local = getSerialized();
92 out.writeInt(local.length);
96 public String getName() {
101 public int hashCode() {
102 return name.hashCode();
106 public boolean equals(final Object obj) {
107 return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
111 public int compareTo(final MemberName obj) {
112 return this == obj ? 0 : name.compareTo(obj.name);
116 public String toString() {
117 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
120 private byte[] getSerialized() {
121 byte[] local = serialized;
123 local = name.getBytes(StandardCharsets.UTF_8);
129 Object writeReplace() {
130 return new Proxy(getSerialized());