f26df831836cc7852cf31f9fd8a826199ffa70b1
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.Strings;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.DataInput;
18 import java.io.DataOutput;
19 import java.io.Externalizable;
20 import java.io.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectOutput;
23 import java.io.Serial;
24 import java.nio.charset.StandardCharsets;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.concepts.WritableIdentifier;
27
28 /**
29  * Type-safe encapsulation of a cluster member name.
30  */
31 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
32     private static final class Proxy implements Externalizable {
33         @Serial
34         private static final long serialVersionUID = 1L;
35
36         private byte[] serialized;
37
38         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
39         // be able to create instances via reflection.
40         @SuppressWarnings("checkstyle:RedundantModifier")
41         public Proxy() {
42             // For Externalizable
43         }
44
45         Proxy(final byte[] serialized) {
46             this.serialized = requireNonNull(serialized);
47         }
48
49         @Override
50         public void writeExternal(final ObjectOutput out) throws IOException {
51             out.writeInt(serialized.length);
52             out.write(serialized);
53         }
54
55         @Override
56         public void readExternal(final ObjectInput in) throws IOException {
57             serialized = new byte[in.readInt()];
58             in.readFully(serialized);
59         }
60
61         @Serial
62         private Object readResolve() {
63             // TODO: consider caching instances here
64             return new MemberName(new String(serialized, StandardCharsets.UTF_8), serialized);
65         }
66     }
67
68     @Serial
69     private static final long serialVersionUID = 1L;
70
71     private final @NonNull String name;
72
73     @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
74             justification = "The array elements are non-volatile but we don't access them.")
75     private volatile byte[] serialized;
76
77     private MemberName(final String name) {
78         this.name = requireNonNull(name);
79     }
80
81     MemberName(final String name, final byte[] serialized) {
82         this(name);
83         this.serialized = verifyNotNull(serialized);
84     }
85
86     public static @NonNull MemberName forName(final String name) {
87         checkArgument(!Strings.isNullOrEmpty(name));
88         // TODO: consider caching instances here
89         return new MemberName(name);
90     }
91
92     public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
93         final byte[] serialized = new byte[in.readInt()];
94         in.readFully(serialized);
95         return new MemberName(new String(serialized, StandardCharsets.UTF_8));
96     }
97
98     @Override
99     public void writeTo(final DataOutput out) throws IOException {
100         final byte[] local = getSerialized();
101         out.writeInt(local.length);
102         out.write(local);
103     }
104
105     public @NonNull String getName() {
106         return name;
107     }
108
109     public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
110         .@NonNull MemberName toYang() {
111         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
112                 .MemberName(name);
113     }
114
115     @Override
116     public int hashCode() {
117         return name.hashCode();
118     }
119
120     @Override
121     public boolean equals(final Object obj) {
122         return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
123     }
124
125     @Override
126     public int compareTo(final MemberName obj) {
127         return this == obj ? 0 : name.compareTo(obj.name);
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
133     }
134
135     private byte[] getSerialized() {
136         byte[] local = serialized;
137         if (local == null) {
138             local = name.getBytes(StandardCharsets.UTF_8);
139             serialized = local;
140         }
141         return local;
142     }
143
144     @Serial
145     Object writeReplace() {
146         return new Proxy(getSerialized());
147     }
148 }