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;
39 Proxy(final byte[] serialized) {
40 this.serialized = Preconditions.checkNotNull(serialized);
44 public void writeExternal(final ObjectOutput out) throws IOException {
45 out.writeInt(serialized.length);
46 out.write(serialized);
50 public void readExternal(final ObjectInput in) throws IOException {
51 serialized = new byte[in.readInt()];
52 in.readFully(serialized);
55 private Object readResolve() {
56 // TODO: consider caching instances here
57 return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
61 private static final long serialVersionUID = 1L;
62 private final String name;
63 private volatile byte[] serialized;
65 private MemberName(final String name) {
66 this.name = Preconditions.checkNotNull(name);
69 MemberName(final String name, final byte[] serialized) {
71 this.serialized = Verify.verifyNotNull(serialized);
74 public static MemberName forName(final String name) {
75 Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
76 // TODO: consider caching instances here
77 return new MemberName(name);
80 public static MemberName readFrom(final DataInput in) throws IOException {
81 final byte[] serialized = new byte[in.readInt()];
82 in.readFully(serialized);
83 return new MemberName(new String(serialized, StandardCharsets.UTF_8));
87 public void writeTo(final DataOutput out) throws IOException {
88 final byte[] serialized = getSerialized();
89 out.writeInt(serialized.length);
90 out.write(serialized);
93 public String getName() {
98 public int hashCode() {
99 return name.hashCode();
103 public boolean equals(final Object o) {
104 return this == o || (o instanceof MemberName && name.equals(((MemberName)o).name));
108 public int compareTo(final MemberName o) {
109 return this == o ? 0 : name.compareTo(o.name);
113 public String toString() {
114 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
117 private byte[] getSerialized() {
118 byte[] local = serialized;
120 local = name.getBytes(StandardCharsets.UTF_8);
126 Object writeReplace() {
127 return new Proxy(getSerialized());