a6794d0032623dff80e2ef92d1d9e11a9c8c6d2d
[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.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.Identifier;
23
24 /**
25  * Type-safe encapsulation of a cluster member name.
26  *
27  * @author Robert Varga
28  */
29 @Beta
30 public final class MemberName implements Comparable<MemberName>, Identifier, WritableObject {
31     private static final class Proxy implements Externalizable {
32         private static final long serialVersionUID = 1L;
33         private byte[] serialized;
34
35         public Proxy() {
36             // For Externalizable
37         }
38
39         Proxy(final byte[] serialized) {
40             this.serialized = Preconditions.checkNotNull(serialized);
41         }
42
43         @Override
44         public void writeExternal(ObjectOutput out) throws IOException {
45             out.writeInt(serialized.length);
46             out.write(serialized);
47         }
48
49         @Override
50         public void readExternal(ObjectInput in) throws IOException {
51             serialized = new byte[in.readInt()];
52             in.readFully(serialized);
53         }
54
55         private Object readResolve() {
56             // TODO: consider caching instances here
57             return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
58         }
59     }
60
61     private static final long serialVersionUID = 1L;
62     private final String name;
63     private volatile byte[] serialized;
64
65     private MemberName(final String name) {
66         this.name = Preconditions.checkNotNull(name);
67     }
68
69     MemberName(final String name, final byte[] serialized) {
70         this(name);
71         this.serialized = Verify.verifyNotNull(serialized);
72     }
73
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);
78     }
79
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));
84     }
85
86     @Override
87     public void writeTo(final DataOutput out) throws IOException {
88         final byte[] serialized = getSerialized();
89         out.writeInt(serialized.length);
90         out.write(serialized);
91     }
92
93     public String getName() {
94         return name;
95     }
96
97     @Override
98     public int hashCode() {
99         return name.hashCode();
100     }
101
102     @Override
103     public boolean equals(final Object o) {
104         return this == o || (o instanceof MemberName && name.equals(((MemberName)o).name));
105     }
106
107     @Override
108     public int compareTo(final MemberName o) {
109         return this == o ? 0 : name.compareTo(o.name);
110     }
111
112     @Override
113     public String toString() {
114         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
115     }
116
117     private byte[] getSerialized() {
118         byte[] local = serialized;
119         if (local == null) {
120             local = name.getBytes(StandardCharsets.UTF_8);
121             serialized = local;
122         }
123         return local;
124     }
125
126     Object writeReplace() {
127         return new Proxy(getSerialized());
128     }
129 }