Remove JournalWriter.getLastEntry()
[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.IOException;
20 import java.nio.charset.StandardCharsets;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.concepts.WritableIdentifier;
23
24 /**
25  * Type-safe encapsulation of a cluster member name.
26  */
27 public final class MemberName implements Comparable<MemberName>, WritableIdentifier {
28     @java.io.Serial
29     private static final long serialVersionUID = 1L;
30
31     private final @NonNull String name;
32
33     @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
34             justification = "The array elements are non-volatile but we don't access them.")
35     private volatile byte[] serialized;
36
37     private MemberName(final String name) {
38         this.name = requireNonNull(name);
39     }
40
41     MemberName(final String name, final byte[] serialized) {
42         this(name);
43         this.serialized = verifyNotNull(serialized);
44     }
45
46     public static @NonNull MemberName forName(final String name) {
47         checkArgument(!Strings.isNullOrEmpty(name));
48         // TODO: consider caching instances here
49         return new MemberName(name);
50     }
51
52     public static @NonNull MemberName readFrom(final DataInput in) throws IOException {
53         final byte[] serialized = new byte[in.readInt()];
54         in.readFully(serialized);
55         return new MemberName(new String(serialized, StandardCharsets.UTF_8));
56     }
57
58     @Override
59     public void writeTo(final DataOutput out) throws IOException {
60         final byte[] local = getSerialized();
61         out.writeInt(local.length);
62         out.write(local);
63     }
64
65     public @NonNull String getName() {
66         return name;
67     }
68
69     public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
70         .@NonNull MemberName toYang() {
71         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
72                 .MemberName(name);
73     }
74
75     @Override
76     public int hashCode() {
77         return name.hashCode();
78     }
79
80     @Override
81     public boolean equals(final Object obj) {
82         return this == obj || obj instanceof MemberName && name.equals(((MemberName)obj).name);
83     }
84
85     @Override
86     public int compareTo(final MemberName obj) {
87         return this == obj ? 0 : name.compareTo(obj.name);
88     }
89
90     @Override
91     public String toString() {
92         return MoreObjects.toStringHelper(MemberName.class).add("name", name).toString();
93     }
94
95     private byte[] getSerialized() {
96         byte[] local = serialized;
97         if (local == null) {
98             local = name.getBytes(StandardCharsets.UTF_8);
99             serialized = local;
100         }
101         return local;
102     }
103
104     @java.io.Serial
105     Object writeReplace() {
106         return new MN(getSerialized());
107     }
108 }