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.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.nio.charset.StandardCharsets;
20 import org.opendaylight.yangtools.concepts.Identifier;
23 * Type-safe encapsulation of a cluster member name.
25 * @author Robert Varga
28 public final class MemberName implements Comparable<MemberName>, Identifier {
29 private static final class Proxy implements Externalizable {
30 private static final long serialVersionUID = 1L;
31 private byte[] serialized;
37 Proxy(final String name) {
38 serialized = name.getBytes(StandardCharsets.UTF_8);
42 public void writeExternal(ObjectOutput out) throws IOException {
43 out.writeInt(serialized.length);
44 out.write(serialized);
48 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
49 serialized = new byte[in.readInt()];
50 in.readFully(serialized);
53 private Object readResolve() {
54 // TODO: consider caching instances here
55 return new MemberName(new String(serialized, StandardCharsets.UTF_8), this);
59 private static final long serialVersionUID = 1L;
60 private final String name;
61 private volatile Proxy proxy;
63 MemberName(final String name) {
64 this.name = Preconditions.checkNotNull(name);
67 MemberName(final String name, final Proxy proxy) {
68 this.name = Preconditions.checkNotNull(name);
69 this.proxy = Verify.verifyNotNull(proxy);
72 public static MemberName forName(final String name) {
73 Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
74 // TODO: consider caching instances here
75 return new MemberName(name);
78 public String getName() {
83 public int hashCode() {
84 return name.hashCode();
88 public boolean equals(final Object o) {
89 return this == o || (o instanceof MemberName && name.equals(((MemberName)o).name));
93 public int compareTo(final MemberName o) {
94 return this == o ? 0 : name.compareTo(o.name);
98 public String toString() {
99 return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
102 Object writeReplace() {
105 // We do not really care if multiple threads race here
106 ret = new Proxy(name);