BUG-5280: add cds-access-api identifiers
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / MemberName.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
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;
21
22 /**
23  * Type-safe encapsulation of a cluster member name.
24  *
25  * @author Robert Varga
26  */
27 @Beta
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;
32
33         public Proxy() {
34             // For Externalizable
35         }
36
37         Proxy(final String name) {
38             serialized = name.getBytes(StandardCharsets.UTF_8);
39         }
40
41         @Override
42         public void writeExternal(ObjectOutput out) throws IOException {
43             out.writeInt(serialized.length);
44             out.write(serialized);
45         }
46
47         @Override
48         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
49             serialized = new byte[in.readInt()];
50             in.readFully(serialized);
51         }
52
53         private Object readResolve() {
54             // TODO: consider caching instances here
55             return new MemberName(new String(serialized, StandardCharsets.UTF_8), this);
56         }
57     }
58
59     private static final long serialVersionUID = 1L;
60     private final String name;
61     private volatile Proxy proxy;
62
63     MemberName(final String name) {
64         this.name = Preconditions.checkNotNull(name);
65     }
66
67     MemberName(final String name, final Proxy proxy) {
68         this.name = Preconditions.checkNotNull(name);
69         this.proxy = Verify.verifyNotNull(proxy);
70     }
71
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);
76     }
77
78     public String getName() {
79         return name;
80     }
81
82     @Override
83     public int hashCode() {
84         return name.hashCode();
85     }
86
87     @Override
88     public boolean equals(final Object o) {
89         return this == o || (o instanceof MemberName && name.equals(((MemberName)o).name));
90     }
91
92     @Override
93     public int compareTo(final MemberName o) {
94         return this == o ? 0 : name.compareTo(o.name);
95     }
96
97     @Override
98     public String toString() {
99         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
100     }
101
102     Object writeReplace() {
103         Proxy ret = proxy;
104         if (ret == null) {
105             // We do not really care if multiple threads race here
106             ret = new Proxy(name);
107             proxy = ret;
108         }
109
110         return ret;
111     }
112 }